When you’re building a Salesforce flow, there are often times where you’ve got a collection of items and you want to perform a specific action or check an individual item. For example, let’s say 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 a number of 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 action.
- 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 basic 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.
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!
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.
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 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:
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.
Now that you have your Collection, it’s time to loop through the records and change the Active 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. (Best practice tip incoming!) Never perform a DML Statement within a Loop! We’ll talk more about this later.
You’re going to perform a ‘double assign’ – this is a nickname I’ve given to the method of assigning a Variable and then assigning it to a new Collection.
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. Create your Update element as follows:
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 of 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 one.
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).
Summary and Further Learning
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’s 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.
One more source that I used quite a bit when learning about flows back in my early days was Rakesh Gupta’s AutomationChampion.com. There are a bunch 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.