Nintex Workflow – Iterate and Start a Workflow
I have found that many people are interested in not only starting a workflow as soon as a document or list item is created, but also to run a workflow over a number of items in another list or document library.
You have 2 options here.
1. Create a workflow that performs all the logic for each item in the workflow.
2. Create a workflow that iterates through each item in a destination list/doclib and starts a workflow off for that item.
To do this, I’m going to create a hidden list that will contain workflows I have created and will run on a scheduled basis. To be fairly obvious, I’m going to name this list List For Scheduled Workflows.
My destination list that I want to start workflows on will be named Random Items. In Random Items, I have created a Yes/No field named “Processed”, so that my workflows will know whether there needs to be work done.
ow I’ll create a workflow, where for this example, all I am going to do is set the Processed field to Yes for the item the workflow is running on.
We now jump back to my List For Scheduled Workflows and create a workflow that will run periodically, query the destination list, and start child workflows.
The first thing we need are the Nintex Workflow variables.
1. collListItemIDs – Collection variable that will contain all the items for the items in the destination list that have not yet been processed.
2. listItemID – this will be used in our For Each loop. As we iterated through the loop, this variable will contain the List Item ID we are up to.
If you want to see how to use the collection variable, please read this : Nintex Workflow – Collection Variable
I’m not going to bother with the Error Handling in this workflow, so that we keep it small, but if I was going to put this type of workflow into Product, I would definitely enable Error Handling.
Now that we have our Workflow Variables, our workflow itself will be quite simple :
1. Query the destination list of unprocessed items
2. Loop through the results of that query
3. Start a Workflow on each unprocessed item
Query List
ere we want to query our destination list for all items that have a Processed field set to No (they haven’t been processed yet), and we want to get back the ID of each item. The reason we want to get the ID, is because that is unique for all items in a list, and this will let us start a workflow on each specific item.
For Each
We now go through our collection variable “collListItemIDs”, and through each iteration, we store the Item ID in our variable “listItemID”.
Call Web Service
Now that we have the List Item ID we want to run the workflow on, we can utilize the Nintex Workflow web service, and specifically the StartWorkflowOnListItem.
All the Nintex Workflow Web Service methods are described in the Nintex Workflow SDK.
We configure the Call Web Service action to call the StartWorfklowOnListItem web method, and give it the List Item ID (our listItemID variable), the List Name (Random Items), and the workflow we want to run (Process Random Item Workflow).
Testing this Process
Once the workflow is published, we can test it out.
From the screenshot below, you can see that my Random Items list has 3 items, all of which have a Processed field set to No.
Moving back to my “List for Scheduled Workflows” list, I haved added a dummy item as in the future (not this article) I will be creating a scheduled workflow here.
I can now start my workflow off on this item.
As this workflow doesn’t do very much but start workflows off on other items asynchronously, the workflow itself is quite fast. When we go and look at the “Random Items” list, we see that all the items now have their Processed field set to Yes. Also we see that the “Process Random Item Workflow” status column is also set to “Complete”, so we can be sure the workflows we started all worked.