Scheduled Job To Run M-F Excluding Weekends
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 08:26 AM
Hello,
I am looking for help with a scheduled job that runs a script to create a number of tickets every weekday. This part is working but I've received a request to modify the job so it excludes holidays. I plugged in the script I found here (https://community.servicenow.com/thread/173469?q=scheduled%20jobs%20exclude​) and set up a test holiday for all day today so I can execute now and test to make sure that it doesn't execute if it's a holiday. But it created the tickets when I executed it. I'm now down to just running the script in the Background Scripts window to see if I can get it to recognize that today is a holiday (if only!). Maybe I just don't understand how the holiday schedule is supposed to work? I know I'm getting to the right schedule because when I print out the schedRec.sys_id (below) I get back the correct sys_id for the holiday schedule record.
The script in the scheduled job:
var answer = false;
var now = new GlideDateTime();
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', 'VSP U.S. Holidays');
var schedule = new GlideSchedule(schedRec.sys_id);
if(now.getDayOfWeekUTC() < 6) // this scheduled job will run only on Weekdays
{
if(!schedule.isInSchedule(new GlideDateTime(gs.nowDateTime())))
{
answer = true;
}
}
When I run the script in Background Scripts and replace "answer=true" with "gs.print("Today is not a holiday!") I get back "Today is not a holiday!" even though I've added today as a holiday to the VSP U.S. Holidays schedule. What am I missing?
Thanks in advance for your help! I'm really looking forward to the "someday" when I can help others!
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 03:36 PM
Thanks, Michael, I really appreciate the help in breaking it down. For your first test/question, yes, it's returning the correct sys ID, so I know I'm in the right schedule. For your second set of tests/questions, now.getDayOfWeekUTC() < 6 evaluates to true as it should. However, schedule.isInSchedule(new GlideDateTime())) does not evaluate correctly. It doesn't seem to find today in the holiday schedule. I'm trying to figure out why. I've not had anything to do with holiday schedules, setting them up or otherwise, so perhaps I just don't understand how they work. I added screen prints. Perhaps you can see right off what I did wrong.
Thanks so much!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 05:03 PM
I did exactly what you're attempting without issue. I added 2/20 as a holiday:
Then in a scheduled job that comes out of the box (Auto Generate Time Cards), I added the script:
var answer = false;
var now = new GlideDateTime();
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', 'U.S. Holidays');
var schedule = new GlideSchedule(schedRec.sys_id);
if(now.getDayOfWeekUTC() < 6) // this scheduled job will run only on Weekdays
if(!schedule.isInSchedule(new GlideDateTime()))//if the current date/time is found in the schedule
answer = true;
gs.log('what is answer ='+answer);
gs.log('what is Glide ='+new GlideDateTime());
System log shows:
what is answer =true
what is Glide =2018-02-20 00:53:19
I'm in EST timezone, so new GlideDateTime() evaluates to 2018-02-20 00:53:19
Maybe because it evaluates to tomorrow, that's the problem!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2018 12:08 PM
Thanks so much for all your efforts, Michael. It could be the time, but, I don't know. Today I changed the holiday to today (2/20) and printing out today's date/time did come out to be 8 hours difference between what time it actually is and what GlideDateTime evaluates to. However, we're still on today's date, so I'm stumped as to why it's not seeing today's holiday in the holiday schedule. I tried this - which is your code - but removed the Time part of it to eliminate that variable.
var answer = false;
var now = new GlideDate();
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', 'VSP U.S. Holidays');
var schedule = new GlideSchedule(schedRec.sys_id);
if(now.getDayOfWeekUTC() < 6) // this scheduled job will run only on Weekdays
{
if(!schedule.isInSchedule(new GlideDate()))//if the current date/time is NOT found in the schedule
{
answer = true;
}
}
gs.log('what is answer ='+answer);
gs.log('what is Glide ='+new GlideDate());
answer evaluates to true, which is incorrect, because it should find today in the schedule
Glide = 2018-02-20, which is correct.
I opened a HI ticket so they can look in our instance and see if there's something else that's fishy. It IS fishy that the time is off, so I'll find out about that in this process, too, hopefully.
Thanks again, SO much! I'll come back and mark yours as the answer if it turns out to be related to the time/timing

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 10:17 AM
We faced similar issue using isinSchedule method. Refer the below KB article as well
https://hi.service-now.com/kb_view.do?sysparm_article=KB0634382
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 10:54 AM
Thanks, vinothkumar, but I don't have any time parameters on this. I've made the holiday entry to work for all day today. I'll keep this in mind, though.
Thanks, again