How To: Loop through a list from a SQL stored procedure

In most instances, when you configure the Loop Step with a reference, it works as you expect, allowing you to use information about each item in each Next Item iteration of the loop. You only need to follow the instructions in this topic if your reference's list method does not contain an input property to retrieve a single record, and all record information in the reference is returned as a concatenated string in each loop iteration.

When you use a SmartObject method in the Loop step, the method must have an input property to load a particular 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 the references to get 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.

Service object methods, such as the GetContacts method for SQL stored procedure, 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 .

This example is for scenarios where you cannot modify the stored procedure to contain a unique identifier.

Before you begin

This How To assumes that you are familiar with SQL stored procedures and K2.

Runtime Example

Scenario

You want to use a stored procedure that returns a list of customers in a workflow and send a notification email of available specials.

This scenario uses a SQL database called My SQL Database with a table called ContactDetails. The table contains four columns - ID, Company, Name, and Email. It also contains a stored procedure called GetContacts. A SQL service instance for this database and associated SmartObjects exist in your K2 environment. For more information about creating a service instance, see the Service Instances topic.

You use the Loop Index reference as the identifier that calls each item in the list.

Steps