The CreatorCon Call for Content is officially open! Get started here.

Schedule job to cancel RITM if approval is pending with Inactive approver

Geeta5
Giga Contributor

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

15 REPLIES 15

Suseela Peddise
Kilo Sage

Hi,

Please use below script. For custom notification create a event on RITM table and trigger event from schedule job and send a notification when the event triggers.

Script:

var gr= new GlideRecord('sysapproval_approver');

gr.addEncodedQuery("state=requested^source_table=sc_req_item^ORsysapproval.sys_class_name=sc_req_item"); //RITM that are in requested state

gr.addEncodedQuery("approver.active=false");//inactive approver(s)

gr.query();

while(gr.next())

{

var cancelRITM= new GlideRecord('sc_req_item');

cancelRITM.addQuery('sys_id',gr.sysapproval); //query the RITM

cancelRITM.query();

while(cancelRITM.next())

{

cancelRITM.state = '4'; //closed incomplete
cancelRITM.stage = 'Request Cancelled';
cancelRITM.work_notes = "Request Item has been cancelled due to inactive approvers.";
cancelRITM.update();

//For custom notification create a event on RITM table and trigger event from schedule job. 

gs.evenQueue('<<event_name>>', cancelRITM, cancelRITM.request.requested_for); //replace with event name

 

}

}

If I have answered your question, please mark my response as correct and/or helpful.

Thanks,

Suseela P.

Hi Suseela,

Thanks for the response.

We also have to check if this is the only approval triggered for that RITM i.e only if single approval is there then it has to cancel the RITM.

Because there could be scenarios wherein there are other approval as well for the RITM in that case it should not cancel the approval and the corresponding RITM.

Hi Geeta,

Did you go through the script mentioned in my previous post

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks Ankur for your response.Yes I went through your script. 

But below line is not giving any count(returning undefined) and not able to go inside if loop of :if(count==1)

var count = approvalAgg.getAggregate('COUNT', 'sysapproval');

I tried with creating one sample case of true scenario but not able to get the count value.

Please suggest.