Working with Collections in Flow

Working With Collections in Flow

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.

9 Comments

  1. Lets say I have a collection variable with 5000 records in it.
    Now I want to Loop thru the collection, and find the record that matches a stored ID.
    How do I do that matching process? Is there a FIND action or something like that? Or do I have to do a LOOP and a DECISION to find the matching record?
    All help appreciated!

    • Hi,
      If you know the Id, then I think you can just do a get for it.
      Otherwise I would say do a loop and use the decision element to find the matching record. However, since you have 5000 records in the collection, you might get the “Number of Iterations Exceeded” error.

      I recommend you to check “Find Record in Collection” action.
      https://unofficialsf.com/list-actions-for-flow/

  2. Say you are creating a new record collection and want to add new records to it that don’t yet exist. Have you found a way to do this without having to first create such a record in the database and then do a Get on that record? This is the only way I have found to do this. It feels quite silly. Then, when the loop is done, I must delete the record I created so it doesn’t stay in the db as an orphan.

    • I figured it out. I will share: It’s so simple. Instead of trying to use Create Record, we do New Resource>Variable>Record, and specify the type of record. Then change the values of that Record Variable in a loop, and in each loop iteration, assign that variable to a collection…got it!

      • Exactly. You have to create a record variable, use an assignment to assign values to its fields, and then another assignment to add it to the collection variable. At the end you can create/update/delete them at once.

5 Trackbacks / Pingbacks

  1. Passing Multiple Records to Flow - Salesforce Time
  2. Limit Collections in Flow - Salesforce Time
  3. Creating a Roll-Up Summary with Flow - Salesforce Time
  4. DML Operations for an Empty List in Flow - Salesforce Time
  5. Using Flow to Share Files with Records - Salesforce Time

Leave a Reply

Your email address will not be published.


*