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.