Help with script to pull Approvers approval from workflow

Community Alums
Not applicable

Hi All,

I am fairly new to the ServiceNow Development world. I am also an novice script-er   So here is my question/Issue:

I've made an order guide that contains 60+/- check boxes that each would flow to a different approver. On the back end i created a   workflow to track role approvals.This is for one of our in house ERP systems. I was able to make the workflow wait for all of the rejections and the approvals but I now have a requirement to note who approved what(which checkbox) in the worknotes of the task.   I was able to pull the approvers and their decision (rejected. no longer required, approved) but I am not sure how to get the actual Checkbox that corresponds to the approver.

I used this workflow(Massive I know!!):

OHM Corp workflow.PNG

This is what is currently created on the worknotes:

ohmtask.PNG

Here is the script I used to pull the approvers decision:

var rowCount ;

      var sc_approval = new GlideRecord('sysapproval_approver');

      sc_approval.addQuery('sysapproval',current.sys_id);

      sc_approval.addActiveQuery();

      sc_approval.query();

  rowCount = sc_approval.getRowCount();

  while (sc_approval.next()){

  var userid = sc_approval.approver;

  sc_user= new GlideRecord('sys_user');

  sc_user.addQuery('sys_id',userid);

  sc_user.query();

  if (sc_user.next()){

  var userName = sc_user.name;

  }

  var theTaskMessage ;

  if (theTaskMessage == null){

  theTaskMessage = sc_approval.state.getDisplayValue()+' '+userName+ ' \n';

  }

  else {

  theTaskMessage = theTaskMessage + sc_approval.state.getDisplayValue()+' '+userName+ ' \n';

  }

  }

task.work_notes = theTaskMessage;

So What   I need to know is how to pull the variable that was approved form the workflow.

Any help or advise would be greatly appreciated!

Thank you!!

1 ACCEPTED SOLUTION

so on the same script change this line to


theTaskMessage = sc_approval.state.getDisplayValue()+' '+userName+ ' \n';



theTaskMessage = sc_approval.state.getDisplayValue()+' '+userName+   ' ' + sc_approval.wf_activity.getDisplayValue() + ' \n';



and that should print the workflow activity name on the same line.


View solution in original post

16 REPLIES 16

yep that is a great way to start what script did you use to get the approvers?   i think in that same script we can grab the workflow activity name.


sorry if you are using wf.name you don't need get display value... either put in get displayvalue or .name you don't need both.


Community Alums
Not applicable

I used this to get the approvers name:




var rowCount ;


      var sc_approval = new GlideRecord('sysapproval_approver');


      sc_approval.addQuery('sysapproval',current.sys_id);


      sc_approval.addActiveQuery();


      sc_approval.query();


  rowCount = sc_approval.getRowCount();


  while (sc_approval.next()){


  var userid = sc_approval.approver;


  sc_user= new GlideRecord('sys_user');


  sc_user.addQuery('sys_id',userid);


  sc_user.query();


  if (sc_user.next()){


  var userName = sc_user.name;


  }


  var theTaskMessage ;


  if (theTaskMessage == null){


  theTaskMessage = sc_approval.state.getDisplayValue()+' '+userName+ ' \n';


  }


so on the same script change this line to


theTaskMessage = sc_approval.state.getDisplayValue()+' '+userName+ ' \n';



theTaskMessage = sc_approval.state.getDisplayValue()+' '+userName+   ' ' + sc_approval.wf_activity.getDisplayValue() + ' \n';



and that should print the workflow activity name on the same line.


just as an fyi when you are coding for a query like this find the approval record.. right click the header and click on "Show xml" this will show you ALL fields on the form... in that you will see the table name at the top right under the xml tag and each field on the record <Shown or not> with a tag.. for workflow it shows on one of mine for example...


<wf_activity display_value="Additional Approver">d043e6da2da1f1007ab33231f7a8d508</wf_activity>



this tells me the field is named wf_activity   it has a display value set.. and the actual value is the sys_id for that item... now   I know i don't have to use .name on it.. i can just use get display value.. and i know exactly what to put in the query/script.