Javascript to determine if a given time is within a given schedule

Jamsta1912
Tera Guru

Hello all,

I have an SLA that runs between customer facing updates for P1s, as our Service Desk aim to make an update every 30 mins. I have a requirement to configure an email notification that fires 10 mins before the next update is due (ie 10 mins before the planned end time of the SLA) to remind the Service Desk agent. I'm able to do this fine using a workflow, but only when the schedule is 24 hours.

I'm struggling to work with other schedules. For example, if the schedule is 09:00 to 17:00, I want to avoid the scenario where an email notification is fired at 16:55, when the update is due at 09:05 the following morning. So rather than use a workflow, I have an idea to use a business rule running on the task_sla table, to generate an event that triggers the email notification. But I only want the event to be generated if the current time + 10 mins falls within the schedule of the sla.

I've read the wiki page https://wiki.servicenow.com/index.php?title=Useful_Scheduling_Scripts. This has an example of how to calculate the diff between two times within a given schedule. What I am after though, is a function that takes a single time and schedule as inputs, and returns true if the time falls within the schedule. Any advice, greatly appreciated.

Jamsta.

8 REPLIES 8

Brandon Grimes
Tera Contributor

Maybe this article will help as well....
How to determine if a given date is within a given schedule


Jamsta1912
Tera Guru

Thank you Brandon

This will definitely get me started. I can see how things get complicated where Mon-Fri only schedules or multiple schedule entries are involved. I will have a play around with this function and see if I can incorporate days of the week.

Jamsta.


My other reply must have gotten lost. Before you spend too much time playing around with my function, you should check out this thread.
How to determine if a given date is within a given schedule

It looks like there is an "isInSchedule" function that accepts a GlideDateTime parameter. If your instance build is Aspen or Berlin, you may use the package call. However, if you are on Calgary I believe you must use the GlideSchedule API. I've adjusted the code to work for any instance build.



var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', 'test');
gs.addInfoMessage('Checking against schedule: ' + schedRec.sys_id);
if (typeof GlideSchedule != 'undefined')
var sched = new GlideSchedule(schedRec.sys_id);
else
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);

//Get the current date/time in correct format
var currentDateTime = new GlideDateTime();
currentDateTime.setDisplayValue(gs.nowDateTime());

if(sched.isInSchedule(currentDateTime)){
gs.addInfoMessage('in schedule');
}else{
gs.addInfoMessage('not in schedule');
}
gs.addInfoMessage('reached end');


Brandon Grimes
Tera Contributor

Taking my previous post a step further, I've adjusted the code so that it is wrapped in a function that you requested.



function timeWithinSchedule(scheduleName, dateTime) {
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', scheduleName);

//automatically use correct API for instance build
if (typeof GlideSchedule != 'undefined')
var sched = new GlideSchedule(schedRec.sys_id);
else
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);

//Get the date/time in correct format
var convertedDateTime = new GlideDateTime();
convertedDateTime.setDisplayValue(dateTime);

return(sched.isInSchedule(convertedDateTime));
}