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

Ok, thanks. So this would add the approval comments to the RITM? How about worknotes from one task to the next? I assume it would be the same logic, just executing a GlideRocord on the sc_task table, in my case?


By putting that code in the Task through the Workflow see screenshot below - Where i have the code below you would add the modified code to the worknotes. I send some to worknotes it all depends on what the requirements are. So below would e Task 1 but again send it to current.work_notes instead of where I have task.description. Then when the Task 2 is created use task.work_notes = current.work_notes and it should pull the worknotes from the RITM. I know it would probably work better though if you sent that information to the description area cause that isn't journaled on the RITM it would look neater. So you could update the code instead to be current.description = "\n Task 1 Approval Comments: from " = apv.approver.name + "\n" = apv.comments.getJournalEntry(-1):



Then on Task 2 you would update it like task.description = current.description - or worknotes or whatever you are using. Make sense?



2015-07-13_12-31-29.png


Sorry to be so clueless, I am very new to development. Thank you for taking the time to help me on this. I really appreciate it.



On your screenshot, it appears that you are calling the approval table for approval comments. If I wanted to pull work notes from another task and insert those into the RITM worknotes, would I use a similar call? So for instance:



var apv = new GlideRecord('sc_task');


apv.addQuery(??Need help here??);


apv.addQuery('work_notes','!=',");


apv.query;




while (apv.next()) {


current.work_notes += "\n" + apv.work_notes.getJournalEntry(-1); }



Then, in Task 2 I would add:



task.worknotes = current.work_notes


Is there a way to set current.work_notes = task.work_notes after a task has been closed or updated?



If so, it seems like it might be easier to do that then just set task.work_notes = current.work_notes for Task 2.


I don't believe that would work but it would be a run script. Ignore the screenshot that was just an example of one of my tasks. That's how it's set up though. It's pulling the information from the approvers table. That's where you need to pull the information from cause it's the only place it's listed.



//Adds approver comments to Worknotes 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
current.work_notes += "\n Approval Comments: from " + apv.approver.name + "\n" + apv.comments.getJournalEntry(-1); // add approver name here
}