Nested Loop Logic

Found an issue with nesting loops in the automation.

I have a loop that for this test returns two records (call 1 and 2). then within that loop another loop that returns two other records (A and B)

I would expect the loop to loop through 1 and within that A and B, then 2 and within A and B
so the expectation would be like so
1A, 1B, 2A, 2B.

Instead it appears to do this
1A, 2B, 1A, 2B

you can see this in the example below. it is first looping through employees then the tasks

========================
Loop #10.L.2 iteration 1 of 2

EMPLOYEEID:
[30316]

PROJECTID:
[00000523]

TASKID:
[Scope-00000001]

========================
Loop #10.L.2 iteration 2 of 2

EMPLOYEEID:
[304146]

PROJECTID:
[00000523]

TASKID:
[Scope-00000002]

========================
Loop #10.L.2 iteration 1 of 2

EMPLOYEEID:
[30316]

PROJECTID:
[00000523]

TASKID:
[Scope-00000001]

========================
Loop #10.L.2 iteration 2 of 2

EMPLOYEEID:
[304146]

PROJECTID:
[00000523]

TASKID:
[Scope-00000002]

1 Like

The behavior depends on how fields are mapped. What is the automation number so I can take a look?

it is a48762a872a0441b8592152bbf2767ca

In my head it would be dependent on the loop and not the fields.
i would think it would do loop 1 and within that loop A and B. then loop 2, and loop A and B. so the final result is 1A, 1B, 2A, 2B

1 Like

Your conceptual intuition is a reasonable assumption based upon programming experience, but unfortunately not how nested loops actually work in the system.

The problem is with the field mappings within this action:

Assigning an action to a loop does not confer any scope. All that happens is that the Loop examines the output of the referenced action to determine how many iterations to perform, by finding the output field with the largest array of values.

So the nested loop works in the same way. It looks at the mapped action above it to determine how many iterations to perform based on its output fields’ largest array.

The problem is with the inner action’s mapped fields. You are probably expecting mappings back to actions to somehow be scoped to the outer loop. That isn’t what happens. The inner loop controls the array index when fetching mapped array field values. So based on the largest array size from action #10.L.1, the system then examines the mapped fields within action #10.L.2.L.1 and sequentially maps array data based on the index counter of the inner loop iteration. It is not using the index of the outer loop when determining field mappings to actions scoped outside the outer loop.

So the approach is to fetch the needed fields within the outer loop and before the nested loop (e.g. maybe use Transform Text replace text actions that replace nothing…just as a way of grabbing the mapped array field values and emitting them). That gives you scope to array data you expect the outer loop to have. Then the inner loop’s action refers to those Transform Text output values. You would keep its mapping to data from #10.L.1 b/c that is the action’s output being looped upon.

Hope this makes sense. Let me know if you need further help with it.

1 Like