how to set up a job condition to exclude holidays?

Marina15
Tera Expert

Hello everyone! 

I tried this script, but it returns false

var answer = new GlideSchedule("sys_id").isInSchedule(new GlideDateTime());
gs.log(answer);

 

Marina15_0-1672411983887.png

type exclude

 

I need the schedule job not to work on holidays
how can i do it?

Thanks in advance!

13 REPLIES 13

Kashyap G
Tera Expert

Hi @Marina15 

 

Please use the below script:

 

var currentdate = new GlideDate();
currentdate.getByFormat("MM-dd-yyyy");
var gdt = new GlideDateTime(currentdate);
var result=gdt.getDayOfWeekLocalTime();
var answer=true;
if(result==6 | result==7)
{
answer=false
}
 
Please accept the solution and mark this as helpful based on the impact.
 
Best,
Kashyap G

 

you misunderstood me, Saturday and Sunday have nothing to do with it, it’s the holidays from the calendar, the cmn_schedule table. if I change the holiday type to any other, everything works, but with the "Exclude" type it doesn't work. And I need to write a condition for schedule job based on these holidays.

scheduled job creates an event every day, to which the notification reacts and is sent. I don't need notifications to be sent on holidays

jaheerhattiwale
Mega Sage
Mega Sage

@Marina15 Please try below code. This should work for you.

 

var answer = new GlideSchedule("<SYS ID OF SCHEDULE HERE>").isInSchedule(new GlideDate()+" 10:00:00");
 
Note:
new GlideDateTime() function return the exact current time. If the job is running at mid night then definitely mid night time will not be in schedule. 
Hence manually adding the time as 10 am to check if for that date 10 am is in schedule or not.
If its Saturday / Sunday, 10am will not be in schedule and for rest of the days 10 am will be in schedule.
 
Please mark as correct answer if this solves your issue. 
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Marina15_0-1672415870442.png

I still think that the reason is the exclude type, because the same thing that I have in the code, only with a different type of holiday, works as expected