Salesforce Flow: Use Loop in a Screen Flow

Introduction to Loops

When building automations in Salesforce, administrators often need to work with multiple records. Many objects have one-to-many relationships, and that requires more complex solutions. The “Get Records” element, as useful as it is, may not be enough in some cases.That’s where loops come in.If you have any coding experience, you know how helpful loops are. They let you repeat a specific action a set number of times. In Salesforce, you can use loops when you need to traverse through a collection of records rather than checking a single record.There are a lot of use cases for loops. Most of them include some sort of automated updates and decision making. We’re going to build something different. We’re going to create a screen flow displaying accumulated information from multiple records.Let’s start.

Building the Flow

Business Case: A sales manager at our company wants to have a quick look into Opportunity products. The business requirement is to have a simple flow that enables a user to select an opportunity, and displays a list of related opportunity products with certain attributes, and an overall price of all products.Sounds simple enough. If we were looking into Opportunity fields, a simple Get Records element would do the job. But for this process, we are looking into child records of Opportunity - OpportunityLineItem (Opportunity Product). We don’t know how many child records could there be, so using multiple Get Records elements for each record is out of the question. Seems like a perfect use case for a loop!Let’s build this screen flow from scratch. Go to Flows, and create a new Screen Flow.We’ll start by adding a Screen to the flow.We’ll need a lookup component to enable users to select an opportunity. Let’s use the same object, we’ll be working with - OpportunityLineItem. Also, we need to make sure the user won’t go to the next step without choosing a record, so let’s make it a required field with a global constant.

API Name:
Field API Name: OpportunityId
Label: Select Opportunity
Object API Name: OpportunityLineItem
Required: {!$GlobalConstant.True}

After the user selects a record, we can use its id to look up the right opportunity product records. Use Get Records element to find all records with opportunityId equal to Id selected on the first screen. When you select “All records”, a collection variable will be created.

get opportunity product records
Get new records

What happens when there are no records? This is a very real possibility, so let’s prevent the flow from failing with a Decision element. If the collection variable auto-created in the Get Records element is not null, then we can continue on with the flow.

We should add a Screen to the path with no records, to inform the user that no records were found.

set up screen when there are no records

Now it’s time to use the key element in our flow. We need a Loop to iterate through each record. All you have to do to configure the Loop element is selecting the collection variable, which is the same variable that the Get Records element automatically created when we selected “All records”.

configure loop element in flow

Now comes the hard part. We have to create a process that will traverse through each record individually, get the data we need, and add it to a resource that we’ll later use to display the accumulated information. And we also need to calculate the total price.Let’s go step by step.Firstly, let’s create a Text Variable to hold the final output.

variable for final output

To access data from each record, Loop creates a special resource. It’s labeled as Current Item from [The Collection Variable’s Name]. We can use this resource to collect the quantity and sell price for each opportunity record.Create a new resource - text template. This type of resource allows us to create a sort of template. For each record in the loop it’s going to be populated with the current record’s data. We can also use plain text to make this information readable.

set up text template

Now, drag and drop the Assignment element.For each run in the loop we’ll take the information from our text template, and add it to the text variable we’ve created for the final output. This way, with each next iteration a new line will be added.

configure first assignment element

Make sure to select “For each record” when connecting the Assignment element to the Loop element.If we wanted to show just the accumulated info from all records, this would be all. But the business requirement in our case is a little bit more complicated. We still need to calculate the total price.Let’s add another Assignment element onto the canvas. This time, we’ll store the final output in a Currency Variable.

set up total sell price variable

Opportunity Product has a field labeled “total price”. By adding the total price of each of the products, we can easily calculate the total price. Because we are looking at one field here, we can just use a Formula resource to store total price from the current item from the collection variable.

Now, the assignment should be configured exactly the same as we did it with the previous one.

configure the second assignment element in the flow

All that’s left is to connect the elements, and create a final Screen. Use the Display Text component to display the values from the two variables we’ve used for the final output.

Configure the final screen

This screen flow will be used multiple times at once, so let’s get rid of the Final button, and let users go back.And that’s it. Run the Debug Tool and check for yourself if everything works correctly.

all elements in the screen flow

Looks like it does. Good job!


Out of many features Salesforce Flow offers, loop might be one of the most important among them. It becomes an incredibly useful tool when you need to create an automation looking into multiple objects, and even modifying them in a certain way.If you want to learn more about Salesforce Flow, make sure to check our blog!