How to wait for a change in any list, wait for multiple field changes in the current item (SharePoint Workflow) - SharePoint Development & Administration + InfoPath

Saturday, November 28, 2009

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.

This can be achieved by configuring an On Change workflow to stop if the required criteria is not met, instead of using "Wait for field Change in the current item" actions in a Parallel fashion which limits functionality. The first step in the workflow can contain as many conditions as required, using the standard tests against fields in the current item, or a field in an item in another list on the sameSharePoint site. If the conditions are not yet met, the workflow will stop. A new instance of the workflow will initiate every time data in an item is added/updated, and will continue only when all of the required criteria is satisfied. The action for all conditions in the first step will be to stop theworkflow and log a message.

Requirements


Create the workflow using SHarePoint Designer


  1. Load the SharePoint site using SharePoint Designer (File -> Open Site...)
  2. From the File Menu, select New to open the "New" dialog.
  3. From the SharePoint Content tab, select Workflow and then Blank Workflow
  4. Press ok to create the workflow.
  5. Set the name of the workflow
  6. Select the option to start the workflow when a new item is created, and when an existing item is modified. Un-check the option start the workflow manually if required. (On Change, On Create)

In the first step of the workflow, create a separate condition/action branch for each of the tests which need to be completed before the workflow can continue. For example, if you need the workflow to wait for a task in a separate list, but associated with the current item to be completed, you would create a condition which tests if the Task Status field of the task item is marked as "Completed". If not, the action will be to stop theworkflow . If you need to wait for multiple fields in the current item to change before continuing, a separate condition/action branch should be created for each, or at least a separate condition. Simple tests on yes/no fields can usually be added to the same branch without loosing functionality. Using a single branch with multiple conditions will also improve performance of theworkflow if there are a large number of conditions being checked each time the workflow starts.

When the conditions in the first step of the workflow are satisfied, the workflow can continue to the next step. In Step 2 of the workflow, a [workflow_started] field is set in the current item to indicate that an instance of the workflow is running. Any new instances will not run, as the [workflow_started] field in used as one of the conditions in Step 1 to determine if the workflow can start. Only one of instance of the workflow should progress to step to and beyond.


Example:

Workflow Step 1


Condition
  • If [Task Status] (separate list) does not contain "Complete"
Action
  • Stop workflow, log: "Task X not yet complete"




Condition
  • If [Project Code] (current list) "is blank"
  • OR: [Assigned To] (current list) "is blank"
  • OR: [workflow_started] (current list) equals "yes"
Action
  • Stop workflow, log: "Project details required"


Workflow Step 2


Step 2 and onwards will only run if all conditions were met in the first step. The first action which should be performed in Step 2 will be to set a [workflow_started] field in the current item to "yes" which indicates that an instance of the workflow is running. As only one instance of the workflow should run, the [workflow_started] field is used as a condition which will stop new instances from continuing past Step 1. The final step in the workflow will be to set the [workflow_started] field back to "no", or a separate [workflow_complete] field to "yes". If using a [workflow_complete] field, you may need to include a condition in step one of the workflow to prevent new instances of the workflow starting if one is running, or is already complete.

A looping workflow can also be created using this technique, as the final step of the workflow involves updating data in the current item (setting the [workflow_started] back to "no"), which will trigger a new instance of the workflow. If your SharePoint environment is running at the Service Pack 2 level or higher, an "On Change" workflow will not start a new instance of the same workflow when the current item is updated. To enable looping functionality when Service Pack 2 is installed, you will need to use an intermediate list which is used to trigger a newworkflow in the main list. See SharePoint Looping Workflow - How to: Loop through and process all items in a list for more information about how to configure looping workflows with Service Pack 2 installed and how to configure a Looping Workflow. For more information about looping workflows, and some scenarios where that can be used such as a looping timer workflow, see article (Looping) Timer Workflows Using SharePoint. The article describes how a looping workflow can be used to send periodic reminders or similar when certain criteria has not been met.


The example above has two condition/action branches, one which tests if a task in a separate list is complete, and the second tests if the "Project Code" and "Assigned To" fields of the current item have been filled. When the conditions are satisfied, theworkflow continues to Step 2 where a field is set in the current item to prevent new instances from starting. Step 3 until the final step will include the main functionality of theworkflow. The final step will involve resetting the field in the current item to indicate that the workflow is no longer running, or complete.

Note: You will need to create a relationship between the current list and the Task list, or develop some method of being able to select the correct task item from the task list. Alookup column in the task list pointing back to the current list can be a good way to link one or more task items to an item in a separate list. Alternatively, the ID of the Task item may be able to be stored in a field in the current item if known. When aworkflow needs to refer to the separate item, it can then find the item using it's unique identifier which will help prevent the workflow selecting the wrong item.

If you are storing the ID of a separate item in a workflow variable or a field in the current item, you will need to use the "List Item ID" workflow variable type, or a Lookup column to retain a relationship between list items. Workflows developed using SharePoint Designer should use the Lookup/List Item ID column/field data types to when selecting items using their unique identifier. In most cases, SharePoint Designer will not allow you to select an item from another list using the unique identifier used by SharePoint unless you use the Lookup/List Item ID data types.


Related Articles:

SharePoint Looping Workflow - How to: Loop through and process all items in a list
"The OOB workflow actions provided by SharePoint don't allow a loop to be configured explicitly. Instead, a number of workflows can work together to create a looping affect. Another limitation of Workflows in SharePoint using SharePoint Designer (SPD) is the inability to simply test if the user who initiated the workflow is a member of a SharePoint group, or to test if the value of a field in the item which initiated the workflow is equal to the value of a specific field from any item in a separate list."


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.

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.

Looping Timer Workflows
To have a looping timer workflow (eg. send a daily notification/reminder until a specific condition is met), the workflow must start when an item in the list is changed, and when it is created depending on the scenario.

SharePoint Workflow Errors: Descriptions, resolutions and Workarounds
The 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.

Service Pack 2 prevents an on-change workflow from starting itself

Share this article:
Stumble This Delicious
Delicious
submit to reddit
Facebook
MySpace
MySpace

2 comments:

Jordan London said...

Thanks for the great ideas!

Question: Do all instances of a particular workflow share variables or only those instances associated with the same list item?

DanielS said...

Hi, an actual "Workflow variable" will only be available to the specific instance in which is is created.

If you are using filed/columns in a list item to store the Workflow variable such as the [workflow_started] field in this example, all workflow instances initiated by a particular list item will have access to the same value in the control field. This assumes that you are using fields from the "Current Item". You could access this value in another list item as long as you have criteria that allows the item to be uniquely identified to share the variables with other instances (not only the current list item), but if would become less manageable if not planned carefully.

Thanks

Post a Comment