Scheduled job to cancel approvals and related requested item

Sofija
Giga Expert

Hi All,

I am trying to execute a script to mark any requested approval created 21 days ago as cancelled and update approval state on the associated requested item as cancelled too. The first part, making the approval cancelled works perfectly, however, I cannot get it to work so the RITM approval would change to cancelled too.

Here is my current work in progress scheduled job script:

checkOutstandingApprovals();

function checkOutstandingApprovals(){

var appr = new GlideRecord('sysapproval_approver');
  appr.addQuery('state', 'requested');
  appr.addQuery('sys_created_on', '>', gs.daysAgoStart(21));
  appr.addQuery('sys_created_on', '<', gs.daysAgoEnd(21));
  appr.query();

while (appr.next()){
  appr.state = 'cancelled';
  appr.update();

  var ritm = new GlideRecord('sc_req_item');
 
  while (ritm.next()){
    ritm.approval = 'cancelled';
    ritm.update();
     
  gs.eventQueue("requestitem.approval.cancelled", appr, gs.getUserID(), gs.getUserName());
  gs.log('Cancelling task ' + appr.sysapproval.number + " as approval was not received from " + appr.approver.name);
   
  }
}
}

Does anyone have any suggestions regarding how to call too updates in the script so I could both update approval on approval table and approval on requested item table? I tried looking in wiki and community but all scripts that I found had a lot more complex login in them.

Kind Regards,

Kamile

1 ACCEPTED SOLUTION

Sofija
Giga Expert

Apologies for not posting this earlier but here is a script that got this whole thing working for me a month ago and is executing well.



cancelOutstandingApprovals();


function cancelOutstandingApprovals(){

var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('state', 'requested');
appr.addQuery('sys_created_on', '>', gs.daysAgoStart(20));
appr.addQuery('sys_created_on', '<', gs.daysAgoEnd(20));
appr.query();

while (appr.next()){
  appr.state = 'cancelled';
  appr.update();
 
  var ritm = new GlideRecord('sc_req_item');
 
  ritm.addQuery('sys_id', appr.sysapproval); //You can use appr.document_id as well
  ritm.addQuery('approval', "!=",   'cancelled');
  ritm.query();
 
  if (ritm.next()){
    ritm.approval = 'cancelled';
    ritm.update();
   
    gs.eventQueue("requestitem.approval.cancelled", ritm, gs.getUserID(), gs.getUserName());
    gs.log('Cancelling task ' + appr.sysapproval.number + " as approval was not received from " + appr.approver.name);
   
  }
 
}
}


View solution in original post

5 REPLIES 5

Sofija
Giga Expert

Apologies for not posting this earlier but here is a script that got this whole thing working for me a month ago and is executing well.



cancelOutstandingApprovals();


function cancelOutstandingApprovals(){

var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('state', 'requested');
appr.addQuery('sys_created_on', '>', gs.daysAgoStart(20));
appr.addQuery('sys_created_on', '<', gs.daysAgoEnd(20));
appr.query();

while (appr.next()){
  appr.state = 'cancelled';
  appr.update();
 
  var ritm = new GlideRecord('sc_req_item');
 
  ritm.addQuery('sys_id', appr.sysapproval); //You can use appr.document_id as well
  ritm.addQuery('approval', "!=",   'cancelled');
  ritm.query();
 
  if (ritm.next()){
    ritm.approval = 'cancelled';
    ritm.update();
   
    gs.eventQueue("requestitem.approval.cancelled", ritm, gs.getUserID(), gs.getUserName());
    gs.log('Cancelling task ' + appr.sysapproval.number + " as approval was not received from " + appr.approver.name);
   
  }
 
}
}