How and When to Use Loop in Salesforce Flow

How and When to Use Loop in Salesforce Flow

A collection is basically a list of records or values that you aim to work on. In order to perform actions for each item in a collection, you need to use the Loop element, which is one of the main elements of flow. Therefore, it is crucial to understand how and when to use Loop in Salesforce Flow.

Loop Element

The Loop element iterates over items within a collection variable by assigning the current record to the loop variable temporarily. This allows the execution of specific actions for each record. You can use the loop variable to reference individual collection items along the loop path. Once the operations for the current record are completed, it gets removed from the loop variable, and the next record takes its place. This process continues until the loop reaches the last record in the collection. Following this, the loop concludes, and the flow progresses to the next element.

How to Use the Loop in Salesforce Flow

This element can iterate over items in any type of a collection variable. For instance, it can be a collection of Account records, a collection of text variables, or even a collection of Apex-defined variables coming from an HTTP callout.

After adding a new Loop element into the Flow canvas, give a name to the element and choose your existing collection variable. Then, select the direction for iterating over the selected collection. You can iterate from the first item to last item, or from the last item to first item.

For instance, let's assume that you have a Get Records element that brings Case Contact Role records. Here is how to use a Loop element to iterate over those records.

Loop in Salesforce Flow

This element temporarily stores the current item in the loop variable. You can use the API name of the Loop element in order to reference the current item in other elements within the loop. For instance, you can type "Loop_Contact_Roles" and select "Current Item from Loop Loop_Contact_Roles". This will return the current Case Contact Role record. Pay attention that the current item is always a single variable with the same data type as your main collection variable. For instance, if you are looping through a text collection variable, current item will be a text variable.

After finishing the Loop element's configuration, you can perform actions for each item in your collection.

Using Assignment Element in a Loop

Salesforce is a multi-tenant environment and there are many governor limits that apply to code and automation. Therefore, you should always perform the DML actions and SOQL (all the pink elements in flow) out of your loop. In most of the cases, you will need to use an Assignment element within the loop. Then at the end of the loop, perform the action for all the records at once.

For instance, let's say that we want to clone all the Case Contact Role records. After configuring the Loop element, add an Assignment element and start assigning values to a record variable. As you can see, you have to reference the current item from the loop.

Order of the lines in an Assignment element is important. After assigning values to a record variable (in this example it is CaseContactRole_Variable), add this variable to a record collection variable. See the last line of the Assignment element.

Assignment Element in a Loop

After the Assignment element, go back to the Loop and after the last item, create all the Case Contact Role records at once. This will make sure that your entire loop will consume only one DML (even if you work with hundreds of records).

Create Multiple Records
End of the Flow

When to Use Loop in Salesforce Flow

There are endless use cases where you will need to use the Loop element in Salesforce Flow. Basically, we can say that you need to use this element if you want to perform actions for each item in a collection. When we say "perform actions" you may directly think about DML actions. However, it doesn't always need to be a DML action. For instance, you can use a Loop element just to prepare a variable in flow.

On the other hand, there are some situations where you may want to update multiple records at once. At this point, it is crucial to understand when and how to use Loop in Salesforce Flow because you may find out that you don't even need to use it at all.

Let's see some examples.

1- Creating Multiple Records

Let's look at the previous example again. You have a collection of Case Contact Role records and you want to clone them. You have to use Loop and Assignment elements to assign values to a temporary record variable and add to a collection. Then you can create them all at once. This is one of the most common use cases of the Loop element.

2- Updating Multiple Records

This one is tricky because you don't always need to use a loop. For instance, let's say that you want to build a record-triggered flow that closes the open Case records of an Account. Do you need to use a Loop? No!

As you can see, only one Update Records element with a criteria can do the job. You don't need to get the open Case records, loop through them, assign to variables and then update them.

Update Records Element to Close the Open Cases

So, don't we need a Loop for updating multiple records? It depends, if you want to make the same update (i.e, update status to Closed) then you may not need a Loop. However, if you want to make different updates (conditional or using values from each item), then you have to use a loop.

Let's say that you want to update the Case description with "CaseNumber's status has changed from PreviousStatus to Closed". Since the value will be different for each record, you have to use a Loop.

First of all, get the open Case records related to the Account.

Get Case Records

Add a Loop element to loop through those Case records and add an Assignment element to assign values to a record variable.

Since you want to update existing records, make sure that you assign the Id as well, so that the system can match the records. Pay attention to the Description field. You can create a formula with the value (sentence) that you want to update. Or, as you can see, you can directly type it in the Assignment element.

Here is the value that you can enter:

{!Loop.CaseNumber}'s status has changed from {!Loop.Status} to Closed.

Assignment Element to Update Records

Here is what happens in this Assignment element.

Assignment Element's Debug Log

Now you have a record collection that stores the new values for those Case records. Add an Update Records element to update them all at once.

Update Case Records
End of the Flow

3- Creating a Text Collection to Use in Other Elements

There are some elements that require you to provide a text collection. For instance, you have to provide a text collection of Recipient Ids for sending a custom notification. Another example is about using the In and Not In operators in Data elements; you have to prepare a text collection of record Ids. One more common use cases is sending an email to multiple email addresses. In that case, you have to prepare a text collection of email addresses.

In these situations, you have to use Loop and Assignment elements without any following DML actions.

For instance, let's say that you want to find the Account records that have open Case records. After getting the Case records, you have to loop through them and assign the AccountId to a text collection.

Assign Account Record Ids

At the end of the loop, you will have a text collection of Account record Ids. Then you can add a Get Records element to find those records.

Get Account Records

4- Other Use Cases

There are endless use cases for the Loop element in Salesforce Flow. Besides all these common uses cases, there can be some custom actions that return a collection. These custom actions can be invocable Apex classes or HTTP callouts from the flow. Therefore, it depends on the action itself.

For instance, here you can learn how to use HTTP Callout to integrate Salesforce with ChatGPT. According to ChatGPT's chat completions api, output contains an Apex-defined collection variable. Therefore, you have to use a Loop element to extract the output (ChatGPT's answer for your question). As you can see, Assignment element within the Loop has Apex-defined variables.

Assignment Element for ChatGPT Integration

Summary

The Loop element iterates through items within a collection variable, allowing you to execute actions for each item in the collection. It is one of the main elements in Salesforce Flow and therefore it is crucial to know how and when to use it. While there are numerous use cases for this element, there is one main rule that you must remember. Never use the pink elements within a loop!

1 Comment

  1. Hi Yumi - I have a question regarding the use case about Updating Records AND we want to make different updates. I built a flow and in my assignment element, instead of using a Record Variable created from scratch to set the values to the fields, I use the "Current Item from Loop ..." which is also a Record (Single) Variable.
    Then I create a Record Collection Variable to add the current record to the Collection and do the update after I have exited the Loop. The update is working well.
    My question: is it necessary to create a New Record (Single) Variable? Maybe I am missing something.
    Thank you
    Pierre

2 Trackbacks / Pingbacks

  1. How to Use the Transform Element in Flow - Salesforce Time
  2. Using Data Table in Screen Flow to Display Non-Object Data - Salesforce Time

Leave a Reply

Your email address will not be published.


*