By default, the iterations in a For Each loop run simultaneously in parallel. However, when dealing with nested loops or when there are variables within the loops that require predictable results, it is important to execute those loops one at a time, or sequentially.
Creating a Logic App workflow with a “For Each” loop that processes items sequentially involves a few steps. Below is a guide to help you set this up in Azure Logic Apps:
Steps to Create a Logic App with a Sequential "For Each" Loop
Create a Logic App:
Go to the Azure portal.
Click on “Create a resource” and search for “Logic App”.
Follow the prompts to create a new Logic App.
Add a Trigger:
Once your Logic App is created, open it in the Logic App Designer.
Choose a trigger to start your workflow. For example, you can use a “Recurrence” trigger to run the workflow at a specific interval, or an “HTTP Request” trigger to start the workflow when a request is received.
Add an Action to Get the Data:
Add an action to retrieve the data that you want to process in the “For Each” loop. For example, you can use the “Get rows” action from the Sheets connector to get a list of items from a Google sheet.
Add a “For Each” Loop:
After the action that retrieves the data, add a “For Each” loop.
In the “For Each” loop, set the “Select an output from previous steps” field to the output of the action that retrieves the data (e.g., the body of the “Get rows” action).
Set the “For Each” Loop to Run Sequentially:
By default, the “For Each” loop runs in parallel. To make it run sequentially, click on the “Settings” icon (gear icon) on the “For Each” loop.
Set the “Concurrency Control” toggle to “On”.
Set the “Degree of Parallelism” to 1.
Add Actions Inside the “For Each” Loop:
Inside the “For Each” loop, add the actions that you want to perform on each item. For example, you can add a “Send an email” action to send an email for each item in the list.
Save and Test Your Logic App:
Save your Logic App.
Trigger the Logic App (e.g., by waiting for the recurrence interval or sending an HTTP request) and observe the workflow processing each item sequentially.
Here's how Logic App should look:
Example Workflow
Trigger: Recurrence
Action: Get rows from a Google sheet
For Each:
Settings: Concurrency Control On, Degree of Parallelism = 1
Actions: Send an email
Conclusion
This setup ensures that each item in your list is processed one at a time, in sequence. You can customize the actions inside the “For Each” loop to suit your specific needs.