Tuesday, May 10, 2011

Nintex 2007 - Query Workflow Tasks list for approval comments

When using the Request Approval action in Nintex workflow, you can use the Approver Comments field for notifications. However, it's pretty ugly when there are multiple approvers, e.g.:
Joe Bloggs (Approved) 10/05/2011 8:05 a.m. - 10/05/2011 8:05 a.m.
(DOMAIN\jbloggs) This is good to go!
Fred Dagg (Not Required) 10/05/2011 8:05 a.m. -
What people find is that they don't want all that extra information, just the comments.

Nintex provides a tutorial for doing this by querying the Workflow Tasks list directly for the ApproverComments fields and putting them into a collection variable. The key is to set up the filters for the query:

  • Workflow Item ID: ID
  • Workflow List ID: List ID
This is all well and good if there is only ever one approval workflow performed on an item. What happens if the approval workflow is run multiple? Querying the Workflow Tasks list will bring back the comments from all instances of the workflow. By default, there isn't a way to differentiate between the workflow instances because there isn't a "Workflow Instance ID" field in the tasks list.

So how can we get around this? It's quite simple - create a unique task name for each instance of the workflow and perform the query based on this unique task name:
  1. Calculate a date: Use date when action is executed and include time. Store the ISO 8601 date string into a text workflow variable, e.g. Workflow Start ISO
  2. Request approval: Set the task name to "XYZ Approval - Workflow Start ISO", i.e. include the workflow start variable in the task name.
  3. Query list: Add filter rule for Title = "XYZ Approval - Workflow Start ISO", while still including the original filters from above.
This will bring back the ApproverComments for all tasks relating to this workflow instance. You can add an additional filter rule where ApproverComments is not null to cut down the results (e.g. if there are multiple approvers, but you are only interested in the first response, the others are blank).