How to configure a workflow to start at specific time, then loop / repeat periodically (Daily, Weekly) - SharePoint Development & Administration + InfoPath

Monday, January 25, 2010

How to configure a workflow to start at specific time, then loop / repeat periodically (Daily, Weekly)

The following method can be used to start a workflow at a specific time, then repeat periodically (each day).

Periodic Workflow Processing (send daily email notification or reminder)
A useful implementation of this technique allows you to configure workflows to run as background tasks at a specific time instead of being triggered by a user manually, or after creating or modifying a list item. This allows processing of data from SharePoint to take place during times when the SharePoint environment is not being used as well as performing tasks that take a long time to complete.
For example, this method can be used to configure a looping timer workflow to run at COB each day (5:00pm) to perform calculations and processing on a list item before emailing a reminder, or details to a user associated with the list item or document. When initiated for the first time, the workflow waits until the specified time (5:00PM) before executing workflow actions. After the workflow is complete, a secondary list item is modified, which in tern initiates a new instance of the workflow. As the initial time (and date) that workflow waited until to start would have passed by the time the workflow is started the second time, the workflow waits for a specified period of time instead (eg. 1 day) before completing the workflow actions. The result is a looping timer workflow that starts at the time specified, then repeats on a periodic basis (every 24 hours / daily) indefinitely, or until certain criteria is met.
As the repeating sequence only needs to be initiated once, this method can be used to build workflows to complete background processing to help minimise the impact on users, as well as improve usability and functionality of web applications created using components of SharePoint. See below for an example of a Looping Timer workflow used to complete processing as a background task at a specific time each day.

Looping Timer Workflow for Background Processing
When a workflow takes time to process but doesn't require user input or intervention, it is possible to configure the workflow to run as a background process to eliminate the need for users to initiate the workflow each time it is required, as well as allowing complex and timely processing to be completed "behind the scenes". As these workflows may take a long time to complete forcing a user to wait for the workflow to complete after manually initiating, or modifying / adding an list item to a list to initiate the workflow can result in frustration if there are long delays. Configuring workflows such as this to repeat periodically allows the workflow to start and finish in it's own time, eliminating the need for SharePoint users to initiate and control the workflow. Other workflows initiated by users can then refer to the data generated and stored by the looping workflow allowing them to complete in a short amount of time as no complex or in-depth processing is required to complete the operation.
The looping workflow sequence is initiated once, then repeats indefinitely or until a specified date if required. The workflow will complete the operations required for each iteration, then pause until the operation is to be executed next. This will mean that the workflow will continue to complete the processing without a user needing to manually initiate a new instance of the workflow. A common implementation of this technique may be to send a daily or weekly email report or reminder with details of a list item or data from many lists and items similar to the example above. Other scenarios where this can be utilised are times when workflows are used to process many list items, as well as perform calculations and other operations on the list items that require time to process. With scenarios such as this, configuring a looping timer workflow to automatically initiate and complete the processing at a specific time each day (or hour) eliminates the need for a user to have to initiate the workflow, as well as allowing the workflow to complete seamlessly to users in the background.

Example: Daily Sales Report Workflow using SharePoint
Calculate sales totals for the current day's invoices. Store the data in a separate "Reports" list item for the day.
A looping workflow may be configured to loop through each item in a SharePoint list (Invoices), then extract data from a specific field from each list item which is the added (concatenated) to a field in a secondary list item (Invoice Reports) for other workflows to refer to, or later use by SharePoint users. The workflow would process all Invoices which have not yet been processed, which will be any from the current day, as invoices from previous days will already have been processed.
A looping timer workflow can be configured to run at a specific time, then repeat every 24 hours to initiate the workflow(s) that loop through all items in a SharePoint list. The looping timer workflow will initiate the looping workflow to process invoices each day. A looping workflow can sometimes take a while to complete if there are a large number of items in the list. Configuring the workflow to be initiated by a user may not be appropriate as the delay may reduce the effectiveness of the workflow and increase user frustration. Configuring a timer workflow to initiate the workflow loop allows the processing (of all items in a list) to complete in isolation to workflows initiated by users that refer to the data generated when processing all list items.
Depending on how often the data changes and how important it is that the data is up to date, you may need to configure the looping timer workflow to initiate the workflow to process all items at shorter intervals (eg. hourly, every 10 minutes, etc.). As long as the list containing the items being processed is separate to the list item that stores the data from all items, it would also be possible to initiate the workflow loop to process all items when any of the items in the list are updated or added. If configured this way, an "On Change" and "On Create" workflow would initiate after a list item is added or modified, which would then make a change to the secondary list item to initiate the workflow loop sequence. The loop sequence would then process each item in the list until complete.
See below for a breakdown of a Background Processing Workflow that repeats daily using the above example.

Looping Processor / Looping Timer Workflow
Components (Daily Sales Report):
SharePoint List: Invoices
- Invoice 1
- Invoice 2
- Invoice 3
The "Invoices" list is the list which contains the items to be processed. Each item / "Invoice" ion the list is processed to calculate daily sales totals for each staff member.

Invoice Reports
- Report xxx (20-01-10)
- Report xxx (21-01-10)
- Report xxx (22-01-10)
This "Invoice Reports" list contains one list item for each day. The report data from each invoice item for the current day will be added to an Invoice Report item in this list once all have been processed to calculate sales totals.

Workflow Control List
- Temporary items used to initiate and control workflows and the looping functionality.

- Invoice Processing Workflow
- Initiate Invoice Processing Workflow
- Invoice Processing Timer Workflow

Looping Timer and Background Processing Workflow Diagram:
(Click to enlarge)

Workflow Details:

1. Invoice Processing Timer Workflow
This workflow will commence at a specified time (eg. 6:00 PM), then repeat every 24 hours (daily) after the first instance has completed. This looping timer workflow will be used to initiate the "Initiate Invoice Processing" (Looping Timer Workflow) at the end of each day by creating an item in the Workflow Control list with the title "Start Report Loop". This "Invoice Processor Timer Workflow" workflow will only need to be initiated once, as it is configured to automatically trigger a new instance of the workflow each day. When configured using SharePoint Designer, one of the first steps in the workflow would be to test if the current time is before or after the start date/time for the workflow (specified in a "start_date" field in the list item).

2. Initiate Invoice Processing Workflow: (Looping Timer Workflow)
The purpose of this workflow is to initiate an instance of the "Invoice Processor Workflow" on each item in the list being processed. The workflow will initiate when an item is added to the Workflow Control list with a specific title (eg. "Process Next Invoice"). The workflow will find the first unprocessed item available list item from the Invoices list, and update a field which indicates that it is being processed ("Processed" = no/false). Updating the field will initiate the On Change workflow (Invoice Processor Workflow) to process the updated item.

3. Invoice Processing Workflow
Process an item from the Invoice List. A separate instance of this workflow will be initiated for each item in the list each time the looping workflow sequence is executed. Updates the item from the invoice Reports list with data from each Invoice (daily totals: sales, turnover, profit, sale type, compute staff rank based on total profit, etc.).

Workflow Control List & Workflow Triggers
The workflows associated with the Workflow Control List Invoice Processing Timer (1), Initiate Invoice Processing (2) ) are should be configured to start when a new item is added to the list. The first step of the Invoice Processing Timer (1) workflow will be to stop the workflow if the Title of the current item (item added to the list) is not equal to "Start Report Loop". This will mean that other items added to the workflow control list with a Title that isn't "Start Report Loop", won't initiate the Invoice Processing Timer (1) workflow unnecessarily. The Initiate Invoice Processing (2) workflow should have a similar configuration for the first step: Stop if the Title of the current item is not "Process Next Invoice". This will mean that only items with this title will continue into the remain workflow steps.

The Invoice list will need a column/filed to flag if the Invoice has been processed or not. This could be a hidden boolean (true/false) field "Processed" or similar, which would be set to No/false as default when an Invoice is added to the list. The "Processed" field would be used by the Initiate Invoice Processing (2) workflow to find the next Invoice item to be processed. The unprocessed Invoice item found should then be updated by setting "Processed" to yes/true, which will then initiate the Invoice Processor (3) workflow for that Invoice. The final step of the Invoice Processing (2) workflow after initiating the next Invoice Processor workflow in the sequence can be to remove the current item ("Process Next invoice"), as t is no longer required. The final steps of the Invoice Processor (3) workflow should be to create a new item in the Workflow Control list with the title "Process Next Invoice", which will re-initiate the Initiate Invoice Processing (2) workflow creating the looping functionality, then set "Processed" to yes/true.

As the workflow is scheduled to run every 24 hours, items in the Invoices list from previous days will already have been processed by previous instances of the workflows. Only invoices added to the Invoice list since the last time the Report workflows were run, which will be any from the current day.


SharePoint Looping Workflow - How to: Loop through and process all items in a list
To use SharePoint workflows to loop through all items in a list to perform calculations, process and update metadata values, or concatenate the value of a specific column in all list items into a single string.

How to get an SPD Workflow to Run at a Specific Time – Employee Vacation Reminder: Part 5

(Looping) Timer Workflows Using SharePoint
Looping timer workflow (eg. send a daily notification/reminder until a specific condition is met.

How to wait for a change in any list, wait for multiple field changes in the current item (SharePoint Workflow)
This article describes techniques which can be used when developing workflows in SharePoint using SharePoint Designer. They allow a workflow to wait for a field change in an item in another list, or to wait for multiple field changes in the current item before continuing.

SPD Workflow - Test if value exists in any item in SharePoint list - create new item if not found
SharePoint Designer Workflow: How to test if a specific value exists in any item in a list without the workflow stopping due to a "List item not found" error if no items exist with the required value.

SharePoint Workflow Errors: Descriptions, resolutions and WorkaroundsThe following table lists some of the common errors that can occur in a Workflow developed using SharePoint Designer. A short description of some of the likely causes for each error are also provided.
Share this article:
Stumble This Delicious
submit to reddit

No comments:

Post a Comment