How to use a Loop

The Loop element gives you the ability to choose the SObject Collection we want to pass through a series of elements.  You have the ability to choose how you loop the collection of records, either in Ascending or Descending order.  Also, you get to select a SObject Variable to be referenced in your Loop.  This is referred to as a Loop Variable.  

It is important to note that the Loop Variable  and the Collection referenced in your Loop must have the same object type.  You can’t have your Collection containing Account records and a Loop Variable being of the type Contact.

One last warning!  If you plan on doing a Record Create/Lookup/Update in a Loop, be careful – you could potentially hit some limits if you have too many records in your Loop!!

Where is the Loop element?  You can find the Loop element under the LOGIC section of the Palette.

Finding the Loop

Now that we have dragged the Loop element onto our canvas, we need to select what Collection Variable we want to pass through the Loop.  You have two options here.

Loop Assignment Options part 1

After you select your Collection Variable for the Loop, we want to determine the order we pass the records through.  This feature has the ability to have a big impact on the way your Flow operates, but I find in the majority of my Flows that use Loops are not affected by the order of the Collection Variable records.

Loop Assignment Options part 2

Now you just need to select the Loop Variable that you want to reference in your Flow.

Loop Completed

Ready for the fun part?  Lets determine what we want to go on ‘inside’ of our Loop.  This is where a Loop can get complicated when you first start out.  So, lets take a step back to clarify what we mean.  We do not actually mean ‘inside’ of the Loop element, but the series of elements that are connected to the Loop.

When routing your Loop, you have two options “for each value in the collection” (or “Next element”) and “when there are no more values to process” (or “End of loop”).  If you have routed a Decision before, it is similar in the sense that you have more than one location you can send your element to.  But unlike a decision, you are limited to two options.  That is why you often will see a Decision being the first element after a Loop.

Loop Routing

For the first example, we want to do a mass Opportunity Owner update of our large deal owners.  We create a Decision Element to sort through the Amount and determine who we will assign as the Owner of that Opportunity.

Full Loop

For the second example, we want to add the Opportunity Amount of all the Opportunities associated to an Account.  For this we will only need to setup one Assignment element and that element gets sent right back to the Loop.  Inside the Assignment element we are adding the Opportunity Amount to the Accounts’ Amount field for each record that passes through.

Loop Number2 Completed

RECAP: Loop elements are your  starting points for a “SOQL Query” or Fast Create in a Flow.  Anytime you want to examine multiple records through a series of elements, you should use a Loop.

7 thoughts on “How to use a Loop

  1. Matt August 23, 2016 / 9:19 am

    Hi David,

    Is it possible to use the loop element to extract IDs of the collection variable?

    To illustrate my case: I want a user to click on a button on account’s object. They would then be able to see a related list of opportunities in a multi-select pick list. The values selected in the multi-select picklist would form the SObject collection variable and then I would like to somehow extract the IDs of that collection variable and create a single record by populating up to 6 lookup fileds with these IDs.

    The purpose of this is to be able to create an email merge document that needs to contain data from more than one opportunity.

    Do you have any idea on how this could be achieved?

    Regards,

    Matt

    Like

  2. Priya June 5, 2017 / 10:35 pm

    Can you please send me the assignment login for second example.

    Like

  3. Priya June 5, 2017 / 10:37 pm

    Can you please post the assignment logic that you applied for second example. I have same exact requirement.

    Like

    • David Litton June 6, 2017 / 1:34 pm

      Priya, you’d create a variable called OpportunityAmount, and simply do a:

      OpportunityAmount — operatore = ADD — [Amount Field in your Loop]

      It keeps iterating the OpportunityAmount variable, and then you reference that later in your record update. Let me know if you need more help than that.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s