- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2023 08:55 AM - edited ‎12-07-2023 08:59 AM
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:
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2023 05:11 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2023 09:29 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2023 03:15 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-07-2023 07:13 PM
why not use the schedule for the calculation?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2023 04:25 AM - edited ‎12-08-2023 04:28 AM
@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.