Flow is the only automation tool that can be used to perform actions on a collection of records (except for Process Builder, which lets you update multiple child records at once). Collections can be described as a list of records or values that you want to work on. In most of the cases, a loop element is needed to perform the operations for each item in the collection.
How to Create a Collection Variable?
There are two ways to create a collection variable in Flow Builder.
Firstly, when you choose to store all the records in a Get Records action, Flow Builder creates a collection and stores those records in it. If you choose to automatically store all fields, then all the field values of those records will be stored in the collection. If you want to choose fields, then only those fields and record Ids will be stored in the collection that Flow Builder creates for you. However, if you want to create a collection variable manually and store these records in it, it is also possible. To do so, select “Choose fields and assign variables (advanced)” option and then select the collection variable that you want to use.
When Flow Builder creates a collection for you, it gives a name to the collection in a specific format;
Object Name(s) from API Name of the Get Records action.
For example, if you have a Get Records action to get task records and you name this element as “Get My Open Tasks”, then the name of the collection becomes “Tasks from Get_My_Open_Tasks”.
Second way to create a collection variable is creating any data type of variable and marking “Allow multiple values (collection)”. This checkbox makes it a collection variable, which means that it can store multiple values of the same data type or records of the same object in an ordered list. In the example below, there is a collection variable that can store multiple account records.
After creating a collection variable, it means that you have a variable that will store a list of records/values. So, you need to add those records to this collection. If you chose to store the results of a get records element in a collection, then your collection variable will have records in it. However, if you manually create a collection variable, then you will need to add records in it. To do so, you should use the Assignment element.
To perform actions to each record in a collection variable, a Loop element should be used.
What is Loop?
Loop element iterates over items in a collection variable. It temporarily assigns the current record to the loop variable, so that desired actions can be done for that record. To reference each collection item in elements along the loop path, you can use the loop variable. After it’s done with that record, it gets removed from the loop variable and the next record is stored in it. This continues to happen until the last record in the collection and after that, the loop ends and flow continues with the next element.
How to Use Loop?
To start a loop path for iterating over items in a collection variable, drag the Loop element from the Toolbox and drop it on the canvas. Give it a name and select the existing collection variable. Lastly, select the direction for iterating over the selected collection. You can iterate from the first item to last item, or the opposite way.
Loop element temporarily stores the current item in the loop variable. To use the current item in other elements in the loop, use the API name of the Loop element. Example: if your flow iterates over accounts with a Loop element named “My_Account_Loop” you can reference the current item from that loop element. Just start typing “My_Account_Loop” and select “Current Item from Loop My_Account_Loop”.
Before doing a loop, it is recommended to add a decision element to check if the collection variable is null or not and continue to the loop only if it is not null. This way, you can avoid getting errors.
After the loop element is created, you can perform actions for each item in your collection.
Bulkify the Flow
Because Salesforce is a multi-tenant environment, there are many governor limits that apply to code and automation. To avoid hitting the governor limits, you should always perform the DML actions (get, edit, create, or delete) out of your loop. In order to do so, use the assignment element inside the loop. At the end of the loop, perform the action for all the records at once.
In the example below, first assignment is used to assign values to a new record variable using the values from the current item in the loop and the second assignment is used to add the variable to a new collection variable. At the end of the loop, all of the records are created at once.
In order to create multiple records using a collection, you have to make sure that there is no Id value. When performing an update for multiple records using a collection, make sure that Id value is not blank for the records. This way, flow knows which Salesforce record to update.
Checking the Size of a Collection
In some use cases, you might need to know how many records are in a collection. There are two ways to know the size of a collection variable.
First of all, you can create a loop to add 1 to a number resource which has a default value of 0. This way, at the end of the loop you will have a number resource that stores the count of records.
However, there is an easier way to do this. If you have a collection variable, you can use the assignment element to assign the record count to a variable.
There are many use cases of loops in Flow Builder. Most important thing to remember about this topic is not to use any DML operations inside the loop and to use assignment elements.