The Zurich release has arrived! Interested in new features and functionalities? Click here for more

whenever line Manager.manager also not approved with in five days then request will be cancelled

Nareshpatel
Tera Expert

when a line manager not approved with in 3 days then the notification goes to linemanager.manager . If linemanager.manager also not approved with in five days( this period line manager should be also able to approve ) , then the request will be cancelled.

Please help anyone to script provide (no flow designer or workflows suggestion).

1 ACCEPTED SOLUTION

If you will provide more details than I may help with the proper script.
But you can check once with this 

var grRITM = new GlideRecod('sc_req_item');
grRITM.addencodedquery('request.request_state=requested^ORDERBYDESCnumber');
grRITM.query();
while(grRITM.next(()) {
      var lineManger = grRITM .u_line_manager; //based upon your field name change this field 
      var lineManagerMang =  grRITM .u_line_manager.manager;  //based upon your field name change this field 
       var grApproval = new GlideRecord('sysapproval_approver');
       grApproval.addencodedquery('sysapproval='+grRITM .sys_id+'^approver='+lineManger +'^ORapprover='+lineManagerMang +'^ORDERBYsys_created_on');
      grApproval.query();
     while(grApproval.next()) {
           var date1 = new GlideDateTime(); //current date
           var createdDate = new GlideDateTime(grApproval.sys_created_on); //approval created date
            var dateDiff = GlideDateTime.subtract(createdDate, date1);
            var days = dateDiff.getDayPart();
          if(days > 4 && grApproval.approver == lineManger ) { //you can change the date value based upon your requirement
              ///you can call a event to trigger the notification and a script action to create a approval request for line manager's manager if this approval request you are note creating from the flow or somewhere
          }
           if (days > 5 && grApproval.approver == lineManagerMang) {
                 write script to cancel the request
           }
}
Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

View solution in original post

11 REPLIES 11

How to achieve the below scenario using scheduled script ?*
A. For a catalog item, send approval to requester’s manager.
B. If manager did not approve within 3 days then send that approval(eventqueue) to line
Manager’s.Manager.
C. If that person (lineManager’s.Manager) also did not do anything within 5 days
then proceed further action is cancel the request

instead of sysapproval table , can we go with other like ritm table below like ;

var grRITM = new GlideRecord('sc_req_item');
 grRITM.addEncodedQuery('active=true^cat_item=fd29303ddb4680d0e634adc3ca96198e^approval=requested^sys_created_onRELATIVELT@dayofweek@ago@1');
 grRITM.query();

 while (grRITM.next()) {
     var user = grRITM.u_requested_for.name.toString();
     var number = grRITM.number; // Get the line manager (from variables)
     var lineManager = grRITM.variables.user_id.manager; // Get line manager's manager
     var managerGR = new GlideRecord('sys_user');
     if (managerGR.get(lineManager)) {
         var managerManager = managerGR.manager; // Optional: check if not VIP before triggering
         if (!managerGR.manager.vip) {
             // Fire the event for each RITM
             gs.eventQueue('kf_inactive_line_manager', grRITM, user, managerManager);
         }

     }

     var date1 = new GlideDateTime(); // current date
     var createdDate = new GlideDateTime(grRITM.sys_created_on); // approval created date
     var dateDiff = GlideDateTime.subtract(date1, createdDate);
     var days = dateDiff.getDayPart();

     if (days > 5 && grRITM.approval == 'requested') {
         grRITM.state = 'Cancelled';
         grRITM.comments = "REQ auto Cancelled as it is awaiting approval for more than 5 days";
         grRITM.update();
     }

     gs.eventQueue('kf_inactive_line_manager_cancel_request', grRITM, user, managerManager);
 }

pls correct the script where i did mistake

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('active=true^cat_item=uvsdalvyut6768s8^approval=requested^sys_created_onRELATIVELT@dayofweek@ago@3');
gr.query();
while (gr.next()) {
    var createdDate = gr.getValue('sys_created_on');
    var createdGlideDate = new GlideDateTime(gr.sys_created_on).getLocalDate();
    var getExpDate = createdGlideDate.getDate();
    var curDateTime = new GlideDateTime();
    var curDate = curDateTime.getLocalDate();
    var forEmailsgetDays = GlideDateTime.subtract(getExpDate, curDate);
    var reminderDays = forEmailsgetDays.getDayPart();
    if (reminderDays > 3 && reminderDays <= 5) {
        // Escalate to line manager's manager
        var lineManager = gr.variables.user_id.manager;
        var managerGR = new GlideRecord('sys_user');
        if (managerGR.get(lineManager)) {
            var managerManager = managerGR.manager;
            if (!managerGR.manager.vip) {
                gs.eventQueue('kf_inactive_line_manager', gr, lineManager, managerManager);
            }
        }
    }

    else if (reminderDays > 5) {
        // Reject the request
        gr.state = -35;
        gr.stage = 'Closed Rejected';
        gr.approval = 'rejected';
        gr.active = 'false';
        gr.comments = "Request rejected due to lack of approval within 5 days";
        gr.setWorkflow(false);
        gr.update();

 

    }this script is working fine but pls provide script if mail trigger to only working hours instead of all days 

Hi @Nareshpatel 
Can you provide these details so that I can help with this to write your script?
the form structure?
Based on which field value approval is generating?

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash