What you should know about Azure Logic apps For each loop

Oguzhan YIGIT
Published by Oguzhan YIGIT
Category : Azure / Logic Apps
23/03/2018

Azure Logic App’s for each statement

Since Azure Logic App’s for each action have been released it has changed the logic apps flow design, the integration possibilites and the time it takes to implement complex workflows. When you are about to use the for each statement within a logic app you should keep in mind the component behavior. In my experience, the important points of the for each statement are :

  • Iteration
  • parallelism
  • limitations

Iterating within your logic app

The iteration process is built on any array of object. So the logic app’s for each action input should be any array. Depending on your needs, it can be an array variable, the result of a sql query or stored procedure execution, the output of an xpath query, the result of any expression either any connector which gives you an array as ouptut. Simply put your expression in the input field of your For each loop.

The following screenshot iterates through the result of an xpath query.

Azure logic app's for each iteration

When it comes to manipulate iterating item, you should use the “item()” expression which gives you the content of the currently iterating item.

Azure logic app for each item

For each parallelism

When I first faced it, I thought that default behaviour was odd. Yes, by default, cycles of the for each loop run in parallel. Once known, its no more an issue. The number of parallel run can be changed using the logic app designer (either the code view). Simply use the “Settings” tab under the for each action, turn on “Override default” button and choose the behavior you need.

Azure logic app for each settings

If you ever need to get your loop sequential, just set the degree of parellism to 1.

For each parallelism consequences

Because the “for each” loop comes with its parallelism behaviour, each iteration will execute as a separate thread. So, you can’t terminate a logic app instance within a for each statement nor stop the process if one iteration fails. That’s true even if you have set your degree of parallelism to 1. Just prefer a “do until” acton if you absolutely need to stop your process if one iteration fails.

Avoid incrementing or assigning variables

Consider setting variables within a “for each loop” as a mistake. Because of parallelism, you can’t control when it each iteration starts or ends, so just avoid using them. For instance you should prefer a “do until statement” if you need to increment an integer within the loop

Limitations

You can reach this post https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-limits-and-config to get the full list of limitations. Shortly :

  • loop is limited to 100, 000 iterations
  • Parallelism is limited to 50. Default value is 20