Using the Loop Step in a Workflow to Iterate through a Reference
When using references in a workflow, you may find that the entire list of items in the reference is returned instead of a single item. This can occur when the reference's list method does not have an input property (filter) to retrieve a single record, so it returns all items in the reference, concatenated together, which is typically not what you need.
For information about using the Create Reference and Loop steps, see the following Help topics:
- Create Reference Step (K2 Cloud)
- Create Reference Step (K2 Five)
- Loop Step (K2 Cloud)
- Loop Step (K2 Five)
Example of Using a Reference with the Loop Step
In this example you are sending an email to all customers in a list. The workflow loops through the email step until all customers on the list have been emailed, after which the workflow continues. In the case where the reference's list method does not contain an input property to identify individual items within the reference, you can use the reference together with the Loop step in order to send a series of emails. This works for other scenarios where you must perform an action in the workflow per item in the reference.
Workaround and More Information
For a typical reference that has a unique identifier, the SmartObject list method has an input property to load a record in the reference, typically an ID, autonumber or GUID, but it could also be a non-key field that acts as a filter on the method. This means that you’re passing a key or filter field as an input to the method, and that the field selected in the Create Reference step is the same as the input on the SmartObject method that you use in the Loop step. If you don’t match these or there is no input property on the method, all rows are returned and concatenated for each iteration of the loop.
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.
SmartObject methods, such as the Get List Items by View method for SharePoint used here, do not always contain an input field. You can still configure looping in your workflow using the service object methods by creating a Loop Index and then using this index as the identifier. While SmartBox SmartObject list methods automatically include the input property for passing in the ID, but other SmartObject methods (such as SharePoint, SQL Stored Procedures, and Web Service methods) may not. To use these methods you can do one of the following to perform an action on each individual item within the reference:
- Create an input property by adding a new property to the method.
- Use the Loop index to create the identifier and then use the Loop Index to join the identifier with the data. To configure the Loop index see How To: Create a Loop through a List of Items using SharePoint.
How to check the SmartObject method for an input property
Check to see if your SmartObject or service object method that you want to use has an input property. You can do this by following these steps:
- Open K2 Designer.
- Browser to the category containing the SmartObject or service object method.
- Select Advanced SmartObject as the type on the General tab. This allows you to see the methods, otherwise you’ll only see the properties.
- Click the Properties and Methods tab.
- Select the Methods tab.
- Click on the List method and select Edit.
- Click NEXT on the Edit Method page.
- Select the Service Object method and click Edit.
- On the Edit Service Object method bindings page see the input properties and if they are mapped to a value. Below is an example of a SmartBox Get List method.
How to Configure your Workflow
The following How to topics guide you through configuring a workflow with a Loop step:
- Standard workflow with a SmartBox Get List method that has an input property – How To: Create a Loop Through a List of Items
- Configure a workflow that integrates with SharePoint and uses a SharePoint SmartObject method that does not have an input property, you will use the Loop Index to link the Identifier to the referenced item’s value. - How To: Create a Loop through a List of Items using SharePoint
This specific example is no longer applicable from K2 Five 5.3 and K2 Cloud Update 8 onward. There was an issue with the way that workflow references were able to filter when a SmartObject Method does not have the unique property as an input property. This issue was fixed in K2 Cloud Update 8 / K2 Five 5.3. You can use this how-to example in pre-K2 Five 5.3 environments where the issue still exists. Note that the issue also affected other service types, not just SharePoint SmartObjects.
- Configure a workflow that integrates with SQL Store procedures and uses a SQL SmartObject method that does not have an input property. – How To: Loop through a list from a SQL stored procedure