Approver comments on Task

Brian Lancaster
Tera Sage

I have a request to make it so that the approvers comments they put in the comments section.   How to I go about placing the approver comments into the task?

1 ACCEPTED SOLUTION

This is what our Approval Events (Task) business rule look like.   It was modified by the company that helped us setup ServiceNow.   How ever even if you want to use a group for approve you still have to use the approval user task in the workflow as that one works for both individuals and groups.   That is the only time that approval comments show up.



function checkRequest() {
      var task = current.sysapproval.sys_class_name;
      return (task == 'sc_request');
}


function checkSCTask() {
      var task = current.sysapproval.sys_class_name;
      return (task == 'sc_task');
}


var isRequest = checkRequest();
var isSCTask = checkSCTask();


if (current.state.changes() && current.state=='cancelled') {
      var event = "approval.cancelled";
      if (isRequest)
              event = "request.approval.cancelled";
      else if (isSCTask)
              event = "sc_task.approval.cancelled";
     
      gs.eventQueue(event, current, gs.getUserID(), gs.getUserName());
}


if (current.state.changes() && current.state=='requested') {
      var event = "approval.inserted";
      if (isRequest)
              event = "request.approval.inserted";
      else if (isSCTask)
              event = "sc_task.approval.inserted";
     
      gs.eventQueue(event, current, gs.getUserID(), gs.getUserName());
      updateTask(current, current.approver.getDisplayValue() + " requested to approve task");
}


if (current.state.changes() && current.state=='rejected') {
      var event = "approval.rejected";
      if (isRequest)
              event = "request.approval.rejected";
      else if (isSCTask)
              event = "sc_task.approval.rejected";
     
      gs.eventQueue(event, current, current.state, previous.state);
      updateTask(current, gs.getUserDisplayName()+ " rejected the task.", current.comments.getJournalEntry(1));
      notifyMyFriends(current);
}


if (current.state.changes() && current.state=='approved') {


      updateTask(current, gs.getUserDisplayName() + " approved the task.", current.comments.getJournalEntry(1));
}


function notifyMyFriends(me) {
      var friends = new GlideRecord('sysapproval_approver');
      friends.addQuery('sysapproval', me.sysapproval);
      friends.query();
      while(friends.next()) {
              if (friends.approver.toString() != me.approver.toString()) {
                      gs.eventQueue("approval.rejected.by.other", me, friends.approver);
              }
      }
}


function updateTask(me, journal, comments) {
      // if this is for a group approval, don't log this user action since the Group Approval Activity will handle the logging
    // if (!current.group.nil())
        //     return;
     
      // only log the user approval activity for workflows when specifically turned on
      // otherwise, we spam the approval history log when it is almost never desired to track via the approval history journal field
      var isWorkflow = !current.wf_activity.nil();
      if (isWorkflow && (gs.getProperty("glide.workflow.user_approval_history") != "true"))
              return;
     
      if (comments)
  journal += " Comments: " + comments;
     
      var task = new GlideRecord('task');
      if (task.get(me.sysapproval)) {
              if (isWorkflow)
                      task.setWorkflow(false);
             
              task.approval_history.setJournalEntry(journal);
              task.update();
      }
}



View solution in original post

44 REPLIES 44

I did some more digging on this.


Apparently there is a condition within the updateTask script which prevents the update in case the approval is triggered by a workflow.



In our case I modified the script to run anyway.



However, playing with that for a bit, it appears that the workflow adds all kinds of approval_history entries. E.g. if an approver is added to to an approval request you will also get an entry (for some reason twice) "user xyz added as approver" on the task.


Therefore we no longer add comments to the approval_history field but as a work note using a separate business rule.



Table: sysapproval;_approver


Type: Before


When: Insert and Update


Condition: !current.sysapproval.nil() && current.comments.changes()


Script:


addApprovalCommentToTask();



function addApprovalCommentToTask() {


      if (!validUser(gs.getUserID()))


              return false;



      if (gs.getProperty("glide.workflow.user_approval_history") != "true")


              return;



      var comment = 'Added comment to Approval record:\n' + current.getValue('comments');



      var task = new GlideRecord('task');


      if (task.get(current.sysapproval)) {


              task.work_notes.setJournalEntry(comment);


              task.update();


      }


}




function validUser(user) {


      if (current.approver == user)


              return true;



      // check if user is a delegate of the approver


      var g = new GlideRecord("sys_user_delegate");


              g.addQuery("user", current.approver.toString());


              g.addQuery("delegate", user);


              g.addQuery("approvals", "true");


              g.addQuery("starts", "<=", gs.daysAgo(0));


              g.addQuery("ends", ">=", gs.daysAgo(0));


      g.query();


      return g.hasNext();


}


The consulting company that we use came in a took a look at the business rule and realized as written it would never work because it was set to run the script After the action.     Since comments are copied to the journal the approval comments would not be there and were showing as blank they added code to copy the first entry from the Journal.



updateTask(current, gs.getUserDisplayName()+ " rejected the task.", current.comments.getJournalEntry(1));


Wendy Peterson
Giga Guru

I have a couple different scripts i use for approval comments this one puts it in the TASK Description or else change description to say work_notes and you can add it in there. It goes before any field items you have on the advanced feature of the Task. In the WF. Let me know if you have any questions.



Workflow - Adds Approval Comments in TASK for Worknotes and Descripton




//Adds approver comments to Description Area


var apv = new GlideRecord('sysapproval_approver');
apv.addQuery('sysapproval',current.sys_id);
apv.addQuery('comments','!=','') // add this to only find approvals that have comments on them
apv.query();



while (apv.next()) { // change if to while so it will go through all approvals with comments
task.description += "\nApproval Comments: from " + apv.approver.name + "\n" + apv.comments.getJournalEntry(-1); // add approver name here
}


Thank you Wendy! it worked like a charm!



Just a quick note the following line from your script




apv.addQuery('comments','!=','') // add this to only find approvals that have comments on them




is missing a ; (semicolon) at the end


Hello, Wendy. I just stumbled across this code and it worked great for what I was trying to do.   Could there be a similar application to bring over worknotes from other tasks to a new task?   If so, what would that look like?



Thanks for any help you can provide.