Loop
Execute one or more actions in loop. Learn how a loop works and how to add it to a workflow, how to configure it, and how to use it in different use cases with the help of an example.
Execute one or more actions in loop. Learn how a loop works and how to add it to a workflow, how to configure it, and how to use it in different use cases with the help of an example.
The Loop action lets you run an action or a set of actions in a Loop. When you drag this action to the canvas, it looks like a block within which you can add multiple actions and create a mini-workflow. You can then connect this Loop action to other actions of your workflow. When you execute the workflow, IBM webMethods Integration executes this mini-workflow repeatedly till it reaches the specified count. You can also add loop within loop to create complex workflows.
To use Loop in your workflow, add this action to your canvas and double-click it. This will open the loop canvas where you can add the action you want to execute in loop.
This is how the Loop canvas looks.
Loop Breadcrumbs: At the top-left corner, you can see the loop breadcrumbs. Whenever you create a loop within loop, the breadcrumb for that loop is appended here. You can use this breadcrumb to navigate to any particular loop level.
Loop Start: This is the starting point of the loop.
Loop Stop: This is the last point of the loop.
From the list of connectors panel on the right, you can add relevant connectors between Loop Start and Loop Stop icons, and configure them as you would normally do for a workflow.
Close Loop Canvas: You can navigate back to the workflow canvas by clicking this icon.
Loop Settings: You can configure the current loop action by clicking this icon.
To configure Loop settings, hover on the Loop action and click the Settings icon that appear. The Loop configuration window will appear on screen. On the right-hand side you can see the Action Configure panel where you can provide inputs to execute the Loop action.
There are two types of Loops:
Times: Lets you do a particular thing x number of times.
Each Item: Lets you perform a certain action(s) for all the elements of a collection
Let’s now look at each of the loop types individually
The Times Loop allows you to perform a certain action x number of times. It lets you specify the number of times the Loop block should be executed. It is recommended that you run this Loop when the workflow within the Loop action doesn’t need its input from the previous action.
To use this loop, select Times in the Select Loop Type drop-down field and specify the number of times you want the loop to be executed in the Number of Times field.
Let’s understand how to use this loop type with the help of an example. Let’s say you want to create five new notes in Evernote with same content. Instead of performing the same action five times, we can use Times Loop.
To do this, follow the instructions given below:
Add the Loop action to your canvas and double-click it. You will be redirected to the Loop canvas. Add Evernote connector on canvas and connect it as shown below:
Next, double-click the Evernote connector icon, select the Create Note action from the Select Action drop down list, and select the Evernote account you want to use to execute the action.
Once this is done, click Next.
In the action configuration form that appears next, provide the details as given below:
Title: Provide a suitable title for the note you want to create.
Notebook ID: Select/specify the ID of the notebook in which you want to create a new note.
Body (Optional): Enter the content that you want to add in the note.
Once you have entered all the details, click Next. You will be redirected to the Test action window where you can optionally Test the action to check if you have configured it properly, or click on Done to return to loop canvas.
Next, hover on the Loop action icon, click the Settings icon that appear, and configure the Loop action as shown below:
Select Loop type: Since we need to run the Loop for specific number of times, select Times.
Number of Times: Enter the number of times (5) you want to execute the Loop.
Once you have entered all the details, click Next, optionally Test the Loop action, and click Done. This will take you back to loop canvas.
Click on the Close Loop Canvas (X) icon.
This will take you back to the workflow canvas.
Save the workflow and click Test to start workflow execution. This will execute the action added inside the loop 5 times and create five identical notes in your Evernote account.
Each Item is used to iterate through each key/value pair in the given array. It traverses the given array list, i.e., it goes through each element of the given array. Hence, this action accepts arrays or objects received from previous actions as input. You need not specify the number of times it should run. This Loop will be executed multiple times, each time taking one item or object of the array as specified in the Array or Objectfield.
To use this loop, select Each Item in the Select Loop Type drop-down field and array/object you want to iterate through in the Number of Times field.
Let’s understand how to use this loop type with the help of an example.
The Trello - Get All List Cards action returns the list of all cards of a list. It returns an array that contains cards as objects. Each object contains properties such as id, title, type, and creation date associated with each card.
Let’s say you want to retrieve just the title of all cards, instead of all the details and send it to a specific recipient. The Each Item loop type allows you do this.
Follow the instructions given below to achieve this:
Add Trello connector and Loop action to canvas and connect them as shown below:
Double-click the Trello icon, select the Get All List Cards action from the Select action drop down list, and select the Trello account you want to use to execute the action.
Once this is done, click Next.
In the action configuration form that appears next, provide the details as given below:
Board ID: Select/specify the ID of the board of which cards you want to retrieve.
List ID: Select/specify the ID of the list of which cards you want to retrieve.
Once you have entered all the details, click Next. You will be redirected to the Test Action window where you need to Test the action to check if you have configured it properly, and click on Done to return to canvas.
Next, hover on the Loop action icon, click the Settings icon that appear, and configure the Loop action as shown below:
Select Loop type: Since we need to retrieve all the items of the list, select Each Item Loop type.
Source Array or object: Add the Cards array listed under the Trello - Get All List Cards action given on the left-side of the configuration window, as an input for this field.
Once you have entered all the details, click Next, optionally Test the Loop action, and click Done. This will take you back to loop canvas.
Next, double-click the Loop action icon. This will take you to the loop canvas. Add Send an Email action between Loop Start and Loop Stop icons.
This will take you back to the workflow canvas.
Configure the Send an Email action as given below:
To: Enter the email ID of the person you want to send the list of cards to.
Subject: Provide a suitable subject for the email.
As the Loop action will be iterating through all elements of the array, we will use the output keys of the Loop action in the Body
Body: Locate the currentItem key listed under the loop output on the left-side of the screen. This key contains all the details of the element currently under execution (learn about loop output keys). Since we want only the name of the card, click on the drop down icon and locate the name key. Add this key in the Body field. This will fetch the name of the array element currently under execution.
Once you have entered all the details, click Next, Test the Loop action, and click Done. This will take you back to loop canvas.
Click on the Close Loop Canvas icon (X) located at the top-right corner of the loop canvas. This will take you back to the workflow canvas.
Save the workflow and click Test to start workflow execution.
This will fetch all card details from the specified Trello list, fetch the name of each card using loop, and send it to the specified recipient via email.
Given below is the list of loop output keys and their description:
currentIndex: Returns the index of the array/object element currently under execution.
currentItem: Returns the value available inside the element currently under execution.
currentKey: Returns the name of the key which is provided as the source array/object.
currentValue: Returns the value available inside the element currently under execution. This value is mostly similar to the value of the currentItem key.
totalLength: Returns the total length of the source array/object.
You can also create loop within loop (nested loop) to build complex workflows. There is no limit on the number of loops you can add.
Let’s understand how to configure a nested loop with the help of an example.
Let’s say you want to create a workflow which fetches all cards from a specific Trello board, creates a new note for each card in a specific notebook in Evernote, and post the details of the note on a specific space in Cisco Webex Teams.
To do this, we will use two levels of loop. The steps given below explain the steps to do this:
Create main workflow
Drag-and-drop Trello - Get Board Cards action on canvas and configure it as you would normally do. Next, add the Loop action to canvas, connect it with Trello - Get Board Cards action, and configure it as given below:
Hover over the Loop action icon and click the Settings icon. This will open the Loop action configuration window. Set Select Loop Type field to Each Item and add Cards key listed in the output of theTrello - Get Board Cards action in the Source Array/Object field, like we did in the previous example.
Once this is done, click Next, optionally Test the action and click Done. This will take you back to canvas. You can now start adding other actions inside the loop.
Add actions to Loop
Double click the Loop icon. You will see a Loop canvas within the original canvas. This is where you can add actions you want to execute inside the Loop.
Since we want to create a note for each Trello card in Evernote, add the Evernote - Create Note and Loop actions to canvas and connect them to Loop Start and Loop Stop icons as given below:
Add the name key listed under the Loop - currentItem key in the Title key and desc key in the Body key. Specify the notebook in which you want to create notes in the Notebook ID field.
Once this is done, click Next, optionally Test the action, and click Done. This will take you back to Loop canvas.
Next, hover over the Loop action icon and click the Settings icon. This will open the Loop configuration window.
Set the Select Loop Type to Times and enter 1 in the Number of Times field.
Once this is done, click Next, optionally Test the action, and click Done. This will take you back to Loop canvas.
Add actions to Loop - Level 2
Next, double-click the Loop action. This will open the Loop-Level 2 canvas.
By clicking the relevant breadcrumbs link highlighted in the image above, you can toggle between different loop levels.
You can now add actions to this Loop - Level 2 canvas, just like we did in the previous step.
For the purpose of our example, we want to post the details of the created note on a specific Cisco Webex Teams space. To do so, add the Cisco Webex Teams - Post New Message action on canvas and add it to Loop Start and Loop Stop icons.
Configure the Cisco Webex Teams - Post New Message action as given below:
Specify the space on which you want to post the message in the Space ID field. Add the title and content keys listed under the Evernote - Create Note action in the Message field.
Once this is done, click Next, optionally Test the action, and click Done. This will take you back to Loop - Level 2 canvas.
With this, your workflow is complete.
Click on the Close icon (X) located at the top-right of the Loop - Level 2 canvas.
This will take you back to the main workflow.
Save the workflow and execute it. This will fetch all cards from the specified list in Trello, create a note for each card in Evernote, and post the details of the note on the specified Cisco Webex Teams space.
This was just an example of how you can use nested loop. You can add more loops levels and create complex workflows as per your requirements.