Friday, March 11, 2011

Looping Workflow Through All List Items

Background
I came across this solution because I was trying to calculate the number of days an item in a list was in a specific state and to send an alert to the creator of the item after the number of days was exceeded. I started by using the “Today” trick. Although the today trick worked to find the difference between dates I still needed a way to cycle through all the list items and send an alert when the date difference exceeded. Since you cannot easily calculate the difference between two dates using [Today] I decided to use the workflow Action “Find Interval Between Dates”. I came across few postings on the web that talked about using two lists to do this but I didn’t want to use two lists so I came up with this solution.

How to Create a Looping Workflow
Create a Custom List called Looping
Create a column called DueDate

  • Type: Date and Time
Create a column called DaysUntilDue
  • Type: Number
  • Decimal Places: 0
Create a column called CountDaysUntilDue
  • Type: Choice
  • Choices: Yes and No
Create a List Workflow called Loop Through Items
  • Initiation Settings: Allow this workflow to be manually started
Set Workflow Variable
  • Workflow variable: idCurrentItem
  • Value:
  • Data source: Looping
  • Field from source: ID
  • Field: CountDaysUntilDueValue: Yes

Find Interval Between Dates
  • Difference Type: Days
  • Date: Current Date

  • Date: DueDate
  • Variable: numDaysUntilDue
Update List Item
  • Field: DaysUntilDue
  • Value: numDaysUntilDue
  • Field: CountDaysUntilDue
  • Value: No
 
Start another workflow
  • This item:
  • List: Looping
  • Field: CountDaysUntilDue
  • Value: Yes
  • This: Loop Through Items
Save and Publish the workflow
  • For each item in the list that you want the workflow to process set the CountDaysUntilDue to yes.
 
Manually start the workflow for the first item on the list.
  • It may take time to for the next workflow to change from Starting to In Progress to Complete.
 

After the workflow updates the last item on the list it returns an Error Occurred result because there is not another item on the list to execute. So in order to run the workflow again on the last item on the list you need to terminate the workflow on that item.


Notes

I’m still trying to figure out how to get the workflow to complete the last item cleanly until I figure out how to do that you need to

Related Articles