How can I access the approvers from a workflow approval step in a run script later on in that same workflow?

jordi3
Kilo Contributor

Hello,

This is probably a stupid question, but I can't seem to find an answer in the documentation or in the community forums.

How can I access the approvers from a workflow approval step in a run script step later on in that same workflow?

Looking at the image below, I have a simple workflow in a London (p4) developer instance with an approval step, some timer thing, and a run script. 

find_real_file.png

If the approval step results in an approve, I want the run script to make an API call containing the name(s) of the approver(s) and the item that was approved.  For this particular post, I am more interested in getting the names of the approvers.  I currently have a script attached to the approval step that sets the approver name to a workflow.scartchpad variable, but it is hard-coded as you can see below.

gs.info('Inside of Test Approval');
workflow.scratchpad.jordi = 'admin'; // TODO: get approver dynamically
gs.info('Workflow Scratchpad Jordi: ' + workflow.scratchpad.jordi);
gs.info('Inside of Run Script');
gs.info('The transient workflow scratchpad jordi variable: ' + workflow.scratchpad.jordi);

gs.info('Making API Call...');
try {
	var r = new sn_ws.RESTMessageV2...
} catch(ex) {
	var message = ex.message;
}

gs.info('Processing Response...');

...

How can I dynamically set this scratchpad variable? Maybe something like workflow.scratchpad.jordi = workflow.getVariable('approvers');?

Please let me know if you have any ideas or resources.

1 ACCEPTED SOLUTION

Deepak Ingale1
Mega Sage

In the approver user activity, use generate the approval via script and there itself initialize the scratchpad variable

 

https://docs.servicenow.com/bundle/london-servicenow-platform/page/administer/workflow-activities/re...

https://community.servicenow.com/community?id=community_question&sys_id=b92e6942db2a1b805129a851ca96...

eg answer = [];



var gr = new GlideRecord("sys_user");

gr.addQuery("user_name", "able.tuter");

gr.query();



while (gr.next()) {  

   answer.push(gr.getValue("sys_id"));

}

workflow.scratchpad.approvers = answer;

 

Note: Please mark reply as correct / closed if it has answered your question.

 

View solution in original post

10 REPLIES 10

Deepak Ingale1
Mega Sage

In the approver user activity, use generate the approval via script and there itself initialize the scratchpad variable

 

https://docs.servicenow.com/bundle/london-servicenow-platform/page/administer/workflow-activities/re...

https://community.servicenow.com/community?id=community_question&sys_id=b92e6942db2a1b805129a851ca96...

eg answer = [];



var gr = new GlideRecord("sys_user");

gr.addQuery("user_name", "able.tuter");

gr.query();



while (gr.next()) {  

   answer.push(gr.getValue("sys_id"));

}

workflow.scratchpad.approvers = answer;

 

Note: Please mark reply as correct / closed if it has answered your question.

 

Thank you for responding.  This solution may work, but I am unsure due to hazy requirements and unfamiliarity with Service Now.  Do you mind providing me with a code sample of getting the approver(s) without setting the approver in the approval script?  Just for peace of mind. Something like current.form_variables.approvers or .users or .groups.

 

 

No, due to the fact that one request can have multiple approvers, those details are not stored in any of the field but are generated in sysapproval_approver table. However, field like "list" which can have multiple user as input could be an alternative just like watch list field on task table. 

 

I wont recommand to configure the another field, if you requires, you can use the script posted above OR can query to sysapproval_approver table and find the approvers which are related to current request and send notification to them.

I'm interested in querying the sysapproval_approver table, but am unsure on how to get the correct record.  I'm looking at this forum post, but it looks like the record I'm looking for doesn't exist.

Code Inside approval step:

var rec = new GlideRecord('sysapproval_approver');
gs.info(rec.get('sysapproval', current.sys_id)); // prints false