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 08:58 AM
Can't you check both in one if. Something like:
if(now.getDayOfWeekUTC() < 6 && schedule.isInSchedule(new GlideDateTime()))
answer = true;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 10:03 AM
Thanks, Michael
Yes, that cleans it up, so thank you for that, but the problem is I want it to run if today is NOT in the holiday schedule, but in my testing I have today set up as a holiday and Execute Now creates the tickets. So, it's not recognizing that today is a holiday in my testing.
So, I've set up today as a holiday in my holiday schedule and in my background script, I've got:
if((now.getDayOfWeekUTC() < 6) && (!schedule.isInSchedule(new GlideDateTime())))
{
gs.print("Today is not a holiday!");
}
And my result is "Today is not a holiday!" even though it is... ???
Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 10:12 AM
After this line: schedRec.get('name', 'VSP U.S. Holidays');
add: gs.addInfoMessage('Checking against schedule: ' + schedRec.sys_id);
just confirming it's returning the sys_id of the schedule VSP U.S. Holidays
if not, is name spelled correctly?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 11:30 AM
So let's break this down.
is this true: now.getDayOfWeekUTC() < 6 - going to say Yes
is this true: schedule.isInSchedule(new GlideDateTime())) - if all is correct, then no.
So you're script probably needs to be something like this:
//if not a holiday
// then check for data of week, if not weekend
if (!schedule.isInSchedule(new GlideDateTime())
if (now.getDayOfWeekUTC() < 6
answer = true;
else
answer = false;