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

Request get cancelled when approval is pending after 10 days

gunjan1
Kilo Contributor

Hello All,

The requirement is to Explore on time limits for approving a request (reject after certain amount of time ) — If the approver does not approve the request within 5 business days, a notification should go to the approver asking them to approve the same after 5 days, incase the approver does not approve it within 10 days the RITM goes ahead & get cancelled.

I have tried this script include in scheduled jobs but I its not working. So, I am not getting what is wrong please check and let me know.

var ReqApprovalReminder = Class.create();  

ReqApprovalReminder.prototype = {  

      initialize: function() {  

gs.log("Approval");

      },  

      RequestApproReminder : function(){  

              var appr = new GlideRecord('sysapproval_approver');  

              appr.addQuery('state','requested');  

              appr.addEncodedQuery('sys_created_onRELATIVELE@hour@ago@1');  

              appr.query();  

              var noOfdays = 0;  

              while(appr.next()){  

                      if(appr.u_reminder_date!= ""){  

                              var dateDiff = gs.dateDiff(appr.u_reminder_date.getDisplayValue(),gs.nowDateTime(),false);  

                              noOfdays = dateDiff.split(" ")[0];  

                            gs.log("No. of days" +noOfdays);

                      }  

                      if((appr.u_num_of_reminder == "") || (appr.u_num_of_reminder == 0)) {  

                              //gs.eventQueue("reminder.emails", appr, gs.getUserID(), gs.getUserName());  

                              // If you want to some specific order guide uncomment if  

                              //if(appr.sysapproval.u_order_guide == 'OrderGuid_Sys_is'){          

                              gs.eventQueue("approval.inserted", appr, appr.approver.sys_id, appr.approver.name);  

                              appr.u_num_of_reminder += 1;   // 1st Reminder sent  

                              appr.u_reminder_date = gs.nowDateTime();    

                              gs.log("reminder1" + appr.u_num_of_reminder);

                              //}  

                        } else if ((appr.u_num_of_reminder == 1) && (noOfdays > 5)) {  

                              //if(appr.sysapproval.u_order_guide == 'OrderGuid_Sys_is'){  

                              gs.eventQueue("approval.inserted", appr, appr.approver.sys_id, appr.approver.name);  

                              appr.u_num_of_reminder += 1;// 2nd Reminder sent  

                              appr.u_reminder_date = gs.nowDateTime();  

                    gs.log("reminder2" + appr.u_num_of_reminder);

                              //}                  

                      } else if ((appr.u_num_of_reminder == 2) && (noOfdays > 10)) {  

                              //if(appr.sysapproval.u_order_guide == 'OrderGuid_Sys_is'){  

                              gs.eventQueue("approval.cancelled", appr, appr.approver.sys_id, appr.approver.name);  

                              appr.state = 'rejected'; // Now Request Cancelled  

                              appr.comments = 'Approval automatically rejected by system due to time expiration';  

                              appr.u_num_of_reminder += 1;  

                              appr.u_reminder_date = gs.nowDateTime();  

                                gs.log("reminder3" + appr.u_num_of_reminder);

                                                         

                              //var reqNum = appr.sysapproval.number;  

                              //this._cancellRequest(reqNum);                          

                              //}  

                      }  

                      appr.update();  

              }  

      },  

      _cancellRequest: function(reqNum){                          

              var scReq = new GlideRecord("sc_request");  

              scReq.addQuery('number',reqNum);  

              scReq.query();  

              if(scReq.next()){                          

                      scReq.state = 4;  

                      scReq.stage = 'closed_incomplete';  

                      scReq.request_state = 'closed_cancelled';  

                      scReq.update();  

              }  

      }};  

4 REPLIES 4

Subhajit1
Giga Guru

Hi Gunjan,


You should ideally use Timer, Notification, and Run Script activities in the RITM workflow for performing these sort of activities.


Timer to wait for the 5-day durations, Notification to send the reminder email and the Run Script to Cancel/reject the approval.


Thanks for your reply but I want this to work for all item.


Okay, then you do not write a Script Include in a Scheduled Job.


You will have to write a normal server-side script(you cannot use current here though) using GlideRecord and invoking Events to trigger notifications.


Hi Subhjit,

I am working similar kind of requirement.

I tried below runscript,

var gr = new GlideRecord("sysapproval_approver");
gr.addQuery('state','requested');
gr.addQuery('sys_created_on','<',gs.daysAgo(2));
gr.query();
if (gr.next())
{
gr.state = 'cancelled';
gr.update();

}

Yet no luck cloud please suggest.

Thanks and Regards,

Meenal