How to Build Salesforce Flow Loops: Best Practices and Examples
By Tim Combridge
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.
More like this:

Comments: