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:
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:
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:
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.
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.
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.