Loop

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, webMethods.io 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.

Loop-main-canvas.png

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.

Types of Loops

There are two types of Loops: 

  1. Times: Lets you do a particular thing x number of times. 
  2. 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.

Times

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:

Step 1: 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:

2-loop-times-canvas.png


Step 2: 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.

2-evernote.png


Step 3: 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.

Step 4: 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.

3-loop-times.png


Step 5: 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.

Step 6: Click on the Close Loop Canvas (X) icon.

3-loop-times-evernote-example.png



This will take you back to the workflow canvas.

Step 7: 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

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 Object field.

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:

Step 1: Add Trello connector and Loop action to canvas and connect them as shown below:

4-each-item-main-workflow.png


Step 2: 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.

4-trello.png


Step 3: 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.

Step 4: Next, hover on the Loop icon, click the Settings icon, 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.

5-loop-each-item.png


Step 5: Once you have entered all the details, click Next, Test the Loop action, and click Done. This will take you back to canvas.

Step 6: 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.

5-send-an-email-each-item.png



Step 7: 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 field.

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.

7-loop-each-item.png

Step 8: Once you have entered all the details, click Next, Test the Loop action, and click Done. This will take you back to loop canvas.

Step 9: 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.

Step 10: 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.

Loop output keys

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.

Nested Loop

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:

Step 1: 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:

3-main-loop-workflow.png


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 the Trello - Get Board Cards action in the Source Array/Object field, like we did in the previous example.

loop-1.png

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.

Step 2: 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. 

2-loop-config.png

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:

loop-1-config.png

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.

evernote-loop.png

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.

loop-2-config.png

Once this is done, click Next, optionally Test the action, and click Done. This will take you back to Loop canvas.

Step 3: Add actions to Loop - Level 2

Next, double-click the Loop action. This will open the Loop-Level 2 canvas.

loop-2-canvas.png

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. 

post-msg-loop-2.png

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. 

post-msg-config.png

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. 

close-canvas.png


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.