When youâre building a Salesforce Flow, there are times when youâve got a collection of items and want to perform a specific action or check an individual item. Letâs say, for example, that you wanted to create a number of tasks related to a record when users made selections on a multi-picklist. Rather than layering the flow with multiple Decision elements (i.e., did they select option A, did they select option B, and so on) â this is where Loops would come in to simplify the flow processing.
If youâre looking for a way to perform a specific action more than once, Loops are going to be your best friend. In this article, weâll dive into what Loops are, how to use them, and some best practices to keep in mind.
What Is a Loop in Salesforce Flow?
A Loop is a Salesforce Flow element that is used to iterate through several items in a collection variable. There are three main components of a Loop:
- Collection Variable: This is the Collection you want to loop through â the Collection contains multiple Variables, each of which you want to either assess or take action on.
- Loop Variable: This will be the temporary holding place of a single Variable from within the Collection as it is being processed. This can either be created manually or can be created automatically when setting up the Loop.
- Direction: This allows you to choose whether you want to Loop from first to last, or last to first item in a Collection. In a lot of standard scenarios, this isnât so critical.

How to Create and Use a Loop
Letâs use this scenario: An account has an Active__c checkbox field, as does the Contact object. Your manager has asked you to create a flow that marks all child contact records as active or inactive, based on the value of the accountâs field. There are multiple ways to do this â one of which is to use a Loop to iterate through all the contact records and use an assignment to set the new value.
If youâre following along at home, all you need to do is ensure thereâs an Active__c checkbox field on both the account and contact.
Note: There are definitely better ways to execute this example, but this example provides a very simple way to demonstrate the Loop feature, so weâre going with it!
The first step is to create a new record-triggered flow that runs After Update. This will be triggered whenever an account recordâs Active__c field is changed.

The next thing you need to do is collect all the relevant contact records into a Collection so that you can process them in a Loop. After the Start element, create a Get Records element as below:

Make sure youâve selected the Triggering Recordâs Account Id ({!$Record.Id}) as the Value in the filter criteria. Ensure that youâve selected All Records under the How Many Records to Store header. This ensures that a Collection, rather than a single record variable, is captured. Thereâs also no need to put a cap on the number of records coming through, as we want to process all the child records.
Now that you have your Collection, itâs time to loop through the records and change the Active__c field according to the accountâs new value. To do this, create a Loop after the Get Records element. Use the Collection that was created in the Get Records element.

The next step is to assign the new value, using an assignment.
Note: Never perform a DML Statement within a Loop! Weâll talk more about this later.
There are two Assignments youâll need to make in this next step. The first Assignment is to assign the values of the individual variable within a Loop, and the second one is to add the Record to a new Collection to be updated. Picture it like this: when you did your Get Records, you gathered a number of contacts and put them into a Collection.
Now, imagine that Collection is a box of records. Then, when you created your Loop you are saying that you want to get each Contact out of the box, one by one, and do something to it. The For Each path is how you handle each individual record.
The first assignment will be used to set the new Active value on the Contact Variable. Create your Assignment Variable within the Loop as follows:

The second assignment will be used to put the contact into a new Collection that youâll later use to update all the contact records at once. Firstly, youâll need to create a new contact Collection Variable as follows:

You now need to assign the contact record to the new Collection Variable. This is so that you have a single Collection Variable to update after the Loop has closed. This assignment needs to be created within the flow, but after the first assignment. Create your assignment as follows:

Finally, you need to create your Update at the end of your flow. This will commit the changes youâve assigned and update the contact records. Select Use the IDs and all field values from a record or record collection to ensure youâre able to set colUpdateContacts as the input collection for the update. Below is what your Update element should look like:

Thatâs it! Your flow is complete. All you need to do now is test it (we wonât go into that in this article). Your completed flow should look something like this:

Best Practices When Using Loops in Salesforce Flow
While creating the above flow, weâve already discussed some best practices that need to be taken into account while using Loops in Salesforce Flows. Letâs reiterate and go into further detail.
Never Perform a DML Statement Inside a Loop
Avoid using the pink Data elements inside a Loop. Thereâs no better way to avoid hitting a governor limit than designing to avoid them!

In the flow above, youâre only pushing a single DML statement â the Update Contacts at the end of your flow. If youâd use the Update Contact element inside the Loop instead of using the double assign tactic, thereâs no way to predict how many times itâll be used.
You may have an account with over a thousand contacts, which means the flow will attempt to use the Update call a thousand times â this will fail. If you assign a thousand records and use the Update call once (as youâve done above), you wonât run into the same issue.
Assign To a New Collection Rather Than Reusing the Original
Reusing a variable is never a good idea, especially when working with Loops. Itâs better to create a new Collection Variable and populate it with your updated records, for use later on in the flow (in the example above, updateContact was your new Collection Variable).
There May Be a Better-Suited Tool
As I mentioned earlier, the Loop function isnât necessarily the best way to handle that scenario. The scenario was constructed to simply communicate the Loop tool without getting too bogged down in the story behind it. Believe it or not, this entire flow couldâve been done using a single element, as shown below.

In the screenshot above, Iâve configured an Update Records to update the records related to the triggering account (in this case, the contacts). Iâve set further filter criteria to only pick up the contacts where the Active__c checkbox does not equal the accountâs Active__c checkbox value (in other words, theyâre different).
Then, Iâm setting the Active__c checkbox on the contact equal to that of the account. No Loop required!
Another tool that you may want to use instead of a Loop is a Transform element. Letâs say you want to create a collection of opportunity line items from a collection of products that a user has selected in a screen flow. Firstly, youâd need to get the price book entries that are relevant to the product and the opportunity. The next step doesnât need to be a Loop, but instead could be a Transform.
In the example below, thereâs a Get Record element that is gathering the opportunity record and another one thatâs grabbing relevant price book entries. Next, thereâs the Transform element that uses both collections to map fields to a new collection of opportunity line items. These can then be used as required throughout the rest of the flow.

Final Thoughts
In the above example, you learned how to use Loops in a flow and also some key best practices to keep in mind when using Loops. If youâd like to learn more about using Loops and using flows in general, there are a few options Iâd recommend.
Firstly, you have Trailhead. Trailhead is Salesforceâs free training platform that has multiple flow-based modules, all of which you can study at your own pace.
Another that Iâve used quite a bit when learning about flows back in my early days was Rakesh Guptaâs AutomationChampion.com. There are plenty of fantastic examples of flows that use Loops (like this one). Theyâre a little bit more complex than Trailhead, but offer a great next step for those who are looking to further their flow education.








Comments: