Condition for Scheduled Jobs isnt working

swapnil15
Tera Contributor

Hi All,

I have been asked to create a scheduled task on weekdays only (excluding weekends and public holidays). Below is what I did:

1. Created a template (say X) on the sc_task table.

2. Created a Scheduled Job and called the template X in the generate this field.

Below is the configuration and script in condition used by me:

swapnil15_0-1701967608810.png

 

 

function PublicHolidays()
{

gs.log('enter the SJ script');
var answer = false;

var now = new GlideDateTime();
var dayOfWeek = now.getDayOfWeekLocalTime();

// Check if it's a weekend
if (dayOfWeek == 0 || dayOfWeek == 4) {
	gs.log('Its a weekend ');
  return false;
} 

// Check if it's a bank holiday for 2024
var bankHolidays2024 = ["2024-01-01", "2024-02-05", "2024-03-18", "2024-03-29", "2024-04-01", "2024-05-06", "2024-06-03", "2024-08-05", "2024-10-28", "2024-12-25", "2024-12-26"];
var isBankHoliday2024 = bankHolidays2024.includes(now.format("yyyy-mm-dd"));
if (isBankHoliday2024) {
	gs.log('Its a public holiday 2024 ');
  return false;
}

// Check if it's a bank holiday for 2023
var bankHolidays2023 = ["2023-01-02", "2023-02-01", "2023-03-17", "2023-04-10", "2023-05-01", "2023-06-05", "2023-08-07", "2023-10-30", "2023-12-25", "2023-12-26"];
var isBankHoliday2023 = bankHolidays2023.includes(now.format("yyyy-mm-dd"));
if (isBankHoliday2023) {
	gs.log('Its a public holiday 2023 ');
  return false;
}

// If it's not a weekend or bank holiday, set answer to true
answer = true;
gs.log('Its a weekday ');
return answer;
}

 

 

 

Issue: When I click on execute now, every time the record is getting created in sc_task table. I want the task to be created only when the day is a weekday. I have set the weekends and public holidays as returns to false, still it is executing to true. The logs that I have (eg, the first system log - enter the SJ script) in the script are not getting printed, meaning the script in the condition is not running but the record is getting created in scheduled task table. Can anyone help me through this. 

@Ankur Bawiskar @Tony Chatfield1  

TIA. 

 

 

1 ACCEPTED SOLUTION

@swapnil15 

simply in the condition field use this script

If the nowtime falls in schedule the condition field will be set as true and then script will run

var nowTime = new GlideDateTime();
var schedule = new GlideSchedule('schedule sysId');
answer = schedule.isInSchedule(nowTime);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Anand Kumar P
Giga Patron
Giga Patron

Hi @swapnil15 ,

In script i can see the format is incorrect  yyyy-mm-dd
var isBankHoliday2023 = bankHolidays2023.includes(now.format("yyyy-MM-dd"));

Mark it as helpful and solution proposed if it serves your purpose.

Thanks,

Anand

Hello Anand, 

I tried changing the format but it did not work. When clicked on the execute button, it created a new record, and the conditions do not turn false + the system logs are not printed.

Ankur Bawiskar
Tera Patron
Tera Patron

@swapnil15 

why not use the schedule for the calculation?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar 
Schedule is new to me. Can you walk me through it?
I have created a new schedule and under the child schedule, I have created 15 schedule entries for holidays in an annual year. Now how to test this schedule. It should be called to create a template on a daily basis except the public holidays and weekends.