How to use Loops
The 'Loop' action lets you execute an action or a set of actions repeatedly for a given number of time or until a condition is met.
This action provides two types of loop methods: ‘Times’ and ‘Each Item’.
The ‘Times’ loop type repeats a task (or multiple tasks) until a specific count is met. So, for instance, if you wish to create many identical notes in your Evernote account, use the ‘Times’ loop time in your workflow. It essentially says ‘do this x number of times’.
The ‘Each Item’ loop type repeats a task (or multiple tasks) for each item in an array (or a collection). You can use this type of loop, if, for instance, you wish to fetch only the value of the ‘name’ property from all the objects of a returned array. It says ‘do this for every item in a collection/array’.
How Loops Work
When you add the ‘Loop’ action on your canvas, it will look like a block that can be expanded or contracted as per your requirements.
What’s special about the ‘Loop’ action is that while it does what others do, you can also add multiple actions within the loop block that essentially create mini workflows.
When the workflow is executed, it starts with all the preceding actions, then executes all the actions within the loop block repeatedly until the defined count or condition is met, and subsequently proceeds to the following actions.
How To Use Loops In Your Workflow
When you add a ‘Loop’ action, you will choose between ‘Times’ and ‘Each Item’ loops.. Let’s understand what these are and how to use them.
Using ‘Times’ Loops
‘Times’ loops let you repeat a task or action for a specific number of times (e.g., create several identical notes in Evernote). Here is an example workflow to show you how it works:
First, drag and drop the loop action onto the canvas, and connect it to the start action as shown in the image below:
Now, search for Evernote – Create Note action in the right panel, and drag this action inside the loop block. The tiny green icon on the left edge of the loop block is the start point of the loop actions, and the red icon on the right of the block is the end point. Connect the start point of the loop action to the Evernote action.
Now, go to the configuration page of the Evernote action by double clicking on the action. Fill up all the fields as you would normally do to create a note in Evernote.
Once you have entered all the details, click on 'Next'. You will be redirected to the ‘Test Action’ window where you can see the input data provided by you to configure the ‘Evernote - Create Note’ action along with a ‘Test’ button.
When you click on the ‘Test’ button, Built.io Flow will actually perform the action. It will automatically create a note in your Evernote account and will show the output of this action in the window that appears. This output can then be used to configure the rest of the workflow.
Click on 'Done'. This will take you back to canvas. Connect the Evernote action to the end point of the loop block. And then connect the loop action to the stop icon of the workflow.
Now it’s time to configure the loop action. To do this, hover on the loop action, and click the settings icon that appear on the top, or simply double click on the loop action.
In the form, select the type of loop that you wish to use. In this case, it’s ‘Times’ as we want to repeat the action for a specific number of times. Then, under ‘Number of Times’, specify how many times you want the action to repeat (e.g., 5).
Click ‘Done’ to return to canvas, and save the workflow. Now, when you execute this workflow, five identical notes will be created in your Evernote account.
Using ‘Each Item’ Loops
This type of loop lets you perform an action (or multiple actions) on each item. For example, if you need to fetch a list of all of your Trello cards and create a note in Evernote for each card (the name of each card being the title of the corresponding note being created in Evernote), this is what you should use.
This is much faster than creating several separate workflows that would each fetch a single Trello card’s name and and create a new note in Evernote.
Let’s see how to do this quickly using ‘Each Item’ loops. We’ll recycle the workflow we used in the last example.
Delete the line that connects the ‘Start’ point with the loop action. Add Trello – Get All List ards and connect the workflow as shown below.
Open the Trello action’s configuration page and select the IDs of your desired board and list.
Next, go to the configuration page of the loop action and select ‘Each Item’ from he options under Loop Type.
In the ‘Source Array/Object’ field, specify the array with which we wish to work. On the right-hand side panel, under ‘Input’, we can see the output of the Trello – Get All List Cards action. We will use the ‘cards’ array as we want to fetch the list of all cards.
Click on the ‘Next’ button. You will be redirected to the ‘Test Action’ window where you can see the input data provided by you to configure the ‘Loop action along with a ‘Test’ button.
When you click on the ‘Test’ button, Built.io Flow will actually perform the action. It will automatically iterate over all the items of the array and will show the output of this action in the window that appears. This output can then be used to configure the rest of the workflow.
Click ‘Done’ to return to the canvas.
Now, configure the ‘Evernote – Create Note’ action within the loop action. Edit the title of the note to be the name of the Trello card that you want to fetch.
To do this, use the ‘currentValue’ property, located on the right-hand side panel, under ‘Loop’. This property fetches the value of the keys of all the objects available in the array that we have selected in the Loop action (‘cards’ in this case).
Here you only want to fetch the value of ‘name’ keys from the objects of the array, since you want the name of the card to be the title of your notes. To do this, click on the drop down icon and click on the 'name' key listed under 'currentValue'.
Click on the ‘Next’ button. You will be redirected to the ‘Test Action’ window where you can see the input data provided by you to configure the ‘Evernote - Create Note action along with a ‘Test’ button.
When you click on the ‘Test’ button, Built.io Flow will actually perform the action. It will automatically create a note and will show the output of this action in the window that appears. This output can then be used to configure the rest of the workflow.
Understanding Loop’s Output
While creating the above workflow, we used the ‘currentValue’ property of the Loop action’s output. There are several other useful properties that you can use in this workflow as well. Here are a few:
currentIndex: This property fetches the index (or the position) of the current iteration. In the above example, if you use {{$a0.currentIndex}} in the ‘Body’ field of the Evernote action, it will specify the position (examples include of the object in an array of objects; of the item in a normal array) of the card for which the note is being created. It will generate ‘0’ for the first card, ‘1’ for the second, and so on.
currentKey: In case the ‘Source’ selected in Loop settings is an object, this property will return only the keys of object. In case the ‘Source’ is an array, it will return the index of every item.
currentValue: In case the ‘Source’ selected in Loop settings is an object, this property will return only the values of the object. In case the ‘Source’ is an array, it returns all the items of an array, including whole objects, if any within the array. You can retrieve the value of particular keys/properties by using dot notation. For example, using {{$a0.currentValue.name}} will fetch the value of the name property.
totalLength: This returns the total count of all the objects within an array of objects, or the total count of all the items within an array.