Schedule Jobs to exclude weekends..... & also Holidays?

cdavis22
Tera Contributor

Hi All,

 

I'm having trouble with creating a scheduled job to create an auto incident from a template to exclude holidays. The code below is working by excluding weekends but I am hoping someone could help me add something in the middle of the code below to exclude holidays?? I've found a few examples but they aren't really working.

 

Code I have below & I am currently using:

var result = true;

var gdt = new GlideDateTime();

var day = gdt.getDayOfWeekLocalTime(); //The day of week value from 1 to 7. Monday equals 1, Sunday equals 7.

if(day==6||day==7)

result =false;

result ;

 

Code I've tested (& found here on the forum) but wasn't successful:

var answer = false;
var s = new GlideSchedule("e939ddfcdb2c98501317f27139961981");
var w = new GlideSchedule("55ebbbf15f002000b12e3572f2b47714");
var gdt = new GlideDateTime();

if(gdt.getDayOfWeek() > 1 || gdt.getDayOfWeek() < 5){
if(s.isInSchedule(gdt)&&!w.isInSchedule(gdt)){
answer = true;
}
}

 

I figured that the code should check for a holiday first & then check for the weekend but I was not able to pull it off. (Yes I added in today as a holiday for testing). 

 

New to coding so I am hoping to hear back!

7 REPLIES 7

It's not that simple because the code I have on the scheduled job is excluding weekends. I need the code to also check for holidays as well & that's why I am asking the question hoping someone can help me with the code.

@cdavis22 I got you. Try this code for one last time.

I missed to notice the or condition in checking for week number. Try this updated code once.

 

var answer = false;
var s = new GlideSchedule("e939ddfcdb2c98501317f27139961981");
var w = new GlideSchedule("55ebbbf15f002000b12e3572f2b47714");
var gdt = new GlideDateTime();

if(gdt.getDayOfWeekLocal() >= 1 && gdt.getDayOfWeekLocal() <= 5){
if(s.isInSchedule(gdt) && w.isInSchedule(gdt)){
answer = true;
}
}

answer;

 

This code checks, if the day is in between Monday to Friday (including) (first if condition) and checks if the day is also a working day in both the schedules (second if block). If both scenarios matches answer is set to true which means the job should run, otherwise it will be false as it is initialized and returns the answer for condition to validate.

 

 

Thanks,
Anvesh

Bert_c1
Kilo Patron

Hi,

 

A quick test of the above logic fails in scripts background.  the following worked as expected to include workdays and not holidays.

 

 

 

var answer = false;
var s = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828");    // 8-5 weekdays excluding holidays
var w = new GlideSchedule("55ebbbf15f002000b12e3572f2b47714");    // U.S. Holidays

var gdt = new GlideDateTime();
var day = gdt.getDayOfWeekLocalTime();
gs.info('day = ' + day);
if(gdt.getDayOfWeekLocalTime() >= 1 && gdt.getDayOfWeekLocalTime() <= 5){
    if(s.isInSchedule(gdt) && !w.isInSchedule(gdt)){
        answer = true;
    }
}

gs.info('gdt = ' + gdt + ', answer = ' + answer);

 

 

 

Note: the check for day of week is "getDayOfWeekLocalTime()" See:

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideDateTimeAPI#r_GDT-...