Excluding the Weekends in Schedule Job

vrnath
Tera Contributor

Hi All,

Appreciate your help,

We are designing the Approval Reminder notifications for pending approvals. Which are exceeded the 3 days then we need t send an reminder for the approver and which exceeds the 5 days we are auto rejecting the approval. For this i created two scheduled jobs which will check for three days and five days and successfully triggering the Emails with pending and rejection.

Came up with another work, we need to exclude the weekends from this. So i checked in web and found some solutions like below

var schedule = new GlideSchedule("607e96184fa88200142ad0af0310c7ff");   // sys_id of the schedule which our organisation is using for to calculate the holidays and work time 7am - 5pm

if(schedule.isInSchedule(new GlideDateTime(gs.nowDateTime())))

{   code here......}

/// not able to triggering the emails when i am using the above code

In the condition of the Schedule job i used the below script.   ---- This script is triggering the emails and running the schedule job for monday to friday.

var answer = false;

//Get the day of week. 1=Monday, 7=Sunday

var now = new GlideDateTime();

//Run only on weekdays

if(now.getDayOfWeek() != 2){

    answer = true;

}

answer;

// the above script is not taking the holidays into account and i am using the below script for the schedule job.

var eQuery = "sys_created_onRELATIVELT@dayofweek@ago@3";

var app = new GlideRecord('sysapproval_approver');

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

app.addEncodedQuery(eQuery);

app.orderBy('approver');

app.query();

while(app.next()){  

gs.eventQueue('request.approval.reminder', app, app.approver);              

}

So my Query is

1 -> How to exclude the weekends and holidays using the Schedule in the schedule job

2-> Currently is we are taking the Sys created record date - for example if an approval is generated on Friday, then saturday and sunday the schedule job will not work and when its works on monday then it will trigger the email to the record which is created on friday. Bcoz its calculating the duration based on the created date. So how to calculate the duration only between monday to friday excluding the weekends. So if i record is opened on friday it has to consider the friday as the day 1 and Monday as day 2 and Tues as day3 then it has to send on the wed the pending email.

Appreciate your help on this!!!!

Thanks

Raghu

7 REPLIES 7

First off, GlideSchedule replaces Packages.com.glide.schedules.Schedule as Chuck already stated, so I would rewrite any code to use the new API.



I had a similar requirement as you, but built all of my email triggers and reminders within a workflow. It gets triggered when a new record is created, and allows me to loop through to launch emails based on whatever conditions, including escalating to another user or group if necessary. There's some helpful information in this thread about workflows and GlideSchedule if you wanted to try going that direction:


How to include a schedule in a workflow "Wait if condition" script



I'm not quite sure what your last statement means... a schedule is a schedule. If something falls outside of it (holiday, outside working hours, weekend) it will not process. So whether a record is created at 5:01pm on Friday or Monday at 6:59am, both would process the same with an email reminder as far as I understand.



I would recommend checking out DurationCalculator:
DurationCalculator - ServiceNow Wiki


Using DurationCalculator To Calculate a Due Date - ServiceNow Wiki


SNOW User8
Giga Guru

Hi raghu nath,



I am also having the same query,


Could you please help me to solve this.


Hey Anna,



We have created a custom field u_integer on the Sys approvals table and whenever a new approval record is created then we are setting the value to 0. We have created a custom schedule by excluding the weekends and passing the schedule sys_id in the script. calculating the hours based upon the company working hours per day - in the below case the daily working hours is 10. Created the event and notifications for to trigger the email to approver.



find_real_file.png