Scheduled Jobs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 08:01 AM
Hey all!
I have a scheduled job that runs every 80 days while excluding weekends. If the job falls on a weekend, it should run the following weekday. The script seems to be fine. However, I've tested the job to run on a weekend, and it was skipped and its execution was set for 80 days from the day the test was executed. I can't seem to find the issue. The job type is Scheduled Entity Generation, the Conditional checkbox is selected, Run is set to periodically, and the Repeat interval is 80 days.
Please see my current script below. Any help is much appreciated. Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 09:13 AM
Hey @nlopez4 ,
The schedule for the next Scheduled execution is in the sys_trigger table. Here, you can change the day for execution. sys_trigger table only shows the next scheduled execution for the job.
The code:
var result = true;
var gdt = new GlideDateTime();
// Get the day of the week (1=Monday, 7=Sunday)
var day = gdt.getDayOfWeekLocalTime();
// Check if today is Saturday (6) or Sunday (7)
if (day == 6 || day == 7) {
// Calculate days to add (1 day if Sunday, 2 days if Saturday)
var daysToAdd = (day == 7) ? 1 : 2;
// Add the days to the current date
gdt.addDaysLocalTime(daysToAdd);
// Schedule the job for the next weekday
var schedule = new GlideRecord("sys_trigger");
schedule.addEncodedQuery("name=Test script schedule");
schedule.orderByDesc('next_action');
schedule.query();
if(schedule.next()){
schedule.setValue('next_action', gdt.getValue());
schedule.update();
}
result = false;
}
result;
Hopefully it helps.
Thanks
Hamza,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 01:43 PM
Thank you for your reply! I tested using the code you provided above, but the Next action date was set to 80 days from today, rather than the next weekday.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 03:20 PM
Another approach is that the current execution on the weekend will be canceled, while another scheduled execution is created, we change its action date,
First, create an event by going to System Policy > Events > Registry,
Make sure you select the table sys_trigger, for this table only this event will work.
Then, create a script action that will run once the event is executed, System Policy > Events > Script Actions,
Script Action Code:
// Schedule the job for the next weekday
var schedule = new GlideRecord("sys_trigger");
schedule.addEncodedQuery("name=Test script schedule");
schedule.orderByDesc('next_action');
schedule.query();
if (schedule.next()) {
schedule.setValue('next_action', event.parm1);
schedule.update();
}
Now, onto the Schedule Job Script:
var result = true;
var gdt = new GlideDateTime();
// Get the day of the week (1=Monday, 7=Sunday)
var day = gdt.getDayOfWeekLocalTime();
// Check if today is Saturday (6) or Sunday (7)
if (day == 6 || day == 7) {
// Calculate days to add (1 day if Sunday, 2 days if Saturday)
var daysToAdd = (day == 7) ? 1 : 2;
// Add the days to the current date
gdt.addDaysLocalTime(daysToAdd);
// Schedule the job for the next weekday
var schedule = new GlideRecord("sys_trigger");
schedule.addEncodedQuery("name=Test script schedule");
schedule.orderByDesc('next_action');
schedule.query();
if(schedule.next()){
// we pass the event name, glide record, and a value of our choice in the parameters
gs.eventQueue("schedule_weekend_execution",schedule,gdt.getValue());
}
result = false;
}
result;
In this way, we can change the execution date.
Hope it works this time, if this solution worked for you, Kindly mark this as a solution so it may help others in the future.
Thanks,
Hamza
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 08:20 AM
Thank you! I believe this worked. I tested by inserting some additional code (see below) into the scheduled job script to exclude the job running today, and the next action date was set to tomorrow. Please let me know if there's another way I should test this. Otherwise, I can mark your answer as the solution. Thanks again!
var result = true;
var gdt = new GlideDateTime();
// Get the day of the week (1=Monday, 7=Sunday)
var day = gdt.getDayOfWeekLocalTime();
// Check if today is Saturday (6) or Sunday (7)
if (day == 2 || day == 6 || day == 7) {
// Calculate days to add (1 day if Sunday, 2 days if Saturday)
var daysToAdd = (day == 2 || day == 7) ? 1 : 2;
// Add the days to the current date
gdt.addDaysLocalTime(daysToAdd);
// Schedule the job for the next weekday
var schedule = new GlideRecord("sys_trigger");
schedule.addEncodedQuery("name=Reminder INC - Transact - Tools and Reports Users Password Reset");
schedule.orderByDesc('next_action');
schedule.query();
if(schedule.next()){
// we pass the event name, glide record, and a value of our choice in the parameters
gs.eventQueue("schedule_weekend_execution",schedule,gdt.getValue());
}
result = false;
}
result;