Jennifer Lee and Jessie Rymph in a new "Automate This!" yesterday.

Automate This! — How to Use Loops in Flow

By

Welcome to another “Automate This!” In this live-streamed video series, we cover all things automation, from use cases and best practices to showcasing solutions built by #AwesomeAdmin Trailblazers like you. With automation, you can remove manual tasks, drive efficiency, and eliminate friction and redundancy. In this episode, let’s see how Salesforce.org Senior Solution Developer Jessie Rymph uses loops in Flow.


Salesforce Admins and Developers use flow loops to create, update, and delete a whole bunch of records at once. They can use these loops in screen flows, record-triggered flows, autolaunched flows, and more.

Some examples of using flow loops to update records include:

  • A record-triggered flow that fires when a user is deactivated and reassigns ownership of all their records
  • A screen flow that asks for an email address, finds all contacts with that email address, and checks the “Email Opt Out” box on each contact
  • An autolaunched flow run just once by the admin to change the Region field on western states when two regions are combined

Every flow loop that updates records uses the same five elements in the same order.

Five elements of flow loops

Update Records

  1. Get Records: Get the records you want to loop through. They are automatically added to a collection, Collection 1.
  2. Loop: Tell the flow to loop through Collection 1, one record at a time.
  3. Assignment 1: Draft changes to fields on the first record.
  4. Assignment 2: Add the record currently in the loop to a second collection, Collection 2. (Start loop again.)
  5. Update records: Finalize update to database.

The five elements of flow loops.

In the rest of this post, we’ll go over the five flow loop elements in more detail and, in parallel, I’ll tell you a story about two 12-year-old girls named Data and Flow. Their story reiterates key concepts about flow loops. At the end, play a matching game to test your comprehension.

My goal is to help you understand and remember these key concepts. For a step-by-step tutorial on how to build this flow, please watch the video above for an in-depth conversation between me and Admin Evangelist Jennifer Lee. You can download the flow now from the AppExchange and walk through it yourself as you read.

Let’s get started

Flow use case: A button on an account record that quickly closes all open opportunities at once.

Meet Data and Flow!

Two school-aged girls smiling.

At the beginning of 7th grade, Data wrote down the names of all the friends she planned to have that year. Now that it’s almost time for 8th grade, she wants her friend Flow to help her update her list. She tells Flow to be brutally honest—if the friendship didn’t happen, say so.

[This will make more sense soon. Just stick with me, and look for key concepts in bold.]

How to pass info to the flow

When the user launches a flow from a button on a record, the flow receives the ID of the current record. It’s passed to the flow inside a variable called recordId. This is a text variable that can only hold letters, numbers, and symbols, and it has been marked inside the flow as “available for input”, meaning it can receive information from outside the flow.

In English class, Data looks to see if Flow looks willing to receive her note, and she passes her a piece of paper so small that it can only hold a bit of writing.

[Do you get it? The text variable is the piece of paper and Flow is “available to input” or willing to receive the note.]

Flow element 1: Get Records

The five elements of flow loops with the Get Records element highlighted as #1.

The flow starts out receiving this 18-digit ID and that’s it. It doesn’t have any other data.

Let’s talk through adding elements to the flow so it can do something with that ID. The first element to add to the canvas is Get Records so that the flow can look up opportunities attached to the account. You give it specific criteria: Find opportunities where accountID = recordId (that was passed into the flow) AND IsClosed = False.

This will find you all the opportunities on this account that are still open.

Flow doesn’t know what to do with the little piece of paper she has received. She knows Data wanted to update her list of friends, but she can’t do anything with this. She decides to ask for more information and sends back a note that says, “Give me your list of wannabe friends for the 7th grade.”

When using Get Records, you can specify that the flow retrieves multiple records at a time from the database. It will automatically store all those records in a record collection variable, a container specifically used to hold the values of all the fields of all the records brought into the flow.

Data makes sure the teacher isn’t looking and hands a little notebook to Flow in which she wrote about all the friendships she hoped to make that year. On each page, Data wrote everything she knew about one potential friend.

Flow element 2: Loop

The five elements of flow loops with the Loop element highlighted as #2.

Next, add a Loop element to your canvas and tell it to go through each record in the collection you just made, one at a time.

Connect Get Records to the Loop.

Flow is ready to look at the list, but she wants to see each name one at a time. She rips out the first page on which Data had written about George and the one time they made eye contact. Across the bottom, it says in curly handwriting, “definitely maybe”.

Flow element 3: Assignment 1

The five elements of flow loops with first Assignment element highlighted as #3.

Add an Assignment element to your canvas after the Loop. Here, you tell the flow that you want to change the opportunity stage to Closed Lost. You aren’t actually making the change yet, you’re just giving the flow a heads up about your plans. The Assignment says take the item currently in the loop (using the loop variable) and plan to change the status to Closed Lost. The flow will actually update the records all at once AFTER the loop. In Flow, it’s important to limit the number of times data is changed because of governor limits.

Connect Assignment 1 to the Loop.

Flow takes the first page and, using her pencil, erases the second part of “definitely maybe” and changes it to “definitely not”. She isn’t ready to hand this back to Data just yet. It’s risky to pass notes back and forth! She should make all her updates THEN send them back together.

Flow element 4: Assignment 2

The five elements of flow loops with second Assignment element highlighted as #4.

Add a second Assignment element after the first one. Now that the flow has updated this first opportunity, where should it go? You need to add it to a second record collection variable. Inside your Assignment, you create a resource that is a variable that can hold multiple records. The Assignment element is used to add the item currently in the loop to this new collection. We use the collection to update all the records at once in the next step. I’ll explain why.

Connect Assignment 2 back to the Loop element. This tells the flow to restart the loop with the next record after these two Assignments.

Flow is holding the piece of paper about George and isn’t sure where to put it. She can’t put it back in the book. She grabs an empty folder from her backpack and sticks the piece of paper in there.

Flow element 5: Update Records

The five elements of flow loops with Update Records element highlighted as #5.

After the loop has gone through all records in the first collection, one at a time, it has a new collection of records ready to be updated. Drag an Update Records element onto the canvas and tell it to update that second collection. You don’t need to specify which changes to make; you’ve already told the flow what updates you want to make back in Assignment 1. The Update Records element will make all the updates at once, saving valuable shared resources and respecting governor limits. Up until this point, the changes were just drafts inside the flow and were not reflected in your actual data.

Flow rips out seven pages of friendships that didn’t materialize for Data and changes them. She puts them in the folder and passes it over to Data, who finalizes all those changes by summarizing them in pen at the end of her notebook.

Now, it’s time to test your comprehension.

Next steps

Watch the video conversation between me and Jennifer Lee in which I walk through how to build this flow out in depth, without all the 12-year-old girl stuff.

You’ll also learn how to create a list of all the open opportunities to show the sales rep before they close them out.

You can also install this flow now from the AppExchange and learn by playing around with it.

Resources

Want to see more good stuff? Subscribe to our channel!

SUBSCRIBE TODAY
Improve sales funnel velocity with Flow

Improve Sales Funnel Velocity Using Flow | Automate This!

Welcome to another “Automate This!” In this live-streamed video series, we cover all things automation, from use cases and best practices to showcasing solutions built by Awesome Admin Trailblazers like you. With automation, you can remove manual tasks, drive efficiency, and eliminate friction and redundancy. In this episode, learn how Becka Dente leverages a simple […]

READ MORE
Send a dynamic email to user-defined contacts with flow

Send a Dynamic Email to User-Defined Contacts Using Flow | Automate This!

Welcome to another “Automate This!” In this live-streamed video series, we cover all things automation, from use cases and best practices to showcasing solutions built by Awesome Admin Trailblazers like you. With automation, you can remove manual tasks, drive efficiency, and eliminate friction and redundancy. In this episode, learn how Michael Barnes uses Flow to […]

READ MORE
Leverage Prompt Builder and Flow Builder to automate data creation

Leverage Prompt Builder and Flow Builder to Automate Data Creation | Automate This!

Welcome to another “Automate This!” In this live-streamed video series, we cover all things automation, from use cases and best practices to showcasing solutions built by Awesome Admin Trailblazers like you. With automation, you can remove manual tasks, drive efficiency, and eliminate friction and redundancy. In this episode, learn how Erick Mahle leverages Prompt Builder […]

READ MORE