Salesforce Flow Loops. Perhaps the Flow Element that freaks people out the most. Fundamentally Flow is going to break Flow Loops down in an easy-to-understand way. After reading this post and watching it's corresponding YouTube video, you'll be ready take you Flows to the next level. Let's do this!
Let's start by getting some records. If we're going to loop through records, we first have to get some records to loop through. Review the last blog post on Working with Data, if you need some help with getting records.
In that post, we talked about Collections as simply being a single variable that holds more than one record. Loops only work with Collections, so it's an important concept to understand.
Using a Get Element, query some records. In our use case, we're going to query all our Account records. Now, please don't query ALL Accounts in your org unless you know you have a small number of records like I do in my demo org.
We have a CSAT Rating field on our Account record. Our flow will be counting the number of Satisified, Neutral, Unsatisfied, and unrated records. In a later lesson, we'll display and act on those results but for this lesson, we're focusing on looping through the records in order to summarize the CSAT scores.
** As a reminder, my use cases are designed to help teach concepts and are not necessarily practical for use in your Salesforce production org.
The Loop element is actually pretty simple to create; it has a Label, the name of the Collection, and an option to define the order for which we step through the records.
The Collection Variable is the "label" from the Get element created above.
The only thing we can sometimes stumble on here is, "why isn't my Collection Variable showing up?" There is really only one reason why the variable might not show; we didn't create it as a Collection. If this happens to you, go back and make sure the Get element's "How Many Records to Store" is set to "All Records".
A loop works through the Collection of records, one record at a time. When considering the "Direction for Iterating Over Collection", think about the order of how you want each record in the Collection to be processed. Do you want to start with the first record then move to the last or the other way around? Most of the time, I simply leave the default of "First item to last item".
Are you familiar with the card game, SlapJack? With SlapJack the cards are divided up among each player and stacked face-down. The top card in your deck is then placed face-up in the center of the table. If it's a Jack, then the first person to slap the center pile gets all the cards from in pile. We don't know what card will come up next but play with the single card we pull from the deck.
Loops are similar to SlapJack. We have our collection of records (cards) and we evaluate (play) with the single record at a time. The only record (card) that matters is the one we're working with.
While inside the loop circle, the Flow will automatically call that current record (card), "Current Item from Loop". It becomes a record variable that you can evaluate like any other record variable. You're able to change field values on that record. You can use the record to make decisions. You can use it to create other records, etc.
The important thing to remember is "Current Item from Loop" is the current record and that's the only record that matters. Don't worry about any of the other records that might be in the Collection. They aren't in play, yet.
When all the elements within the Loop circle are complete, the "Current Item from Loop" record (card) is discarded and the next record (card) becomes the "Current Item from Loop". The new "Current Item from Loop" is processed through the Loop circle and so on until all the records have been processed.
When there are no more records in the Collection, the Flow will exit the Loop and continue through the rest of the Flow.
Loops are powerful and are a critical element of Flows. They give us the ability to quickly process multiple records and do so many cool things. Once you master this element, you will be well on your way to becoming a Flow Master.
Be sure to watch the video presentation on YouTube. Your Likes, Subscribes, Follows, and Shares help me tailor my content around the items you find most helpful.