Schedule job to cancel RITM if approval is pending with Inactive approver
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2020 01:09 AM
Hi All,
I have a requirement where I have to create a job which runs daily to check all such approvals which are pending with inactive approvers. Below condition should be checked:
1)Approval is in 'requested' state and is pending with Inactive user
2)This is the only approval triggered in the RITM. This should not be a part of group approval.
3)In case this is the only User approval triggered in the RITM, then this approval should be set as 'Cancelled' and the corresponding RITM also set as 'Closed Incomplete'. User should get a notification that request has been 'Closed incomplete' as approver is no longer in system, please raise new request.
Please help on this
--
Thanks,
Geeta Dhami
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2020 12:12 AM
Hi Ankur,
Apologies for the delayed response.
With the above code it will always return count of all rows which are in requested state.
I have to check for the particular request number which is checked in the beginning(line no 6)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2020 07:49 AM
Hi Geeta,
you mentioned you want it to run for all approvals and not specific to 1 RITM
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2020 01:57 AM
Hi Geeta,
sample script below;
ensure you have event create; notification created and associated to this event
enhance the code as per your requirement; notification to be on which table; the same needs to be used for eventQueue()
I believe email needs to be sent on RITM table
getRecords();
function getRecords(){
try{
var approvalAgg = new GlideAggregate('sysapproval_approver');
approvalAgg.addEncodedQuery('approver.active=false^state=requested^groupISEMPTY');
approvalAgg.addAggregate('COUNT','sysapproval');
approvalAgg.query();
while(approvalAgg.next()){
var count = approvalAgg.getAggregate('COUNT', 'sysapproval');
// for this if only 1 ritm approval exists
if(count == 1){
var rec = new GlideRecord('sysapproval_approver');
rec.get(approvalAgg.sys_id);
rec.state = 'cancelled';
rec.update();getRecords();
function getRecords(){
try{
var approvalAgg = new GlideAggregate('sysapproval_approver');
approvalAgg.addEncodedQuery('approver.active=false^state=requested^groupISEMPTY');
approvalAgg.addAggregate('COUNT','sysapproval');
while(approvalAgg.next()){
var count = approvalAgg.getAggregate('COUNT', 'sysapproval');
// for this if only 1 ritm approval exists
if(count == 1){
var rec = new GlideRecord('sysapproval_approver');
rec.get(approvalAgg.sys_id);
rec.state = 'cancelled';
rec.update();
// update ritm
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.get(approvalAgg.sysapproval);
ritmRec.state = 3;
ritmRec.update();
// trigger email
gs.eventQueue('<event_name>', ritm, ritmRec.<user_field>)
}
}
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}
// update ritm
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.get(approvalAgg.sysapproval);
ritmRec.state = 3;
ritmRec.update();
// trigger email
gs.eventQueue('<event_name>', ritm, ritmRec.<user_field>)
}
}
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2020 04:17 AM
You need to define the criteria for inactivity. When do you consider the approver inactive ? 10, 20, 30 days later ? When you know this criteria, please use below script to create scheduled job.
CancelApproval();
function CancelApproval() {
var gr = new GlideRecord('sysapproval_approver');
gr.addEncodedQuery('Add your encoded query here based on inactivity criteria'); // Copy Query rom List View
gr.query();
while (gr.next()) {
var id = gr.sysapproval;
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id',id);
ritm.query();
if(ritm.next())
{
ritm.setValue('state',4); // This could be different at your instance. Check It accordingly
ritm.setValue('stage','Request Cancelled');
ritm.update();
}
gs.eventQueue('ritm.auto.cancel', ritm);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2020 04:25 AM
Hi Alp,
I have to check if for the RITM in 'sysapproval_approver' table this is the single approval triggered and is with user who is disabled in system, then only I have to cancel this approval and subsequent RITM.