I recently leaded a user group meeting about Flows and someone asked what the difference is between variables, collection variables, sObject variables, and sObject collection variables. It can certainly be a little bit confusing and its even more confusing to know when to use each type. I certainly don’t get it right the first time every time but hopefully this brief overview will help you select the right one more often. For this we’ll use the many costumes of Astro to help us understand what data we can put into each variable.
Variables are for holding a single piece of information. This might be the record id of the record from which you started the flow or the record id of the record you created in the flow. If we look at variables as if they are coming from an Excel spreadsheet, this is a single cell of the spreadsheet. They are particular useful for passing data into a flow from a button or a process builder or returning it to your button to take you to the newly created record. In this case we might be storing (RED) Astro or Lightning Astro in our variable. We can only store one piece of data though with this variable.
Collection variables are great ways to bring over the same piece of data about multiple records. So instead of just one record id, you could lookup all records that meet a specific criteria and store all the record ids of the records that match in a collection variable. Going back to the Excel spreadsheet, this is a single column of data. You might use this during a loop where you want to update all of the records in the same manner. With this variable, we could store all of the column A or all of column B. If we are righting a flow where we need to know all of the outfits that Astro wears, we can store all of column C in a collection variable.
An sObject variable is used for storing multiple fields from the same record. If you are going to create a new record based on an existing record, you can use an sObject variable to hold all of that information so that you can populate it on the new record. This is like the row in Excel. You have to specify which fields off the record to store but you can then reference any of those fields from the one variable. For this variable, we might store all of row 2 or all of row 3 in the variable. We will have a main piece of data, in this case Dolphin Astro, then also the representing and outfit of astro. But we can only store one version of Astro in this variable.
sObject Collection Variable
Since the collection variable is a column in Excel and the sObject variable is a row, it stands to reason that the sObject collection variable would be both. In other words, it is the whole Excel sheet. You can store multiple records and the corresponding fields for each of the records in this variable. If you need to find all of the children of a record and also store multiple fields, use this. Or if you need to find any record that is a specific record type or owned by a specific person and also store multiple fields from these records, this is the right variable. This is the ultimate of variables. We get to store all of Astro’s personas as well as all of the information about each of the personas.
There are a few limitations on when you can use certain types. This is the number one scenario when I find myself making a last minute change to the variable I selected. Hopefully this brief overview gives you a better understanding of what each variable offers you.