Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Background Script to change the state of the Approvals

Riya25
Giga Contributor

Hello Experts,

 

I need to change the state of the Requested Approvals to No Longer Required only for those tickets which are closed complete.

For ex- If a Ritm is closed complete but still its approval is pending with some user who is now inactive then i need to change the state of that approval to No Longer Required using Background script.

There are multiple records.

How to achieve this?

 

Regards,

Riya

 

 

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi Riya,

 

You can try using below background scirpt. Check for 1 or 2 records first & then execute it for all. Also, check for messages to get the count for a check.

var app=new GlideRecord('sysapproval_approver');
app.addQuery('state','requested');
app.query();
while(app.next())
{
gs.print('Approval count in requested state '+app.getRowCount());
var chkritm=new GlideRecord('sc_req_item');
chkritm.addQuery('sys_id',app.sysapproval);//gets ritm
chkritm.addQuery('state','3'); //closed complete
chkritm.query();
while(chkritm.next())
{
gs.print('RITM with closed state but approval still in requested state '+chkritm.getRowCount());
app.state='not_required';
app.update();
}
}

View solution in original post

2 REPLIES 2

Jaspal Singh
Mega Patron
Mega Patron

Hi Riya,

 

You can try using below background scirpt. Check for 1 or 2 records first & then execute it for all. Also, check for messages to get the count for a check.

var app=new GlideRecord('sysapproval_approver');
app.addQuery('state','requested');
app.query();
while(app.next())
{
gs.print('Approval count in requested state '+app.getRowCount());
var chkritm=new GlideRecord('sc_req_item');
chkritm.addQuery('sys_id',app.sysapproval);//gets ritm
chkritm.addQuery('state','3'); //closed complete
chkritm.query();
while(chkritm.next())
{
gs.print('RITM with closed state but approval still in requested state '+chkritm.getRowCount());
app.state='not_required';
app.update();
}
}

Kieran Anson
Kilo Patron

Hi Riya,

You can use the following which will cancel any approvals where the approving task is inactive.

var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('state','requested');
appr.query();

while (appr.next()){
	var task = new GlideRecord(appr.sysapproval.sys_class_name);
	task.get('sys_id',appr.syapproval.sys_id);
	if(task.active == false){
		appr.setValue('state','cancelled');
		appr.update();
		gs.print("Updated Approval for : " + appr.sysapproval.number);
	}
}