SharePoint Designer 2013 Approval Workflow with Comments

Adding Comments to a SharePoint 2013 Approval Workflow

I needed the ability to capture comments from Approval Tasks in a workflow.  This has changed greatly in 2013 workflows.  Everything is more modular and as such must be built somewhat from scratch.
  • SharePoint 2013 workflows can become corrupt.  This may have been fixed by a patch, but please refer to a previous post before creating any 2013 workflows in Designer: http://www.ericjochens.com/2014/12/sharepoint-2013-workflow-observations.html
  • SharePoint 2013 does not have a built-in approval workflow, nor does a built-in task item include comments.  I solved this by building my own approval workflow, and using a custom content type to include a comments field.
  • A SharePoint 2013 Workflow will not store the Task ID when a task is created, so you cannot do a lookup into that task list to grab comments or any other field.  I solved this by creating a separate workflow in the task list to read the Related Items column, and update the originating list item with comments.  It's reverse, but it works!

Background:

I have a list which holds details for a business process.  The approval process is serial, going through each approver until complete.  Users need to be able to leave comments during each approval task and have these reported back during subsequent tasks.

Steps:

  1. Using the custom list, I called it "AFE", create a new SharePoint Designer 2013 Workflow, I called it "2013Approval".
  2. Create a new Workflow Tasks list for this workflow, automatically named "2013Approval Tasks".  This is important.
  3. Create a new site column, Multi-Line Text, called "Workflow Comments".
  4. Create a new Task Content Type, inherited from the "Workflow Task (SharePoint 2013)" content type.  (This must inherit from this content type or it will not be available for selection later in the task process).  I called it "Approval Task".
  5. Add the site Column created in step 3 to this content type.  We now have a task content type with a comments field.
  6. Add a column to the parent list, "AFE", Multi-Line Text, Called: "Consolidated Comments".  Turn on versioning for the list and set this column to append changes.  This will add new comments on new lines.  You can also choose to have a separate field for each approval stage if you wish to show the individually.
  7. Attach the content type "Approval Task"created in step 4 to the custom task list used by the workflow "2013Approval Tasks", created in step 2.  (List settings -> Add form existing site content types)
  8. In the parent list workflow "2013Approval", add an action to assign an approval task.  "Assign a Task".
  9. Edit the task properties.  In the section Outcome Options, change the Task content type to the one you created in step 4.
  10. Using SharePoint Designer, create a new workflow for the custom tasks list you created in step 2.  I called it "Update AFE Comments".  This will read in the related items column, extract the parent Item ID, so we can update that item with the comments from the task. Set the workflow to run on any Change.
  11. Create the following variables:[stringRelatedItem | string],[indexStartWebId | integer],[idParentItem | String],[numberLengthItemId | Number],[commentWithRole | string] (Note: the commentWithRole allows me to add the Assigned TO: before the comment.  If you are writing to individual comment fields for each task, then you may omit this variable because you can simply write the whole comment without adding text)
  12. I start with a test condition to see if the RelatedItems field is Not Empty, so as to reduce errors.
  13. Set the variable stringRelatedItems.  Then I log it to the history list for tracking purposes.  (I cannot recommend enough using the history list to report values and verify what is happening.  For example that results saved to variables are what you expect and in the format (data type) you expect.)  Step 14 shows what is stored in the stringRelatedItems field:
  14. {"ItemId":1,"WebId":"0a1b956f-fdd5-47c3-ac25-a50079c107dc","ListId":"b4ddc283-3a17-46fb-b5f5-14bd2a95bdf9"}  (Note: if you view this with PowerShell or SharePoint Mnagaer 2013, there will be brackets outside this, but they are not stored in the variable)
  15. Next we need to do some string indexing to find the length of the ID in the stringRelatedItems string.  The ItemId will always start at position 11, but it could be an unknown number of digits, example ID=1 or ID=1232.  I run an action, Extract Substring from Start of String, on the stored variable to find location where "WebId" begins.
  16. Add an action, Do Calculation.  We need to subtract 13 from the variable indexStartWebId, because this is the number of characters (11) at the start of the string before the ItemID and the trailing (2) characters after the ItemId and the start of the WebId that we searched for.  Do your own Excel calculations if you want to confirm it.
  17. Next we do an action, Extract Substring of String from Index with Length.  This will pull out the number of characters of the ItemId.  Ouput it to the variable idParentItem.
  18. Next I log that ID to the History List for Tracking.
  19. The next step is to update the parent list item with the comments.  I add my own action to build a concatenated string with the assigned to field so I can write that back into the Consolidated Comments field in the parent item.  You can choose how you wish to report back the comments.
  20. Next is the Update List Item action.  We can do a lookup on the originating list, AFE, with the ItemId we saved as a variable.

  21. Then I log the output to the History List for Tracking.

Full Workflow:

Update AFE Comments Workflow

Result:

Now when a user edits the task, the comments are written back into the item.

  1. Running the Approval workflow on the AFE list:
  2. Editing the task and adding comments:
  3. Approving the task, the comments now show up in the parent item:

Comments

  1. Excellent post Eric. I had to use Pause for duration before the workflow sends out the rejection/approval comments.

    ReplyDelete
  2. Hi Eric very good Description but I have a Problem and maybe you can help me to solve this Problem.

    I have a workflow (Name approval) on a Liste that starts a process when I add a new item. As part of this it creates a task and emails to the users.

    Then I have a second workflow (Update Data) on the Task list. He starts a process when I Change the Task.

    That works fine.

    The Problem is, that on my Task there is no related item (the field is emtpy) but I need the ID for my process.

    Why is this related item not being assigned?

    I read that when i create a Task that the related item will fill automatically

    Attached you'll find any pictures.

    Yes I use the german Vision and if you need a Translation ask me:)

    Thanks for helping.

    Marco

    ReplyDelete
  3. Hi Eric very good Description but I have a Problem and maybe you can help me to solve this Problem.

    I have a workflow (Name approval) on a Liste that starts a process when I add a new item. As part of this it creates a task and emails to the users.

    Then I have a second workflow (Update Data) on the Task list. He starts a process when I Change the Task.

    That works fine.

    The Problem is, that on my Task there is no related item (the field is emtpy) but I need the ID for my process.

    Why is this related item not being assigned?

    I read that when i create a Task that the related item will fill automatically

    Yes I use the german Vision and if you need a Translation ask me:)

    Thanks for helping.

    Marco

    ReplyDelete
  4. Hi Eric, did you receive my comment? Marco

    ReplyDelete
  5. Hi Eric, thank so much for the thorough post! I cannot say how much it has helped me. I have successfully implement your workflow, but string in the "Consolidated Comment" column for AFE picks up as {"__metadata":{"type":"Collection(Edm.Int32)"},"results":[173]} "Superb!". Is there any way I can just pull the string itself into the column?

    Thank you for your help!

    ReplyDelete
  6. Hi Eric, just wanted to let you know, I figured it out. For some reason, my workflow didn't like the "assigned to" in the "then Set Variable: commentWithRole to %CurrentItem:Workflow Comment%. Once "assigned to" was taken out, all is well. Thanks!

    ReplyDelete
  7. Thanks Eric this is a solid post!

    ReplyDelete
  8. Hi. I really enjoyed your post. However I get it to work correctly. I find it adds

    username (7/11/2017 3:01 PM): {"__metadata":{"type":"Collection(Edm.Int32)"},"results":[43]}:

    at end of the list. this keeps happening and I cannot get stop it. Can you offer any advise?
    Thank you.

    ReplyDelete

Post a Comment

Popular posts from this blog

Change SharePoint server hostname and Web Application Names

The Timer Service Failed to Recycle