Scheduled Jobs

nlopez4
Tera Contributor

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!

 

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
    current.setValue('next_action', gdt.getValue());
    result = false;
}

result;
6 REPLIES 6

Amit Gujarathi
Giga Sage
Giga Sage

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



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.