top of page

Recreating a "While Loop" in Flow

Aug 1, 2024

5 min read

1

6

0

While Salesforce Flow offers powerful automation capabilities, it notably lacks the direct equivalent of a traditional while loop found in Apex. This limitation can pose challenges when trying to replicate complex iterative logic within the platform. In this post, we'll dive into the intricacies of constructing a makeshift while loop using Flow's building blocks, exploring potential workarounds, and discussing best practices to overcome this hurdle.


If you are unfamiliar with what a "while loop" is, here is a brief definition:


A while loop is a programming construct that repeatedly executes a block of code as long as a specified condition remains true.   


How it works:

  1. Condition Check: The loop starts by evaluating the condition.

  2. Code Execution: If the condition is true, the code inside the loop is executed.

  3. Repeat: The process returns to step 1.


When to use a while loop:

  • Unknown number of iterations: When you don't know beforehand how many times the code needs to run.

  • Looping until a specific condition is met: When you want to continue the loop until a certain condition is evaluated as true.


So, while Salesforce has Loops, your only out of the box behaviors are to iterate through the loop until you reach the end. And while you can do a lot of decision making WITHIN a loop, it can be difficult to manage additional criteria to stay in the loop while specific conditions are being met.


Let's solve this using the following (fun) scenario:


I am a very gracious farmer, and I announce that I am going to give away all of my bananas at the farmer's market. I let everyone in line know that I have 50 bananas, and I will fill up everyone's bag until it is full.


The complexity in my give away is that everyone has a different sized bag, so I need to do some basic calculating as each person steps up in line to determine how many bananas I can give them, and how many of my 50 bananas I have remaining.


To be extra careful about overfilling a bag (we wouldn't want the bag to rip!), I will put 1 banana in the bag at a time, until the bag is considered full.


As each person steps up, I look at their bag and know how many bananas I can fit in it. Some may fit 6 bananas, some may only fit 3.


Now, the conditions of exiting my loop isn't the number of iterations to go through, but an external factor, which is how many bananas I have on hand, not the number of people in line. Some people may miss out on free bananas!


This is where a while loop concept may be useful. I want to keep filling bags until it is full.


The Challenge


I know I can use loops in Flow, and I can easily iterate through each person in line. But that isn't enough. For each person in line, I need to know how many times to repeat an action until requirements are fulfilled.


In Apex, I could write a while loop that says iterate over a given person's bag until the bag is full or I run out of bananas. There is not a straightforward way of doing this in flow. There are no decision making elements within a Loop element.


In the following section, we'll explore how to construct a makeshift while loop using a combination of Flow elements to overcome these limitations.


The Solution


I am using the following concepts in my flow/loop to get the job done:


  • Using number variables as a method of counting and establishing quantity. This will help me understand how many times I can put a banana in someone's bag, and exit the loop if I run out of bananas

  • Decisions & Element Connections will help me re-route intelligently within my loop.

  • A combination of these two concepts will help me define a while loop in flow.


Let's take a look at the flow, and then break it down:



  1. Variable Setup

    1. I establish a number variable to store the amount of bananas I have. Let's say 50.

      1. var_TotalBananas

    2. I establish another number variable to store the current person in line's bag capacity. This will be updated every time we iterate over a person in line.

      1. var_CurrentBagCapacity

  2. Person In Line - My loop is a collection of the people in line. Each person may have a different sized bag, and can hold a different amount of bananas. Loop behavior goes through one iteration at a time.

  3. Do I Still Have Bananas To Give Away? - I check first that var_TotalBananas > 0. If my counter is 0, I exit the loop immediately using an element connector. If I still have bananas to give away, I continue.

  4. Establish How Many Bananas Can Fit In The Bag - I assign the var_CurrentBagCapacity variable a value equal to the bag capacity of my current person's bag

  5. Log Banana Giveaway Transaction - I create a version of a record variable var_BananaTransaction

  6. Add Transaction Log to Collection - I add my variable record to a collection so I can create them in bulk later

  7. Remove Space in Bag, And From Overall Quantity: I said initially that my process is to give away 1 banana at a time, so I subtract 1 from both of my variables:

    1. I remove 1 from var_TotalBananas, to keep track of how many I have left

    2. I remove 1 from var_CurrentBagCapacity, because I have added a banana to someone's bag and I need to track how many more I can add to the bag, if any

  8. Have I Filled The Bag? - If both variables are 1 or greater, it means A) I have more space in the current person's bag still, and B) I have more bananas to give away.

    1. This decision and element connection is the key piece of forcing the loop to stay in the current iteration WHILE the bag has capacity and I have bananas.

    2. If I still have space and bananas, I force the flow to go back to the Log Banana Giveaway Transaction, so it can continue logging each time I give someone a banana, and recalculate against both of my variables.

    3. If either A) The bag has been completely filled, or B) I run out of bananas...

  9. Restart Bag Space Counter - This resets the var_CurrentBagCapacity variable, so that when we get to the next person in line, I can accurately set the variable again and start anew.

    1. In the event that in the next iteration the flow realizes there are no more bananas left, it will exit the loop immediately through the decision we walked through in step 3.

  10. Create Banana Transaction Logs - Once I have either given away all of my bananas, or serviced each person in line, I can complete my process and commit all of my banana transaction logs to the database.


In Summary


We used decisions, and element connectors to enforce the continuation of the while loop, and we used number variables to track the criteria in which to continue the while loop.


Considerations


While Salesforce has removed the iteration count in flow, be mindful of data scale. While flow may let you iterate as many times as you want, you must still adhere to Salesforce limits and can still get a CPU timeout error.


Also, while Flow should always be your first choice for automation, if you are finding requirements too complex and flow too clunky, apex is always a reliable approach when low-code fails or becomes inefficient.

Aug 1, 2024

5 min read

1

6

0

Comments

Share Your ThoughtsBe the first to write a comment.