Thursday, March 3, 2011

Triggering workflows on lookup fields

When you create a "choice" item associated with a SharePoint list, it is relatively easy to associate that choice with a workflow. You simple create the workflow, and in the condition portion, you select the choice field you created, and in the value column, you can select the choice that you wish to trigger the workflow on, for example:

If Current Item:Status equals (1) ready
Email steveburgess10@gmail.com

This is nice and easy. But what about the case where a lookup is used to populate the choice dropdown menu? If I have a seperate list that I wish the lookup to use, as shown below, then the approach needs to be different.



Comparing the lookup field value with either the ID of that field or the text of the field both fail, for example:

If Current Item:Dynamic Status equals (1) ready
Email steveburgess10@gmail.com

does not work. Even using the ID of the field (retrieved from the lookup list) does not work, for example:

If Current Item:Dynamic Status equals 1
Email steveburgess10@gmail.com

But we are close. The ID is the key. Here are the steps for correctly creating a lookup field that can trigger a workflow.


1. Within you SharePoint environment, navigate to the list you wish to associate the workflow to.
2. Click the "list" tab and then the "list settings" button in the office ribbon.

3. Click the link to your lookup field listed in the "Columns" section of the list settings page.

4. KEY STEP. As shown in the below screenshot, in the section titled "Add a column to show each of these additional fields", put a checkmark beside the ID value. This value is what will be tested against in our workflow.





















5. Open SharePoint designer and open the site that hosts the list you wish to associate the workflow to.

6. Choose the "Workflows" selection in the site objects collection, and select "List Workflow" from the choices provided.

7. Name and describe your workflow.

8. KEY STEP. Click the "condition" button at the top of the workflow page and select "if any value = value". NOT "If current item = value". While they result in identical looking code, using the second choice WILL NOT WORK. This will create your condition logic:

If field equals value

9. In the "field" selection that you wish to match against, choose the lookup list name:ID field. In my example, I am looking at the status field, so I am looking for the field value "Status:ID".

10. For the value, enter in the ID value that corresponds to the value of the lookup item you wish to trigger the workflow from. So, in my example, I know that the status ID of the status I am interested in is 4, so my condition looks like this:

If CurrentItem:Status:ID equals 4

Email steveburgess10@gmail.com

11. Add any fields or other information to the email that is generated, save and publish your workflow.

Associating workflows with lookup fields is not done in the same way as choice fields, as you cannot compare the selected values in the same way (such as a string compare). By extracting the ID value from the source list and including it in the destination list, you can compare that ID value to an integer value to successfully trigger the workflow.


1 comment: