How to determine if a given date is within a given schedule

KamalM
ServiceNow Employee
ServiceNow Employee

Im trying to determine if a given date is within a given schedule.
I am not able to follow the following article as there is a note saying: "Functionality described here is obsolete"
http://wiki.service-now.com/index.php?title=Verify_if_dateTime_is_Within_Calendar_Hours

I have tried following this info and was not able to get it either:
Placing time/date conditions inside a business rule script

Here is the code that I am running:

//Get a schedule by name to see if we are currently in the schedule
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', 'test');
gs.addInfoMessage('Checking against schedule: ' + schedRec.sys_id);
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);

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

1 ACCEPTED SOLUTION

KamalM
ServiceNow Employee
ServiceNow Employee

The above code wasn't working because the 'isInSchedule' method takes in a GlideDateTime object and not a string.

This code works instead:



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

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


View solution in original post

5 REPLIES 5

KamalM
ServiceNow Employee
ServiceNow Employee

The above code wasn't working because the 'isInSchedule' method takes in a GlideDateTime object and not a string.

This code works instead:



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

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