RSS

Flow Loop Doesn’t Start at First Record

02 Dec
Flow Loop Doesn’t Start at First Record

Here is a problem that drove me crazy to figure out: In certain circumstances a Flow loop does not start at the first record.

It happens when a loop is encountered multiple times. Each time the loop is encountered it begins parsing through the collection at the record just past the last one accessed.

For example, if the first three records have been accessed on the first time through the loop, then the next time the loop is entered it starts with the fourth record of the collection.

This only happens when you have a loop inside of a loop and you exit the inner loop as soon as a decision evaluates to true (which may be before all the records of the inner loop have been parsed).

Here is an example. This is an inner loop which goes through a set of “form” records, and as soon as the right type of form is found the loop is exited:

Loop That Doesn't Reset

Inner Loop That Doesn’t Reset

.

There are five records in the collection. The first record does not have an Id in the field that is being evaluated, and the second through fifth records have these Ids:

ID List

ID List

.

The first time the inner loop is encountered there are three records that are parsed before a match is found–the record with the missing Id, then two more records:

.

The second time the loop is encountered the NEXT record (the fourth record) in the collection is the first record that is accessed:

Loop Result 2-1

.

Since the flow exits the loop once the last record is encountered, the first three records are never parsed, and, in this example, a match is not found.

Ugh.


The Solution

The only solution, as near as I can tell, is to make sure that every record is parsed every time the inner loop is encountered.

To make this happen in my example I’ve created a “flag” variable that is set to True if a match is found, and other variables to store the values that are needed at the end of the loop. I make sure the flag is set to False prior to entering the loop, and check to see if it is true at the end of the loop.

Modified Loop

Modified Loop


Efficiency versus Accuracy

I’m a big fan of efficiency, so this solution doesn’t sit well with me, but I’m a bigger fan of accuracy. It appears that the only way to be sure that your Flow is accurate when you have a loop inside of a loop is to be sure that the inner loop go through every record every time.

On the other hand, if you’re going through a collection in an outer loop looking for a particular record, it is totally okay to exit the loop as soon as your criteria is met.

Cheers!

Advertisements
 
1 Comment

Posted by on December 2, 2015 in Flow Tips & Tricks, Visual Flow

 

Tags: ,

One response to “Flow Loop Doesn’t Start at First Record

  1. thinkdata

    March 30, 2016 at 7:14 pm

    this drives me crazy – in a new twist, I am adding to a collection within the first loop that is used as the basis for the inner one, and it won’t reset the start sequence in the inner loop.
    (I am searching the collection to make sure an entry is not there before adding to it).
    argh.

    Liked by 1 person

     

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

 
%d bloggers like this: