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.
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.
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.
Now you just need to select the Loop Variable that you want to reference in your Flow.
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.
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.
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.
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.