Ask for Approval via Script

Priyanka NR
Tera Contributor

Hi All, 

    The Requirement is to for Group approval via Client Script

OnChange of a Column value in a sc_task, it should ask for RITM approval for the specific group. 

I have created OnChange Script. Which is as below..

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

if (newValue == 'true'){

var tableSysId = g_form.getUniqueValue();
var ritmSysID = [];
var tableName = "sc_task";
var gr = new GlideRecord('sc_task');
gr.query('sys_id', tableSysId);
gr.query();
while (gr.next()) {
ritmSysID.push(gr.parent.toString());
}

 

var grApproval = new GlideRecord('sysapproval_group');
grApproval.initialize();
grApproval.parent = ritmSysID; 
grApproval.assignment_group = g_form.getValue('processorteamassignedgroup'); //Group name//
grApproval.approval = 'requested';
grApproval.wait_for='any';
grApproval.insert();


var grItemApproval = new GlideRecord('sc_req_item');
grItemApproval.addQuery('sys_id',ritmSysID);
grItemApproval.query();
if(grItemApproval.next()){
grItemApproval.approval ='requested'; 
grItemApproval.update();
}
}
}

 

Now I can see all the member of the group are getting approval request.

But, if one member of the group is either approving or rejecting then:

1.it is not reflecting as Approved/Rejected under group approval tab, it remains as requested.

2.it is not updating for Request Item State field as either Approved/Rejected, it remains as requested.

3.it is not updating as 'No Longer Required' for other members of the group, it remains as requested.

I have attached screen shot of it..

Requesting you to please help me in this issue..

I am new for Scripting and for ServiceNow.

 

Thank you.

 

1 REPLY 1

Tony Chatfield1
Kilo Patron

Hi, it is not best practice to run client-side GlideRecord scripts in this manner and the appropriate solution would be to utilize GlideAjax to call server side script\functions from your client script.

GlideAjax | ServiceNow Developers

 

However,
I see no valid reason to use client-side script to generate group approvals and suspect this solution will result in issues\anomalies when\if multiple different sc_task updates insert multiple group approvals, or if multiple users updating the same sc_task in the UI trigger multiple group approval inserts.
It also bypasses\excludes the OOB functionally used to manage the approval process, which is probably why you see issues.

I would start by revisiting the requirement, and utilizing OOB workflow approval process on the ritm table

Approval and rollback workflow activities (servicenow.com)
You could then trigger your  ritm approval via an update to your sc_task field using an after update BR.