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

Uncle Rob
Kilo Patron

A couple things.


- You never actually executed your GlideRecord query after you declared the var ritm


- You don't really need to loop through the results, since each approval record will tell you exactly which request item to cancel



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');


    ritm.get(appr.document_id); // This is the line you're missing.   GlideRecords actually need to be queried via either a get
  ritm.approval = 'cancelled'; // since we can determine the precise record to get, we don't need a while loop to check for next
  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);

  }
}
}


Thank you for the assitance! I knew I was missing some kind of relationship but was not sure how to define it. This works perfectly.


Actually - it didnt work! I just noticed that it is closing all related RITMs, however because in most cases there is more than one approver per RITM, the script is creating new RITMs because it does not find a value. For example, if there were 15 approvals outstanding for 1 RITM, the 1 RITM will be closed + there will be additional 14 blank RITMs opened and closed as well.


Hi Kamile,



Sorry for coming late to this post, I came accros to this topic,


Please find below the scrpt I am using to cancel the approval of the RITM and the RITM on itself.



rejectOldApprovalRecord ();



function rejectOldApprovalRecord ()   {


var approval = new GlideRecord("sysapproval_approver");


approval.addQuery('sys_updated_on' , '<=' , gs.dayssAgo(21)); // There is no updates


approval.addQuery('state', 'requested');//Or approval.addQuery('state', '=', 'requested');, it's the same because we are like you on the list of approvals, we have to make the filters right


approval.addQuery('sysapproval.number', 'STARTSWITH', 'RITM');//Searches for all RITM not any change Managment call


approval.query();



while (approval.next()) {


        var requestedItem = new GlideRecord("sc_req_item");


        requestedItem.addQuery('number', approval.sysapproval.number)


        requestedItem.query();    


        if (requestedItem.next()) {


                  gs.eventQueue("requestitem.approval.cancelled", requestedItem, gs.getUserID(), gs.getUserName());


                  gs.log('event fired');


        }


        approval.state='cancelled';


        gs.log(approval.sysapproval.number + ' ................ ' +   approval.sysapproval.state);


        approval.update();


        }


}



Let me know if this helps you or if need further info.



Kind Regards,


ZA



Don't feel shy to hit correct or helpful answer