In this blog, we will understand the concept of loops in Salesforce flow. With the help of an example let’s discuss loops quickly and later on we will use loops to create multiple records in Salesforce.
Let’s create a screen flow and use the loop element over there.
Firstly, provide a label then the API name will be automatically populated. So first create a collection variable, for this click get records, and fetch all the accounts available in the org.
Don't forget to check out: Salesforce: Advanced 'Change Owner' Control in Flow and Apex
Now we will use the loop element and fill in the necessary details as shown in the example below.
So here we will also use a screen inside the flow to show the working of the loop and we will also provide an assignment operator to how many times the loop is called inside the flow. After that save the flow and activate it.
Here is the complete flow using the Loop element in Salesforce. Now we have to debug this flow. But before debugging click on run as we have to check the number of accounts, we have in our org for this we will simply use the Salesforce inspector plugin and use query as shown below:
select count(Id) from Account
This shows how many account records we have to fetch. Now let's move to the debug process and click on run. As we can see this is showing the records in our account object with the help of a loop by clicking next, we can see all the records until we get the last record the loop will stop.
To Change the Working of the Loop
If we move to the flow in our edit loop, we also have a specified direction for iterating over the collection as the last item to the first item so this will show the records from the last record in the object instead of the first record. So, we can also change the order from the loop element.
Check out another amazing blog by Bhawana here: Decorators in LWC and its Types | Salesforce Lightning Tutorial
To Show the Size of the getRecord Element
In the edit assignment, we have to change the value of the count variable’s operator from add to count and provide value as getRecord element or collection variable as shown below.
This will get the total account records with the help of a loop.
So, this is all about the loop in Salesforce flow.