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".
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.
Great comprehensive post. Thank you.
Thank you!
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/
I too need answer for this.
Thanks for making this clear. I was looking for flow tutorials. There are so many out there but yours is clear.
Thank you, I am glad that you found it useful.
I wish I had read this post two years ago. It’s the best description of collection use anywhere. Additionally it clarifies my understanding of using assignments in a loop and why to do so.
Thanks very much,
Pete
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.
A year later, just want to say thanks for coming back to the comments to share the answer! So frustrating when people just say NVM, figured it out.
The confusing thing about adding a record to a collection is the screen shows the parent records and nothing to select. Figured it out by just clicking out of that selection screen. Hopefully that helps anyone else out who also landed here!
How would we reference the record IDs (or even an individual record ID) of those newly created records within the flow?
Hi Kris,
Sorry for the very late reply.
When you use a variable (single or collection) to create records, then the IDs of those new records are assigned to the variable that you just used.
For example let's say that you used a variable called AccountRecord and then used it to create a single account record. After the create, {!AccountRecord.Id} will store the Id of that new account.
Hi Yumi, I tried to go through the same steps, but it turned out different. You used {newWoli} for the first assignment, that I assume is the Record Single Variable resource. And you assigned each field to the item in the loop relatively... What the problem is, that in my side it is not possible to use {newWoli} as a value in the second Assignment. Because since {newWoli} is the Record Single Variable, I can't select it without a field.
Hi,
I assume you are talking about the second assignment, which is adding the record variable {newWoli} to the collection.
In this case, when you select the record variable, I mean {newWoli}, it asks you to select a field and that part is a little confusing. In this case, even though it looks like you need to select a field, actually it is not a must. Just click somewhere else without selecting a field from the {newWoli}.
By the way, just a small update, Salesforce changed the logic of the assignment element. Now you don't need to have the second assignment. You can do it in the first assignment as the last line.
Hi Yumi, great post with valuable information!
One question though: I have a flow with "Get records", which gets 3 contact records. I now want to send out an email in which I reference all 3 contact records (their name & their mail address), e.g. "Hi Employee, please check those contacts:
Email first contact, Name first contact
Email second contact, Name second contact
Email third contact, Name third contact
Is that somehow possible? I assume when I use a loop I´d send out 3 emails and not just one single email, right?
Thanks a lot!
Thanks! You can use the loop to create a text collection of email addresses. Then you can use the send email action of flow to send the same email to multiple email addresses (emails that you have in the text collection). So in this case you will need to build the email body as a text template in the flow and not use the email alert.
Hi Yumi!
Your post are always on point.
I have this flow designed to create reimbursement record for users.
After entering all their info in the “General Information screen”, The flow then takes me to a new screen where I need to enter the reimbursement info based on the type of expense (3 different types) selected on the initial screen. On this same screen, there is a radio button with a question asking if I would like to create another record for the current employee. If I answered yes and click next, the flow takes me back to the initial screen (the General Information Screen).
But If I suddenly changed my mind and click on “Previous” to take me back to the previous screen (For instance Per Diem Information screen) and changed my response to a “NO” (Meaning I do not wish to create another record for the employee), and then click on “Submit”. The flow somehow creates the record, anyway, repeating all the info that I entered for the employee the first time. Instead of 1 record, I now have duplicates. I also noticed that when I navigate back to the "General information screen, all the initial values are still retained in the fields.
The "Get Record" element is stored in a variable called "varReimbursementId". From your post, how can I use the idea of collection variable to prevent the flow from creating duplicates when I navigate away from a screen to previous one and then submit.
Thank you
hi, how to create "add to collection"
You have to create a record collection variable, add an assignment element to the canvas, then add the record variable to the record collection variable.
HOw i create the newWOLI?
You have to create a new record variable and select WorkOrderLineItem for the object.
I am new to flows, especially with loop.
When a record in my custom object is created, there are picklists that do not have a value. I want to update 8 corresponding text fields with the value of a picklist but only the first time an value is chosen and not on subsequent changes to each picklist.
So, I have Picklist1 which correlates to Text1 (Picklist2 to Text2, Picklist3 to Text3 and so on...) and I want to create a flow which updates Text1 with Picklist1 value the first time Picklist1 is updated. I can do this with a flow but what I cannot work out is how to loop through all 8 picklist and text fields. Is loop and assignment elements what I need to solve this?
Hi Peter,
Loop element is not needed in this case. You can build a before save record triggered flow that will update those text fields according to the picklist fields.
Really useful post! What i am struggling to find anywhere is how to add collection values together without installing a package. i have created a collection of DOBs and want to add them all together then divide by count to get the average DOB for all users on the account. any suggestions would be greatly apprecited