Javascript to determine if a given time is within a given schedule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2013 06:58 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2013 07:55 AM
Thanks Brandon. This is bang on what I was after. Really helpful... was just expecting some pointers but you've delivered the solution!
Jamie.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-05-2013 07:24 AM
Yes, thank you so much for providing this script, Brandon! This is very helpful. Got most of the requirements built using your example.
Do you or anyone else here happen to have more info on Packages.com.glide.schedules.Schedule / GlideSchedule by any chance?
Specifically, what I need to do is retrieve the Start and End date/time of the Schedule Entry (cmn_schedule_span) within which my date/time falls so that I present the exact dates of the blackout / freeze to the end-user on the alert box
-----------------------------------------------------------------------------------------------------------------------------------------
Script Include:
var CheckScheduleAgainstChgDates = Class.create();
CheckScheduleAgainstChgDates.prototype = Object.extendsObject(AbstractAjaxProcessor, {
datetimeWithinSchedule: function() {
var schedname = this.getParameter('sysparm_schedule_name');
var chg_date_time = this.getParameter('sysparm_dateTime');
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', schedname);
//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 ChangeDateTime = new GlideDateTime();
ChangeDateTime.setDisplayValue(chg_date_time);
return(sched.isInSchedule(ChangeDateTime));
}
});
-----------------------------------------------------------------------------------------------------------------------------------------
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CheckScheduleAgainstChgDates');
ga.addParam ('sysparm_name','datetimeWithinSchedule');
ga.addParam ('sysparm_schedule_name','Hard Freeze');
ga.addParam ('sysparm_dateTime',newValue);
ga.getXML(CheckScheduleAgainstChgDatesParse);
function CheckScheduleAgainstChgDatesParse (response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
alert ('The CAB has restricted changes to be implemented during this time frame. Your Change may be denied');
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
Thanks in advance!
Cosmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2013 10:25 AM
SN Support got back to me in the meantime and after reiterating the fact that they don't support this they gave me the following useful info.
"... there does not appear to be anything in Schedule.java class that you can use. We do see that the ScheduleDateTimeSpan.java does have getStart() and getEnd() methods, and that's what we're using to compare in the isInSchedule() method. But this is not something that support would assist with. This would fall into the scope of professional services or perhaps reaching out to the community to see if others may have already gone down this road before."
Does anyone know how could I use getStart() / getEnd() methods within this class to get stat / end date-time for the cmn_schedule_span matched by my date?
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2018 03:09 PM
Glide Packages is No More Supported in ServiceNow-
Try the below script to fulfill the requirement-
var g = new GlideRecord('cmn_schedule');
g.addQuery('name', 'Service Time');
gs.addInfoMessage("test");
g.query();
if (g.next()) {
gs.addInfoMessage("test2");
var sched = new GlideSchedule(g.sys_id);
var d = new GlideDateTime();
//d.setDisplayValue("2018-06-01 08:00:00");
if (sched.isInSchedule(d))
{
gs.addInfoMessage("Is in the schedule");
}
else
{
gs.addInfoMessage("Is NOT in the schedule");
}
Reference: ServiceNow Docs