Loop & iteration
Last updated
Last updated
In this video, we will walk through everything you need to know regarding loops and iterations in Easyflow.
Let’s first create a new flow with the title "Loop and Iteration". We'll start with a simple use case by reading a list of files from Google Drive and adding them to a Google sheet one record at a time.
First, drag and drop the Google Drive connector into the "workflow designer" canvas.
Then configure Google Drive by choosing and configuring the List files option from the operations panel. Also, don't forget to set this step as a starting point.
By clicking Preview Results, you will be able to see how the returned data from Google Drive looks.
As you can see, the list of files are returned in JSON format as an array of objects under the files key name.
The next step is to send the "Google Drive files list" to “Google Sheets” through a simple iteration. To achieve this goal, we need to add 2 steps: The Loop connector and the Google Sheets Connector.
Let's add the Google Sheets connector to the workflow designer canvas and configure it after selecting the "Add a row" option from its list of operations.
In the configuration panel select the connection, then fill out the Spreadsheet Id and the range. For now, leave the data text box as is until we configure the Loop connector.
Next, drag and place the built-in loop connector between the "Google Drive" and "Google Sheets" steps. The goal is to have the Loop connector iterate through all the retrieved "Google Drive" files and insert them one by one into "Google Sheets".
In the Loop connector, we must specify a value for the "Index Length" text box in order to determine how many times the Loop should iterate.
By concept, the loop should iterate several times, equal to the number of returned files. To do that:
Click on the "4 dots", then go to the "Array Function Expressions" tab and choose Length.
Go to the Mapping tab and locate the "List files" Google Drive group.
Choose the files key and place it inside the "Length" function expression. Save your changes.
Next, let's continue the configuration of the Add a Row Google Sheets connector by mapping the data text box with the "files list" coming from "Google Drive".
In the demo we will map 4 columns: Kind, id, name and mime Type.
The final step is to close the loop by dragging an arrow from the Google Sheets step back to the Loop step then press Save.
Let's test and validate the outcome of this automation by running our flow. As you can see, the data is coming to Google Sheets one record at a time.
Now, let's demonstrate an advanced use case. In the first example, we iterated an array of objects. In this case, we'll iterate an array of arrays and insert each record into Google Sheets.
The plan is to retrieve a "List of Products" from a Google Sheet, then insert the returned data into a different "Google Sheet".
In order to retrieve data from a Google Sheet you will need to first drag and configure a Google Sheet connector after choosing the “Get Range Values” option from the operations panel.
Remember to set this step as a starting point.
From the Preview Results panel, we can see that the data is returned in JSON format. The "List of Products" is returned as an array of arrays under the "Values" key.
Next, we will connect the "Get Range Values" Google Sheets step directly to another Google Sheets connector. As an experiment, we will demonstrate how we can send all the returned data from the first step as one attempt to Google Sheets without iteration.
Let's configure the second Google Sheets connector after choosing the "Add a Row" option from the operations panel.
In the data text box, we can notice that the "values" key requires mapping to an "array of arrays".
When comparing these requirements with the "values" key returned from the first step, we can see that the format matches.
Both of these values have the JSON format of "array of arrays". Accordingly, the mapping is quite simple. It can be done by following these steps:
Highlight the "array of arrays" JSON from the data text box then click on the "4 dots" icon.
Locate the "Get Range Values" from the Dynamic Content panel and choose "Values".
Click Apply to save your changes.
Also, we need to link the 2 steps together and run the flow to test the outcome.
As we can see from the results, all of the product data was successfully transferred to Google Sheets at one attempt.
There may be times when you need more control over the individual steps and logic within a loop or iteration. In these cases, Easyflow has a variety of methods to give you more control.
The next demonstration will show you how to handle an advanced and complex iteration by looping and picking up every record from "Get Range Values Google Sheets" by its index using variables and function expressions.
To make this demonstration clear and easy to explain, we will split this case into multiple stages.
First, we need to place a Loop connector in between the two "Google Sheets" steps and link all the steps together.
We will need to change the data mapping settings of the "Add a Row Google Sheets" step from mapping an entire array of arrays to mapping incoming data record by record.
In the data text box, we will use the GET function expressions to map the columns of Google Sheets with the files list coming from the "Get Range Values" first step. This can be done by:
Highlight the desired “Google Sheet” column from the data text box.
Click on the "4 dots" icon then on the "General functions" tab.
Select the GET function expression.
The GET function expression allows you to capture data from JSON. It required 2 inputs:
First: The source of data. In our case, we have to select the "Values" array of arrays from the "Get Range Values" step.
Second: The path to the data within the selected source. In our case, n.0 or n.1 where n represents the "index of the record" we are targeting and the number on the right represents the column number we are targeting.
In the first attempt, we will give n a value of 0 for demonstration purposes.
After saving the configuration and closing the loop, we can now run the flow and test the outcome.
As we can see, the same record is repeatedly inserted into google sheets because we fixed the index to 0.
We noticed no data went to "Google Sheets". It seems that our configuration is not set up correctly. Let's use this opportunity to learn how to debug and fix the problem.
In the Preview panel, we can see that the flow returns an "index out of range" message. This means the data in the "Add a Row" connectors are not mapped properly with the data from the first initial step.
When moving the cursor over the Values array key inside the data text box, we see that the data is mapped with the "Add a Row" step instead of "Get Range values".
After running the flow, we can see the same record is repeatedly inserted into google sheets as expected because we fixed the index to 0.
On the second try, we'll replace the fixed 0 index with an incremental index variable.
To achieve that, we will create a "Workflow" variable called var_index with default value set to 0. This variable will represent the index during the iteration. For example, if the process is in its 5th iteration, the var_index variable will have the value of 4 as it starts from 0.
To increment the var_index variable by 1 in each iteration, we have to use the Variable Setter built-in connector.
This can be done by dragging the Variable Setter block and linking it to the "Google Sheets - Add a Row" step.
From the Variable Setter step, map the var_index variable to the "key" textbox by clicking on the "4 dots" icon. Then locate the variable and click on its "Plus" sign. The var_index variable should be added to the text box in black.
From the Values text box, click on the "4 dots" and select the "var_index" variable. Then, click on the "Plus" sign and type So the syntax expression will be "var_index" plus 1. After having the index configured, we are ready to update the data mapping configuration of the “Google Sheets - Add a Row” step by replacing the fixed 0 index with the "var_index" variable.
Also, let’s map the remaining product fields by adding additional columns to the array of arrays inside the data text box.
After running the workflow again, we can see the records are successfully being inserted into Google Sheets.
Note that the var_index is a "Workflow" variable. This means its value will persist between runs. Therefore, we must give it an initial value of 0 before it enters the loop.
This can be accomplished by placing and configuring a Variable Setter before the Loop step.
Let's run the workflow again and we can see the records are successfully being inserted into Google Sheets as expected.
Thank you for watching this video.
Please feel free to contact us if you have any questions.