Flow: How To Use “Loop”

flow loop - variable

Loop is a very common concept in all programming languages, but I find the flow Loop element very unique in Salesforce. To separate the two, let’s call the loop in real life “Repetition”, and the Loop element in Salesforce “Loop”.

In a real life, there are two scenarios when repetition is needed.

  1. Repeat the same actions for every item in one collection.
  2. Repeat the same actions for a specific number of times. (Run this action for 5 times)

In Salesforce, the Loop can only achieve the first scenario. However I will show you how to handle the second without using a Loop in this article.

Scenario 1 – Repeat By Loop Element

Step 1 – Create a collection variable

First you need to have a collection variable. The most common way is by using Get Records with multiple records return. For example, here I got all the accounts with rating = Hot.

flow loop - step 1
Step 2 – Put in the Loop element

Assuming I want to create one task for each hot account, I will need to use the Loop. For the configuration, you only need to enter which collection variable you want to loop through, and whether you want to start from the first or the last item (Usually useful for date-related operations)

At the bottom there is a long paragraph which looks complicated, but basically you can think of Loop as “For each account, do ____ and ____”. In order to take out each account from the collection, the system will create a new variable – Current Item from Loop XXX. Then later in the loop, you will need to use this variable to execute the actions.

flow loop - step 2
Step 3 – Add actions inside the loop

After I have the loop, I need to add actions that will run for each record. Here I want to create new tasks for each account. As reminded by the community, the best practice is to store the new records in a collection variable and then use the Create Records only once. First, we need to create a record single variable and a record collection variable.


When I am assigning the Related Id, I can see there is a “Current Item from Loop – For Each Account”. By clicking it, I can get the Account Id for each item, and then create a task associated with it. Also, I put all actions in one assignment here just to demonstrate, but please remember to move the add to collection action to another assignment!

flow loop - step 3

Then you connect the loop with the action. You will see two options – For each item in the collection and After last item in the collection. Choose the first one and we will talk about the second in step 5.

Step 4 – IMPORTANT! Close up the loop!

This is the most unique part that differentiate the repetition in Salesforce and in other programming languages. After you finish the actions, you have to close the loop up manually!!! This is very very very important, as if you forget to do so, your flow stops at the first iteration.

Here we can conclude, the Loop element is not a complete loop actually, but a function that helps to take out each item in one collection by order (It confused me pretty well in the beginning).

flow loop - step 4
Step 5 – Add actions after the loop is done

If you want to continue your flow, you have the option “After Last Item”. Drag it out from the Loop element, and then your flow will go on after all items have been looped through. Here we need to add the Create Records at the end, so the system and create all tasks at once.

flow loop - step 5

Then we finish with how Loop should be set up and utilized. If you want to know how to run the same repetitions unrelated to collections for specific times, check out this article!

Check Out How Loop Can Be Used In Real Cases!
Use Case: Get a List of Names of Related Records to a Field
A while ago we published a Use Case to create ...
Read More
Use Case: Update All Related Contacts (Account Contact Relationship)
Do you have the Account Contact Relationship enabled for your ...
Read More
Use Case: Clone Any Objects with Related Lists with Flow
"Clone with related" is an amazing feature which sadly is ...
Read More
Use Case: Simple Loop Example in Flow
Earlier we have published the how-to-guides on Loop and Counter, ...
Read More
Use Case: Mass Update Records From Related List #2
In this article we introduced the magical ids variable to ...
Read More
Use Case: Mass Update Records From Related List #1
Have you heard of inline editing? This is my favorite ...
Read More
Use Case: Create Custom Roll-Up Summary With Flow
I have really mixed feelings about the roll-up summary field ...
Read More
Use Case: Notify Owners When Case Merged Or Master Case Closed
I believe many are excited about the Case Merge release ...
Read More
Use Case: Clone Assets To Opportunity Products
I received this case to auto-create Opportunity Product or Quote ...
Read More

Official Salesforce Help Article On Loop

Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anthony Foster

You’ll want to bulkify your loop “create record” action by adding the new records as a record variable then adding the new record to a collection of records to insert/update in a single DML action. Also, creating records in a loop is not best practice and increases the chance of hitting platform limits.