Determine if date is within a schedule

abrahams
Kilo Sage

I am having issues with my script not always performing as expected.   I've tried to determine why it is so inconsistent.   Time within the schedule consistently work however It is a rather hit and miss when time is outside of the schedule.   I can't quite determine why or what is causing it to fail.   Would anyone have any ideas?   Below is my script.

if (current.isNewRecord()) {

// Determine if date is within the schedule

var schedID = '<<sys_id of the schedule is entered here>>';   // Business Hours 7-4:30 weekdays excluding holidays

var sched = new GlideSchedule(schedID);

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

  current.u_after_hours_call = true;

  current.u_after_hr_system_checked = true;

}

}

1 ACCEPTED SOLUTION

abrahams
Kilo Sage

ServiceNow couldn't find any issue with my script either.   They did suggest not to use the sys_id directly in my script but to look up the sys_id by schedule name.



I determined what the issue was.   My script was working as it should.   I am a bit embarrassed to say that the issue was not all scenarios were accounted for.


View solution in original post

8 REPLIES 8

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi,



I would put a log on it so you can see what times it doesn't work as intended and see if you can see a pattern there.


I can't see anything wrong with it.



//Göran



Från: abrahams


Skickat: den 1 februari 2016 22:20


Till: Göran Lundqvist <goran.lundqvist@ldc.lu.se>


í„mne: - Determine if date is within a schedule



<https://community.servicenow.com/?et=watches.email.thread>




Determine if date is within a schedule



created by Sue Abraham<https://community.servicenow.com/people/abrahams?et=watches.email.thread> in Scripting - View the full discussion<https://community.servicenow.com/message/879691?et=watches.email.thread#879691>


mrswann
Kilo Guru

I Might have similar need, to introduce additional approval during 'freeze' periods, thus I'm following the topic. Thanks for posting.


It should be a easy thing. Not to complex and pretty much school book example:



http://wiki.servicenow.com/index.php?title=Scoped_GlideSchedule_API_Reference#isInSchedule.28GlideDa...


I did a report to see when it fails and when it succeeds.   It isn't consistent.   Are you suggesting I not use the API?   I guess I could try a query against the 'cmn_schedule' table itself to see if that works better.