Skip to main content

Know When to Loop

Learning Objectives

After completing this unit, you’ll be able to:

  • Understand how Flow Builder’s Loop element works.
  • Know when to use a Transform element vs a Loop element vs a manual loop.
Note

This badge is one stop along the way to Flow Builder proficiency. From start to finish, the Build Flows with Flow Builder trail guides you through learning all about Flow Builder. Follow this recommended sequence of badges to build strong process automation skills and become a Flow Builder expert.

This badge builds upon important concepts talked about in Multirecord Elements and Transforms in Flows. If you haven’t yet completed that badge, do so now.

Get In the Loop About Loops

The Transform element is great for processing collections of data, but it has its limits. It can’t run a collection through other elements; you can’t configure a Transform element to take actions or make decisions. It can’t traverse objects in the source data (for example, looking up each record’s owner’s manager’s email address). Also, there are some screen components, such as repeaters and choice sets, that aren’t compatible with the Transform element. Thankfully, you can use the Loop element to cycle through a series of elements, processing a collection one data point at a time.

The Loop element creates a circular path in your flow, and has only one configuration setting: a collection variable. The Loop element runs each value in the collection variable through the elements on its path, in order.

Think of a collection variable like a giant bin full of highly popular, highly collectible, and somewhat ugly blind-box dolls, ready to be boxed up at the factory and shipped out to stores. Each doll has to be put in a bag, the bag has to be sealed, then the bag has to be put inside a box, and the box has to be sealed before being put into a case for shipping. All of these actions have to be repeated in order, again and again, once for each doll in the bin.

The Loop element is like a claw that grabs all of the dolls, one at a time, and places each one on the conveyor belt. The conveyor makes sure each doll goes through every bagging, boxing, and sealing step, just like the loop’s path passes through flow elements such as actions and assignments. Unlike a factory conveyor belt, however, a flow’s loop path only processes one thing at a time. So when the loop path finishes processing the first item in the collection, the Loop element pulls out the next item and puts it through the same process. This repeats over and over until every item in the collection has been processed.

Let’s take a slow walk through an example of an actual loop. Pyroclastic Inc.’s org has a flow that locks a collection of opportunity records and posts to those opportunities’ Chatter feeds.

Loop diagram corresponding to the numbered descriptions that follow.

  1. The flow reaches the Loop element, which has the opportunity collection variable assigned.
  2. The loop temporarily stores the collection’s first opportunity in a special variable called Current Item from Loop.
  3. The flow runs the next element in the For Each path: Lock Opportunity.
  4. The flow runs the first element in the For Each path: Opp Chatter Post.
  5. Continuing along the For Each path, the flow finds no more elements to run, so it returns to the Loop element.
  6. The loop replaces the contents of the Current Item from Loop variable with the next opportunity from the collection variable.
  7. The flow repeats steps 3–6 until it finishes running the collection variable’s last opportunity through the For Each path.
  8. The flow proceeds along the After Last path.

When the flow runs, it processes the first record by running it through those two Action elements. When those elements are done, it goes back to the start of the loop and processes the next record. It keeps doing this, one record at a time, until it has processed all of the records in the collection.

Note

Loops can also process collection variables that contain a list of individual values, but it’s more common to loop through a collection of records.

Check out this video for another metaphor explaining how loops work.

Loops Without a Loop Element

You might be asking, “Why should I use a Loop element? Can’t I just use the Connect to Element function to manually connect elements to form a circle?” Manual loops are riskier than Loop elements for two reasons.

  • To exit a manual loop, a flow needs a Decision element. Users and live data sometimes create unexpected scenarios that result in the flow never taking the decision path that exits the loop, causing an infinite loop.
  • Manual loops don’t have any of the optimization built into the Loop and Transform elements, resulting in a longer process with a higher chance of exceeding the CPU time limit.

However, manual loops have one major advantage: They don’t require a collection variable to iterate through. If you need to loop through data that’s not in a collection, such as data from a multi-select picklist, then a manual loop is feasible. However, in many cases it’s better to get that data in a collection, so you can avoid a manual loop.

The general rule of thumb is: If there’s something you can accomplish using either the Transform element or the Loop element, use the Transform element. Use loops only when you can’t use transforms. Manual loops should be used only when loops and transforms aren’t possible, or when avoiding a manual loop would make the flow much more complex.

Function

Priority

Advantages

Limitations

Transform element

Highest

  • Fastest option
  • Far less likely to hit query and CPU limits
  • Works only with collections
  • Can’t include other elements
  • Can’t traverse objects in source data
  • Incompatible with repeaters
  • Incompatible with screen components that use choice sets

Loop element

When Transform isn’t possible

  • Can loop through decisions, actions, and other elements that can’t handle collections
  • Compatible with all elements and screen components
  • Works only with collections
  • Slower than a transform, especially when working with a large collection

Manual loop

Only when necessary

  • Can loop through elements without iterating through a collection
  • Compatible with all elements and screen components
  • Most likely to hit CPU limit
  • Easier to accidentally trigger infinite loops

Avoid Nested Loops

Speaking of the CPU time limit, there’s one more loop practice to handle with care: Avoid putting a loop inside a loop. This practice is sometimes referred to as nested loops.

A loop element nested inside another loop element.

If an element is inside a loop that’s within another loop, the number of times that element runs rises sharply. For example, if the outer loop iterates through 5 records, and the inner loop runs 5 more times, that element runs 25 times. However, many loops need to process collections that are much larger than just 5 records. In that same example, 500 records means that the element runs 2,500 times. Most loops contain more than one element, which multiplies the final number of elements that run.

As you can see, the number of elements run by a flow can rise quite sharply as a flow’s complexity increases. When the flow runs, all of this element repetition takes up a lot more CPU time, increasing the risk of hitting the flow’s CPU limit. Therefore, it’s best practice to avoid nesting loops unless you’re certain the flow will loop through only a small number of records.

Note

The Transform element is much more optimized than a loop, so you don’t have to avoid using a transform inside a loop.

Resources

Comparta sus comentarios sobre Trailhead en la Ayuda de Salesforce.

Nos encantaría conocer su experiencia con Trailhead. Ahora puede acceder al nuevo formulario de comentarios cuando quiera desde el sitio de la Ayuda de Salesforce.

Más información Continuar para compartir comentarios