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

gaidem
ServiceNow Employee
ServiceNow Employee

It appears that package is no longer available.

Check this out:
http://wiki.service-now.com/index.php?title=Calculate_Duration_Given_a_Schedule


john_roberts
Mega Guru

The method isInSchedule requires a GlideDateTime parameter.



if(sched.isInSchedule(new GlideDateTime())){


KamalM
ServiceNow Employee
ServiceNow Employee

Thanks John, hadn't seen your reply.


Hi All,

I have similar kind of requirement.

I have to calculate hours outside the schedule.

If my start time on the form is 8 am and my schedule time is 9 am , then there is a difference of 1 hour.

I need to get this difference, but the problem is, schedules doesn't return the negative value and hence I am stuck..............

Any Help would be much appreciated.

Best Regards,
Namrata Jain