The essentials of Loops in Flows

Loops in flow builder let you do interesting things, but it may take some time to wrap your head around how they work. Let us start at the beginning.

In flow builder you can work with singular variables and collections variables. Thes can be collections of records, but also more simple variables like numbers or text can be added to collections.

In order to perform an action for each individual variable in such a collection, you employ a loop. Within the loop, you can manipulate one of the variables in the collection. After completing those steps, you return to the loop element to repeat the same steps for the next variable in the collection, until the entire set has been processed.

This may sound somewhat abstract, so let's explain it using examples. Suppose you have an Account that undergoes a name change. Consequently, the website domain and email addresses of the employees also change. This Account has 50 contacts, where only the email domain needs to be adjusted. In this example, we utilize a flow with a loop.

It typically starts with a Get records element to retrieve the collection for which you want to execute the loop, so let's fetch those Contacts.

Next comes the loop element.

Within the loop, something needs to be done for each Contact. You do not employ Get, Create, Update, or Delete elements within the loop. Not only is it inefficient to perform such actions one by one, but it also carries the risk of your flow not functioning correctly. This is because within a flow transaction (which may have begun before your flow and continue after your flow due to other responding flows), you are only allowed a maximum of 100 queries (retrieving information from the database) and 150 DMLs (writing to the database to create, edit, or delete records; DML stands for Data Manipulation Language). Therefore, updating those Contact records must be done differently.

Hence, within the loop, we only work with the yellow elements such as Assignments, Decisions, and Collection Filters. In this example, since it is simple, we only need two assignments.

In the first Assignment, we make the modification to one of the Contacts.

Here, newEmail is a SUBSTITUTE formula where I replace the old domain name with the new one. We have now updated the email address in one of the Contacts within the Contact record variable, but nothing has changed in the database yet.

To save the changes, we need a new record collection for Contacts. We cannot use the Get_Contacts collection because it only contains unmodified Contacts.

If I were to use Get_Contacts now for the Update Records element, nothing would change because the Contact variables in that collection still have the exact same values as when we retrieved them.

Adding the modified Contacts to Get_Contacts would result in an error because we would be adding a record with the same record ID as a record already present in the collection. This means that within a single database command, we would have two different instructions to update the same record.

Therefore, you add the modified Contact to a new collection.

The Contact record collection variable to perform the update
The Assignment where you add the modified individual Contact variable to the collection variable

This is done for each Contact. If there were 5 contact records found in Get Contacts, the loop performs its trick for all 5 contacts, resulting in a collection of 5 contact records with different email addresses than those in the original collection.

After the last element of the loop, so outside of the loop, we utilize this updateContacts collection in order to update all 5 Contacts in one operation.

The flow as a whole the looks like this.

Of course, this is merely the foundation, and there is much more to learn about how to cleverly work with loops.