Over the last couple of weeks people have asked me how to use the For Each loop event in K2, so I thought I would put this quick demo together.

The demo loops through a list of subscribers and updates their status from ‘Not Sent’ to ‘Sent’.

Getting started

For demo purposes I have created a simple SmartBox SmartObject with the following properties:

  • Name, of type string
  • Email, of type string
  • Id, of type autonumber and key
  • Status, of type string

I have inserted three rows of data with the status ‘Not Sent’. We will use the For Each event to loop through this data.

Creating the workflow

Adding the For Each event

  • Open K2 Studio or K2 for Visual Studio (I am going to use K2 Studio)
  • Create a new workflow solution called ‘For Each Demo’
  • Rename the workflow from Process 1 to ‘ForEachWorkflow’
  • Once the workflow canvas has loaded, drag the For Each event on to the canvas
  • In the event wizard, give the event the name ‘Loop demo’
  • Click on the Source icon so the drop-down list disappears, and open the context browser
  • In the first tab (Environment), expand ‘SmartObject Services’
  • Go to the SmartObject you want to use and expand the list method you want to use
  • Drag the property you want to use, normally the primary key, into the event box
  • The SmartObject wizard loads up. Click ‘Next’ and ‘Next’ again
  • In ‘Select a return property’, make sure the ‘Id’ value is selected
  • Click ‘Next’ and make sure ‘Return all results that match filter’ is selected
  • Click ‘Finish’
  • For the reference name, enter ‘Dummy Data Reference’
  • For the index name, enter ‘Dummy Index’
  • Click on ‘Finish’

Interacting with the For Each item

  • Back on the canvas you will notice there are now two lines coming out of the activity
  • Drag a placeholder event on to the canvas and name both the event and the activity ‘End’
  • Drag the line named ‘No more items’ to the End activity
  • Create a new activity on the canvas and name it ‘Status Change’
  • Drag the SmartObject event into the new activity
  • In the event wizard for the SmartObject event, name it ‘Update Status’
  • Select the update method for the SmartObject you used with the For Each event
  • Click ‘Next’
  • For the ‘Id’ property, click Assign
  • In the context browser, go to the third tab (Data), expand ‘Item References’ and then ‘Loop demo reference’
  • Click on the ‘Id’ property and click on ‘Add’
  • Click on ‘Status’, click ‘Assign’ and enter the text ‘Sent’
  • Click on ‘Next’, ‘Next’ again and then ‘Finish’
  • Drag the remaining line, named ‘Next item’, to this activity, then create a new line from this activity back to the For Each activity

That is a simple example of using the For Each event. Anything that follows the ‘Next item’ line is inside the loop and has the context of the current row. You can have multiple activities in this part, and the workflow will move on to the next item once it has completed everything in that section. You must always have a line going back to the activity with the For Each event in it.

Other things to remember

  • Choose one property to return in the For Each event. It’s always best to use a primary or foreign key
  • Remember to select ‘Return all results that match filter’, as it returns the complete list. It doesn’t matter if no filter is applied

Next step: using references with the For Each event

So we have created a simple workflow with a simple For Each loop. But what if we wanted to get more data for each item and use it in a line rule?

In the next example, the workflow checks the current status of the item it is looking at. If the status is already ‘Sent’, it ignores it and moves on to the next item in the list. If the status is ‘Not Sent’, it updates the status as normal.

I am going to use the same workflow as before.

Creating the reference

  • In the context browser, go to the third tab (Data)
  • Right-click on ‘References’ and click on ‘Add’
  • In ‘SmartObject Method Reference’, click on ‘Next’
  • Give the reference a name. For my example I have used ‘Dummy data’
  • In SmartObject Method, select a read-type method that takes the value you selected in the For Each as its input parameter. I am pointing my reference at the Read method
  • Click ‘Next’
  • In Input Mapping, click on Assign and select the Id from the For Each loop’s item reference
  • Click ‘Next’ and ‘Finish’
  • We have now created a reference that gets the related details for the current row of the For Each event

Editing the line rule for ‘Next item’

Now that we have the reference set up, we can edit the line rule for ‘Next item’.

  • Right-click on ‘Next item’ and click on ‘Properties’
  • ‘Line General Properties’ will load up. Click on the green arrow for line rules
  • Click on ‘Add’
  • In the rule editor, select ‘And’ from ‘Boolean Operator’
  • Click into ‘First Variable’ and, in the context browser, go to References in the third tab
  • Expand References, then expand the reference you created in the last section
  • Drag ‘Status’ into ‘First Variable’
  • The SmartObject wizard pops up. Click on ‘Status’ and then ‘Finish’
  • Select ‘<>’ from ‘Comparison Operator’
  • In ‘Second Variable’, enter ‘Sent’
  • Click on ‘OK’ and then ‘Finish’
  • We now need another rule, similar to the ‘Next item’ rule, for when the status is equal to ‘Sent’

Creating a new line rule for ‘Next item’ where status equals ‘Sent’

  • Right-click and drag a line from the For Each activity to ‘Update Status’
  • Then drag the line from ‘Update Status’ to For Each. (We have to do this because a line can’t go back on itself without being connected to another activity first)
  • Right-click on the line and click on ‘Properties’
  • In ‘Label’, enter ‘Next item and status is sent’
  • Go to the line rules and click on ‘Add’
  • In the rule editor, open the context browser for ‘First Variable’ and expand the process data fields
  • Click on ‘Dummy Index Result’ and click ‘OK’
  • Select ‘=’ for the operator and enter ‘True’ in ‘Second Variable’
  • Click ‘OK’
  • Click on ‘Add’ again and select ‘And’ from ‘Boolean Operator’
  • For ‘First Variable’, go to References in the third tab of the context browser, expand the reference you created earlier and drag ‘Status’ in
  • The SmartObject wizard pops up. Click on ‘Status’ and then ‘Finish’
  • Select ‘<>’ from ‘Comparison Operator’
  • In ‘Second Variable’, enter ‘Sent’
  • Click on ‘OK’ and then ‘Finish’