Loop Step
Use the Loop step to perform a looping action in a workflow. Create dynamic reference and index fields to loop through items from an item collection. The Loop step is designed to use references to retrieve each referenced item's values from the SmartObject when it starts each loop. When there are no more items in the reference, the loop completes.
Use the Loop step in the following scenarios:
- To loop through a list of items in order to perform tasks on each of the items within the list
- To loop through a list of static values provided to the step
- To loop through a list of items in a reference created in the workflow
Requirement
The Loop step requires that the SmartObject List method has an input property, such as ID, autonumber or GUID to be able to retrieve the referenced items. If your SmartObject or Service Object List method does not have an input property, refer to the following article for more information: KB002121 - Using the Loop Step in a Workflow to iterate through a Reference.
Drag the Loop step from the Logic category, Favorites bar, or the Recent category onto the canvas.
Example of a Loop step in a workflow
- See the Infinite Loop topic for considerations about potentially creating infinite loops or loops that could affect your environment when looping through large sets of data.
- See How To: Filter and Update List Items for an example of using the Create Reference step to create a reference that is used by a Loop step in a workflow.
- See How To: Use a Loop Step to Loop Through a List for an example of using a Loop step in a workflow to loop through items in a SharePoint list.
- See How To: Use a Loop Step with a Loop Index for an example of building a workflow to loop through a list of items using a loop index.
Use this to learn how to use the Loop step.
- From the Toolbox, click the Logic category, and then click and drag the Loop step onto the canvas.
- From the Toolbox, click the Basic category, and then click and drag the Create Reference step onto the canvas. Configure this step to create a reference to a SmartObject list.
- Configure the Create Reference step and click the Create link to create the reference.
- From the Toolbox, click the Logic category, and then click and drag the Loop step onto the canvas.
- In the List section, select a reference from the drop down menu.
- After you choose a reference, the new list item reference gets created for you. This reference is how you access the current item that contains a list of referenced properties. To view the item reference, expand the Options section.
- Click the expand / collapse toggle to expand the Configuration Panel. This exposes the references for both the Create Reference step and the List Item reference created on the Loop step
.
- Click the Create link in the Index section to store the current loop index. You can select an existing index from the Or Choose an existing item... link.
If your workflow has multiple loop step configurations, you can select an existing Loop Index. At runtime the selected Loop Index is re-used. You can also select a variable, created in the Context Browser, from the Or Choose an existing item link.
- The Loop Index variable is created. Click the Edit link to change the Loop Index label. Click the Change link to select another index.
- Click the expand / collapse toggle to expand the Configuration Panel. Notice the new Loop Index variable.
- From the Toolbox click the Basic category, and then click and drag the Send Email step onto the canvas. Select the Send Email tab. Customize the email message by providing a subject line, additional recipients, and body of the email. Click the Open Text Editor link to customize the message. Type the email body. You can use the text formatting tools such as Font, Font size, Bold, Italic, Underline and Align. You can also use of the Context Browser to drag fields and functions to define the message.
- Drag a line from the Next Item port (on the Loop step) to connect it to the Send Email step.
- The Loop step creates two paths, namely Next Item and Complete. You need to connect the Next Item path back to the Loop step so the step can successfully loop. This action ensures that your workflow loops through the entire list.
- After the loop action has run through the entire list, the complete path is followed.
- You can add a non-outcome path to the Loop step if your workflow requires it. However, it is important to keep in mind that the activity connected to this non-outcome path fires with the Next Item outcome until the loop has completed and when the completed path is followed. In this example, a non-outcome path is added to the Loop step.
After the reference is created, the Next Item path (Send Email) is followed and loops back to the step. Before the next round of the Loop step starts, the non-outcome path is followed. This means if your Loop step loops through 10 items, the non-outcome path is also followed 10 times. After all items have been looped through, the complete and the non-outcome paths are followed, which means the non-outcome path is followed a total of 11 times.
- From the Toolbox click the Logic category, and then click and drag the Loop step onto the canvas.
- In the List section, select Type/Construct my own from the drop down menu. You can also click on the Toggle button to type or construct your own list.
- The dynamic mode is enabled and you can create your own list. Select this option to automatically create the Items reference in the Options section.
- Type the static items into the List field. Ensure the values are separated by a semi-colon. In this example, type a list of names Bob; Codi; Dennis; Erica.
- From the Toolbox click the Basic category, and then click and drag the SmartObject Method step onto the canvas.
- In the SmartObject section, select the Browse option from the drop down menu. Browse and select a SmartObject. In this example, the SmartObject (Names SmartObject) has two properties - ID and Name. The loop function loops through the static values and creates an entry, for each value, into the Name property within the SmartObject. In the Method section select Create from the drop down menu.
- From the Input Mappings section, click the Add button and select the property. In this example, select Name.
- To locate the added reference, expand the Context Browser, select Fields and then expand the Reference section. Expand the Items reference, click and drag the Value field to the Input Mappings section. This creates the values, specified in the static list on the Loop step, to the Name property in the SmartObject.
- In this example, select the Do not map outputs radio button.
- Drag a line from the Next Item port (on the Loop step) to connect it to the SmartObject Method step.
- The Loop step creates two paths, namely Next Item and Complete. You need to connect the Next Item path back to the Loop step so the step can successfully loop. This action ensures that your workflow loops through the entire list.
- After the loop action has run through the static list, the complete path is followed.
- At runtime the workflow starts, loops through the static list and completes.
- Run a view on the Names SmartObject. In this example, the view is Names List View. Values from the static list created in the Name field of the SmartObject.
- You can add a non-outcome path to the Loop step if your workflow requires it. However, it is important to keep in mind that the activity connected to this non-outcome path fires with the Next Item outcome until the loop has completed and when the completed path is followed. In this example, a non-outcome path is added to the Loop step.
After the reference is created, the Next Item path (Send Email) is followed and loops back to the step. Before the next round of the Loop step starts, the non-outcome path is followed. This means if your Loop step loops through 10 items, the non-outcome path is also followed 10 times. After all items have been looped through, the complete and the non-outcome paths are followed, which means the non-outcome path is followed a total of 11 times.
- With the step selected, select the Properties tab.
- Click the Errors tab.
-
Your changes are automatically saved.
The following table explains the options available on this tab:
Option | Explanation | How to Use |
---|---|---|
List |
Specify a list to loop through. You can select a reference created within the workflow using a Create Reference step. |
Select a referenced list from the drop down menu. |
Item Reference | After selecting the reference from the List drop down menu, the current item in the loop is accessed by the created reference. The item reference shows below the selected list as a link. This link expands the Options section. | Click the added item reference link to expand the Options section. |
Options | Use this section to view the List Item and Index. By default, this section is not expanded. | Click the down arrow to expand this section. |
List Item | This is a reference to a current item in the list and automatically gets created when the list is selected. You can use this in your Loop steps. | Click the Edit link to change the List Item label. |
Index | This field stores the current loop index. You can either create a new one or select an existing item. |
Click the Create link to create a new item. Click the Or Choose an existing item... link to select one. Click the Edit link to change the Index label. Click the Change link to select a different existing item. |
You use a list of customer details that needs to be updated. You create a workflow, and as the first step in the workflow, you create a reference to this list. You then add a loop function to loop through all items within the list in order to send an email containing the customer details. The loop action runs through each item in the list and populates an email with information. This action continues until it has processed the entire list.
For more information on creating a reference, see the Create Reference step topic.
For more information on how to use the error console, see the Error Console topic.
See the Send Email step topic for more information about using this step.
In this example, instead of using a referenced list, you use a list of static values separated by commas. The static values in this example creates data (a list of names) in a SmartObject. This means, the loop function loops through the static values and creates a value, for each of the static values, into the specified field within the SmartObject. This action continues until it has processed all items in the static list.
The following table explains the options available on this tab:
Option | Explanation | How to Use |
---|---|---|
Title / Name | By default, each step on the canvas has a step title. You can change this title to suit your workflow logic and show it on the canvas by checking the Show Label box. | Enter a value into the Title field. |
Reset link | Use the link to reset any changes to the title. | Click the Reset link to reset the Title field to default value. |
Show Label | Allows you to see the step label on the canvas. The label shows the value of the title. | Check the check box to display step label on the canvas. |
Description | Allows you to add a detailed description for the step. | Enter a value into the Description field. |
Notes | Allows you to add additional notes for the step. | Enter a value into the Notes field. |
Different types of exceptions can occur on a step, such as:
Type | Description |
---|---|
Known exceptions | Any SmartWizard or standard wizard that has Boolean options for handling known exceptions have these options displayed as check boxes in the Known exceptions section of the Exceptions tab. |
Unhandled exceptions | Any unknown runtime exceptions. |
The tab contains the following known exceptions:
Exception | Explanation | How to Use |
---|---|---|
Continue on error |
On step execution and an error occurs, continue to the next step in the workflow. |
Check the box to continue if an error occurs. Uncheck the check box to stop workflow execution if an error occurs. |
The tab contains the following unhandled exception:
Exception | Explanation | How to Use |
---|---|---|
Log Exception | Logs the exception to the error log. | Check the box to log the exception. |
When working with the Loop step keep in mind the following considerations:
- The Loop step requires a List method type reference. This is created in the workflow (using the Create Reference step or any other step where a reference can be created using the List method) and selected from within the Loop step
- The Loop step creates two paths, namely Next Item and Complete. In order for the step to successfully loop through, you need to connect the Next Item path back to the Loop step
- The Complete path is followed when all items in the loop complete
- The Loop step cannot be added or made into a multi-step
- You cannot reset the loop index counter. Example: You loop through a list of five approvers. The 2nd person on the list declines. This does not reset the loop index counter. Instead, it continues to loop through the list and reaches the completed outcome.
- You cannot reset the loop index counter. Example: You loop through a list of five approvers. The 2nd person on the list declines. This does not reset the loop index counter. Instead, it continues to loop through the list and reaches the completed outcome.
- See the Infinite Loop topic for considerations about potentially creating infinite loops or loops that could affect your environment when looping through large sets of data.
- If your loop references a large list of records and the loop does not contain any asynchronous steps where the workflow pauses in the loop, be aware that you could be adding a lot of processing load to your K2 environment while it executes the loop for each item in the loop. For example, if your loop step contains only a SmartObject step and you are looping through thousands of records in the loop, you could be adding a lot of processing load onto your K2 environment while it processes the loop.