Scheduled Job To Run M-F Excluding Weekends

cmsrenaski
Tera Contributor

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!

10 REPLIES 10

cmsrenaski
Tera Contributor

I just wanted to close the loop on this one in case anyone else runs into the same problem. I never could get the code to work as I had it using the existing schedules, even with HI and a consultant helping me. We ended up creating a new schedule, Weekdays excluding holidays, which contained a Schedule Entry for Every Weekday (Mon-Fri), All Day. We then added a Child Schedule which contained all of our company holidays, all of which are set as type Excluded. Then, in the Scheduled Job, we just added this in the Condition field:

 

     var now = new GlideDate();
     var schedRec = new GlideRecord('cmn_schedule');
     schedRec.get('name', 'Weekdays excluding holidays');
     var schedule = new GlideSchedule(schedRec.sys_id);

     schedule.isInSchedule(new GlideDate());

I tested by creating a test holiday and set it to today, ran the job, and it worked. For some reason, using the logic of searching to see if a day was NOT in the schedule didn't work. But, searching to see if the day IS in the schedule, and having that day of type Excluded, did. Go figure. Hope that is helpful for someone.

Thanks again for the help!