In the Flow Builder, Loop is a step that iterates over all the objects of an incoming array of data. Loop helps perform a specific operation on all the objects of an array one by one.
In our Flows, many steps fetch records from apps, usually in the form of an array. The response that is received is usually paginated and contains multiple items. Even if there is a single object, you will receive it as a single element array. In order to iterate over a single item & perform any operation on it, we use loops.
A Loop should be added as a child to the Query step so that all the iterations brought as the response of query can be traversed by the loop.
In this example, we'll demonstrate Loops using the Flow, "For all the repositories created in Github, create a project in the task manager app." As a first step, we have added a query inside the flow.
After adding query to the Flow, click on the kebab menu and select the option, Add Child Step.
Search for Loop and select it from the menu.
Once you add the loop step, you will need to provide a variable name where the objects of the array will be stored.
Let's assume that our paginated query brought 10 repositories in 10 iterations. Each repository will become one object of the array and the loop will traverse through every single object of every iteration. We'll provide the variable name as "Github_Repositories." This variable will store each object of the array (fetched in the query step) that the Loop is iterating on and pass its value on to the next step (i.e., the Action step).
Next, you have to select the array that the Loop step will iterate over (shown above).
You can directly provide your array here or better yet, you can provide an output from any step (using tags) of the flow that has your required data in an array.
In this example, the array is being fetched by the query Get Repos, therefore, we'll select the query step (shown above). To select tags, click on the '+' sign in front of the input field area and select the array output tag.
To add the output array tag to the Array field, click on '+ Add to Field' button (shown above).
An important thing to note here is that you have to select the Out tag (which is the array tag) to populate all the attributes of the array object(s) (e.g., issues_url, deployments_url, etc.). So that, none of the attributes inside the array object(s) gets missed while passing it on to the Action step.
When you're done adding the Variable Name and the Array in the Loop step, click on 'Test' to test the loop. The loop will fetch the results, iteration-wise.
You can view the results by expanding the Iteration (shown above).
For the loop to traverse the next iteration of the query, click on 'Test Again.'
Similarly, you can keep on fetching the Next Page of the Query and testing your Loop for every iteration until all the pages are exhausted.
Click on 'Done' when you're done testing your Loop step and it'll get successfully added to your Flow.
Next, you can add an Action step to your Flow. This Action step has to be added as the Loop's Child Step. So that, when the Loop runs on the Query and saves the array object in a variable, it can perform the Action on the Action application for each iteration.
In our running example, we are creating a project in our task manager for every repository created in our Github account. Learn how to add that Action to your Flow builder's step by visiting this guide.
When the end-user sets up the above Flow, the integration will execute in the following manner:
- Query Step: First, you select your application from where you want to bulk import all the data. You'll add your Github account and the query will execute. The query will fetch all the repositories from your Github account in a page-wise manner.
- Loop Step: The loop step will iterate over the records fetched by the query and save the output object of the array in the "Get_Repositories" variable.
- Action Step: If the Action is to create a project in the task manager app, the data for the first output object will be sent to the loop's child step. This is the action that will create a project in the task manager app.
With this, the first iteration of the loop will be completed and the details of the first object (repository) will be sent to the task manager app. The loop will run again for the second repository and so on. This way, all the repositories fetched by one app (as a result of bulk import) will be reflected one by one in the other app through the usage of the loop.