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 06:42 PM
HI @nlopez4 ,
I trust you are doing great.
Here's an adjusted version of the script that should address this issue:
var result = true;
var gdt = new GlideDateTime();
// Set the date to 80 days from now
gdt.addDaysLocalTime(80);
// Get the day of the week for the new date (1=Monday, 7=Sunday)
var day = gdt.getDayOfWeekLocalTime();
// Check if the new date is Saturday (6) or Sunday (7)
if (day == 6 || day == 7) {
// Calculate days to add (2 days if Saturday, 1 day if Sunday)
var daysToAdd = (day == 6) ? 2 : 1;
// Add the days to move to the next weekday
gdt.addDaysLocalTime(daysToAdd);
}
// Update the next_action field for the job
var schedule = new GlideRecord("sys_trigger");
schedule.addEncodedQuery("name=YOUR_JOB_NAME_HERE"); // Replace with your actual job name
schedule.orderByDesc('next_action');
schedule.query();
if (schedule.next()) {
schedule.setValue('next_action', gdt.getValue());
schedule.update();
}
result;
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 07:34 AM
Hi Amit! Thank you for your reply. I tested this, but unfortunately the Next action date was set to 80 days from today, rather than the next weekday.