The Loop action executes 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—Repeats a task or multiple tasks until a specific count is met. So, for instance, if you want to create many identical contacts in your Onedrive account, use the ‘Times’ loop time in your workflow. It performs a task x number of times.
Each Item—Repeats a task or multiple tasks for each item in an array or a collection. You can use this type of loop if you want to fetch only the value of the ‘name’ property from all the objects of a returned array. It performs the task for every item in a collection or an array.
For example, use the Each Item loop option if you want to fetch a list of all of your Trello cards and create a task in Google Tasks for each card. The name of each card is the title of the corresponding task that is created in Google Tasks. This works much faster than creating several separate workflows that fetches a single Trello card’s name and then create a new task in Google Tasks.
Using the Loop Action
To use this action in your workflow, connect it to the ThingWorx Flow. To connect to the flow, do the following:
1. Click and drag the Loop action under the Loop utility to the canvas. The Loop action can be expanded or contracted per your requirements. You can add multiple actions within the loop block to create mini workflows. Once an action is dragged on workflow canvas outside the loop, it cannot be dragged inside the loop. To add action in loop, drag it from action navigation panel to loop directly.
When a 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 then subsequently proceeds to the following actions.
2. Place the pointer on the action, and then click or double-click the action. The Loop action window opens.
3. Edit the label name, if needed. By default, the label name is same as the action name.
4. In the Select Loop Type field, select one of the following options:
Times—Specify the number of times in the Number of Times field the loop should be executed. It is recommended that you run this loop when the workflow within Loop action do not need its input from the previous action.
Each Item—Executes multiple times, each time taking one item or object of the array as specified in the Source Array or Object field.
5. Click Done.
The following video demonstrates how to use the Loop action.
Output Schema
The Loop action returns an output schema as shown in the figure that follows:
currentIndex—This property fetches the index or the position of the current iteration.
currentItem—If the source selected in the Loop settings is an array of objects, this property returns current object for each iteration.
currentKey—If the source selected in the Loop settings is an object, this property returns only the keys of object. In case the source is an array, it returns the index of every item.
currentValue—If the Source selected in Loop settings is an object, this property returns 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 or properties by using a dot notation such as {{$a0.currentValue.name}}
totalLength—Returns the total count of all the objects within an array of objects, or the total count of all the items within an array.