Search This Blog

Thursday, January 13, 2011

SharePoint Designer Workflows 3 - Developing a Timer Loop for Workflows

1. Introduction: Many times when designing workflows there is a need to use a Workflow Timer. As you may know a workflow timer will process workflows at a specified time or interval. While SharePoint Designer does not provide an out of the box (OOTB) capability for creating workflow timers, still, there is a way to create a workflow timer using SPD workflows.

2. Scenario: In our simple scenario we want our example workflow to run every five minutes. Now, given this is not a very realistic scenario, most users will want the workflow to run once a day, week, month, etc. However, once we have designed the mechanism to run a workflow every five minutes, it’s a simple matter of changing the pause delay to increase that to any duration needed.

3. Explanation: Essentially we will have two workflows, running on two separate lists; one workflow, “Endless Loop”, creates an endless loop and the other workflow, “Delay”, one creates a delay or timer. The logic for the Endless Loop workflow looks like this:

a. Calculate (increment) the loop counter

b. Update the item in the Delay workflow with the counter value

c. Log the Event

The logic for the Delay workflow looks like this:

a. Calculate (increment) the loop counter

b. Pause (Delay) for a period of time

c. Update the item in the Loop workflow with the counter value

4. Overview: To build a Workflow Timer we will need two SharePoint lists, and two SharePoint workflows. The first list will contain our Loop workflow, and the second list will contain our Delay workflow. For obvious reasons we will call the first list “Endless Loop” and the second list “Delay”. To create the lists follow these steps:

a. From the SharePoint site create a custom list, call it “Endless Loop”

b. From the Loop list add a custom Number column, call it “Counter”, set it’s number of decimal places to 0 and it’s default value to 1, finally add it to the default view

c. Create 1 item in the list and title it Endless Loop

d. From the SharePoint site create a custom list, call it “Delay”

e. From the Delay list add a custom Number column, call it “Counter”, set it’s number of decimal places to 0 and it’s default value to 1, finally add it to the default view

f. Create 1 item in the list and title it Delay

To create the workflows for these lists follow these steps:

a. From SharePoint Designer create a workflow that is associated with the Endless Loop list, call it “Endless loop”

b. Set this workflow to be started manually and to start automatically when an item is changed

c. Create 1 step for the workflow, call it “Endless Loop”

d. Create a condition – If Created By is not empty – this means the workflow will always run

e. Add a Do Calculation item, set it to Calculate Endless Loop: Counter plus 1 (output to Variable: calc), the configuration should appear as below:












f. Add an Update List Item activity, update the Delay list, Delay item, changing the value of Counter to the workflow calc variable’s value, configure the activity as shown below:


















g. Add a Log to History List activity, configure the log to write “Endless Loop – Ran”

h. The complete workflow configuration should appear as below:








i. From SharePoint Designer create a workflow that is associated with the Delay list and call it “Delay”

j. Set this workflow to start automatically when a item is changed

k. Create 1 step for the workflow, call it “Delay”

l. Create a condition – If Created By is not empty

m. Add a Do Calculation item, set it to Calculate Delay:Counter plus 1 (output to Variable: calc), the configuration should appear as below:











n. Add a Pause for Duration activity, pause for 5 minute (this is only a place holder testing value that can be increased to whatever value is needed for the delay)

o. Add an Update List Item activity, update the Endless Loop list, Endless Loop item, changing the value of Counter to the workflow calc variable’s value, configure the activity as shown below:




















p. Add a Log to History List activity, configure the log to write “Delay – Ran”

q. The complete workflow configuration should appear as below:









5. Test the Workflows: To test the workflow we only need to start the Endless Loop workflow from our item in the Endless Loop list. After a while we will see that the two workflows will keep initiating each other forming an endless loop.

6. Now Add Your Activity: Now you only need to add your activity to the loop, the best location being right after the Pause for Duration action in the Delay workflow. Once you add your activity here, it will run at the interval that you set in your Pause for Delay.

7. References: Use SharePoint Designer to Email Daily Task Reminders

36 comments:

kobymaster said...

Tom, great post! I'm new to this but things are starting to make more sense thanks to your post. I'm trying to integrate this timer loop with a for each loop. But, I do have a few questions about this workflow. In step 4.d., you're creating a custom list called delay. However, in 4.e., you start off with "From the Loop list..." Do you have a "Counter" field in both lists? Also, when creating workflows step m. adds a calculation "Add a Do Calculation item, set it to Calculate Endless Loop:Counter plus 1" Could this possibly be a typo? Thanks again for your posts...

kobymaster said...

To rephrase the second question, I'm asking if "Add a Do Calculation item, set it to Calculate Endless Loop:Counter plus 1" should say "Add a Do Calculation item, set it to Calculate Delay:Counter plus 1"

kobymaster said...

Tom. Never mind my questions. This worked perfectly. I figured out using the screen shots you provided. Now to implement the For Each Loop. Thanks again!

Tom Molskow said...

Hey Kobymaster,

Yes, you are right, good catch, I must have used some of the field names from a different project - I will correct those.

Yes, there should a counter field in both list.

I appreciate all these types of comments because I want the instructions to be clear for everyone.

I'm glad it work for you! I hope For Each does as well!

Thanks!

Tom

kobymaster said...

Tom, the For Each process worked perfectly. I also connected the four workflows together and everything is running superbly. GREAT BLOG!!!!! In case you're wondering, I'm using these to update a list with Today calculations. These loops have automated the process. Thanks again!

Tom Molskow said...

Hey Kobymaster,

That's awesome! I'm really glad everything work out for you!

Thanks!

Tom

Anonymous said...

I was able to create the Endless loop workflow and it worked great but the Delay workflow I created keeps stopping at the Pause for Duration area. Any ideas on what the issue might be?
This is what my workflow looks like:

If Current Item:Created By is not empty

Calculate Current Item:Counter plus 1 (Output to Variable:calc)

then Pause for 0 days, 0 hours, 5 minutes

then Update item in Endless Loop

then Log Delay ran to the workflow history list

I am using SharePoint Designer 2010 with SharePoint 2010.

Tom Molskow said...

Hello,

Are you getting any error messages in the workflow history log? If so please post those.

Also, a good technique for error catching is to add a log before and after the action that may be causing the error, that way you'll know if the problem occurred before or after that activity in the workflow.

It's also possible the error is with the update action that occurs after the pause. Please put a log after that step as well.

Once you have checked the workflow history list and created the additional logs run the workflow again and LMK if there are any additional issues.

Thanks!

Tom

S Spencer said...

Tom,
Can you post a little more information about how to add my activity? I need to compare a field in a list and send out email reminders based on whether or not my calculating date field matches [Today].

I added another Workflow step with an "If status is Active" and then %variable date field% equals today, then send the email, etc. I ended the first Delay step with the pause... The two workflows are triggering each other, but not running my date check/send email. Help? :(

Sherri

Tom Molskow said...

Hey S,

First I apologize for the long delay; I was in crunch mode on a big project and could not find the time or energy to respond.

Ok, so for your question, I would try these steps:

1) Make sure your added step works - try the same step in a throw away manual workflow, if it works there then we eliminate that as a possibility

2) Add a log just before the pause and use it to capture the value of the date field

3) Your activity can go in either workflow, but it should go before either of the pauses

If possible please post the design of your workflow, just a simple text version will do. Also, it would help to know more about your list. If you are using a calculated column for the date field then that could be the issue, calculated columns only update when their list/library items are updated.

Anyway, I will be more available these next few days so I hope I can help.

Thanks!

Tom

S Spencer said...

Tom,
I took a break from working on this, so your comment is perfect timing!

I need to check a list called "Contractor Terminations." I need to send reminder emails to the manager 14, 7, and 1 day before the termination date, so they can update the date if necessary (they would update the list, and it recalculates), and then send emails 1 day after the termination date and 2 days after to shut off access.

I created calculated date fields, and then told the workflow to check all the fields and if they match [today], then send the appropriate email for each option.

My problem might've been that I created a 2nd workflow step for my activity, after the pause in the first step. I'll change that.

Is there a better way to do what I'm trying to accomplish?

Anonymous said...

Tom,
Did you ever get my comment back. I wrote a long one last week and then joined the blog - wondering if it disappeared because it hasn't appeared..... :(

Tom Molskow said...

Hey Sherri,

Again, I apologize for the long delay in my response, I have been very busy with a SPS 2010 migration project.

To answer your question, I do agree with your new design. If I understand your response, you have simply had the Timer Loop workflow check each of those date conditions from the calculated columns, all in one step rather then multiple steps, and then if the condition is true (and only one can be), your workflow will send the appropriate e-mail. That's exactly how I would do it and that should work well with the timer loop.

I hope that helps!

Tom

S Spencer said...

Thanks Tom,
I think I have a bigger problem. I need the workflow to KEEP looping and checking each calculated field on each item each day. It will send a total of 4 warning emails before the last email that shuts off access and turns the status to Terminated (on each list item). I don't know how to get it to keep checking. Once it finds one condition, it sends the email and then the workflow completes. Maybe this is just a bit over my head. :(

Thanks for you help though.

Tom Molskow said...

Hey Sherri,

You should have two workflows, each configured to start on change. Each of the conditions in your first workflow sends an e-mail when it is true, and each condition in your first workflow should also finish by changing a field in an item of the list of the second workflow. Now the second workflow starts, pauses for a day, and then changes a field in an item in the list of the first workflow, which causes the first workflow to run again. Now you have a continuous loop that runs once a day.

I hope that helps!

Tom

Anonymous said...

This a brilliant solution, and it worked great, thanks for the step by step guide really helped set it up. Some may already know this but you can hide these lists from browsers after you have created them by looking the properties in sharepoint designer so nobody else can open these lists, thank you so much for taking the time to give a solution :-) Kris

Tom Molskow said...

Hey Kris,

I'm glad I could help! I hope to be back posting new solutions for SPS 2010 workflows soon (I've been very busy with a SharePoint Migration project).

Thanks!

Tom

Anonymous said...

This is a good solution, but unfortunately with Sharepoint 2010 it only runs 5 times. Any ideas on how to change it to keep running?

Anonymous said...

Good solution, but how would you run in in every item on a list to update calculates fields???

Tom Molskow said...

Hello,

If the timer quits after a number of iterations try this fix - set the pause duration in the "Delay" workflow to a higher duration - example 1 to 2 hours. Then reduce the timer in the "Endless Loop" workflow by the same duration. Save, republish, and test. Please LMK if that works.

I think in 2010 there are some changes with the rehydration settings and that may be causing the issues. There are ways to verify this so if you want to know more LMK.

Check your logs as well - are the timer workflows trying to start and then failing, or are they not starting at all? Also check the last run date/times for the workflows, this may shed some light on whats' going on. Finally, ensure the workflows are updating each other's fields properly.

I hope that helps!

Tom

Tom Molskow said...

Good solution, but how would you run in in every item on a list to update calculates fields???

Combine the Timer Job solution with my For Each loop Solution located here. The Timer Job starts the For Each Loop, the For Each Loop runs on every item in a list to update the calculated fields.

I hope that helps!

Tom

Cam said...

This is great, except its not working on Sharepoint 2010. Any ideas on a solution to that? I need to run a workflow every night to check on certain items and send out reminder emails.

Thanks,

Cam

Tom Molskow said...

Hey Cam,

I have one that has been running in my environment for months with no issues. Can you tell me more about your design, any error messages, your environment?

Thanks!

Tom

Cam said...

I set it up as you suggest, but it stops after 5 loops. Is there a way to keep it going?

Tom Molskow said...

Hello Cam,

You may be using SPS 2010, if so then please see my other blog located here (this solution only works with SharePoint 2007) -

http://sharepointgypsy.blogspot.com/2011/11/create-for-each-loop-for-workflows.html


Thanks!

Tom

Todd Wilder said...

I have 5 years of experience in SP customizations / workflows and I needed to develop a daily workflow. None of these "workflows calling workflows" methods work for long periods of time or after a catastrophe. The only really enterprise way to do this is to use Information Management Policy with Expiration and a mirror list where items are deleted and maybe recreated every day.

Tom Molskow said...

Hey Todd,

I have had a very different experience, the timer loop workflow I deployed has continued to work through system reboots, power outages, and a migration from SharePoint 2007 to SharePoint 2010. It's run for well over a year and still going strong.

Still, I will try your method as well, and then post about it.

Thanks for the suggestion.

Tom

TND said...

Everything works fine when you have one item in each list but when I add additional items in the endless list, and have it looking up on title Endless -- it doesnt' run properly.. I suspect b/c delay is pulling counters from all these different items in the endless list. What do I need to do differently to have this actually work in with an endless list with multiple items?

Tom Molskow said...

Hello TND,

You could create the two workflows needed for the timer loop and then create a set of For Each workflows as I describe in my two posts located...

here (SPS 2010) - http://sharepointgypsy.blogspot.com/2011/11/create-for-each-loop-for-workflows.html

and here (MOSS 2007) -
http://sharepointgypsy.blogspot.com/2011/01/sharepoint-designer-workflows-2.html

Please look over those blogs and then LMK what you think.

I hope that helps!

Tom

Ganesh Thirunavukkarasu said...

Hi Tom,

I have created two workflows, for sending daily reminder emails, however i set the time as 5 minutes (just for testing purpose), only three mails are sending. after that both the workflow gets completed. what could be the problem, kindly suggest some idea..

Thanks in advance
Ganesh

Tom Molskow said...

Hello Ganesh,

You may be using SPS 2010, if so then please see my other blog located here (this solution only works with SharePoint 2007) -

http://sharepointgypsy.blogspot.com/2011/11/create-for-each-loop-for-workflows.html


Thanks!

Tom

Rajendhiran Muthusamy said...

Hi Tom,
Thank you very much for your valuable post.

With Regards,
Rajendhiran Muthusamy

vmrao said...

Hi Tom,
I need to implement a solution which is a combination of your Timer loop and For Each solutions. The For Each solution is working great. But I am not sure how to combine both solutions into one. Would the Data List serve as the Endless Loop List too when we are combining both the solutions ? How do I trigger the workflow on 'For Each Loop List 1' from the Timer Loop Workflow ? Can you please list the steps needed to combine both the solutions.

Tom Molskow said...

Hello Vmrao,

You need to add another Update List Item action to the Delay workflow. In the Update List Item update the first item in the Loop 1 List.

Please let me now if this works for you, I'll check for updates tomorrow night.

I hope that helps!

Tom

Ghim said...

Hi Tom,

May I know if the above works for SPD 2013? I created 2 lists & workflows following your steps above, but when I start either one of the workflows, it only updates the counter in the other list but fails to trigger the workflow after that. Not too sure what went wrong.

Appreciate if you might be able to provide some advice on this.

Thank you!

Tom Molskow said...

Hey Ghim,

No, I would not use this method in SP 2013, there are better ways to do this in SP 2013, and I will eventually post an article about that.

Thanks!

Tom