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.
- Repeat the same actions for every item in one collection.
- 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.
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.
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!
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).
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.
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!