Schedule Jobs to exclude weekends..... & also Holidays?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 04:28 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 07:19 PM
Hi @cdavis22 ,
Check this part ( I modified it include Monday and Friday),
if(gdt.getDayOfWeek() >= 1 || gdt.getDayOfWeek() <= 5){
if(s.isInSchedule(gdt) && !w.isInSchedule(gdt)){
answer = true;
}
}
And as per this logic, the day you are checking should not be a holiday in one schedule (s) and at the same time it should be a holiday in another schedule (w). Is that what you intend?
Then try making today a holiday in w (55ebbbf15f002000b12e3572f2b47714) schedule and not a holiday in s (e939ddfcdb2c98501317f27139961981) schedule and check the condition.
And, getDayoOfWeek() return the UTC week number, you can try using getDayOfWeekLocalTime() as you did in checking the weekend.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 07:27 PM
Hi!
So as of right now the scheduled job is running Monday-Friday & exlcuding weekends with the code below.
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 ;
I am not sure how to exclude holidays. With the 2nd code posted below from a previous post I was using the sys_id for the US Holiday schedule (& i added in today as a US holiday for testing reasons) & the sys_id for the Monday-Friday but the code was not working.
My job is running perfectly to exclude weekends but the next step is excluding holidays which is the challenge i'm facing. Is there nothing I can add into the code i'm currently using?
Hoping I am making sense!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 07:39 PM
In that case just check for US holiday schedule, no need to check Monday-Friday schedule.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 07:44 PM
@cdavis22 use this code.
var answer = false;
var w = new GlideSchedule("55ebbbf15f002000b12e3572f2b47714");
var gdt = new GlideDateTime();
if(gdt.getDayOfWeekLocal() >= 1 || gdt.getDayOfWeekLocal() <= 5){
if(w.isInSchedule(gdt)){
answer = true;
}
}
answer;
Anvesh