Help to Make Scheduled Jobs to Run Only on Weekdays

Mohamed Elsayed
Tera Expert

Hi All,

 

I’ve used the Scheduled Entity Generation feature to set up a daily case for morning checks. However, I need it to run only on weekdays. I’ve made it conditional and applied the script below, but it’s still running on weekends. Any suggestions?

 

MohamedElsayed_0-1737365804686.png

 

(function() {
    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) {
        return false; // Do not run the job on weekends
    } else {
        return true; // Run the job on weekdays
    }
})();

 

Regards,

Mo

20 REPLIES 20

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Your logic is correct but I think when you are converting it to Singapore time zone what is where its failing

Try to convert this whole thing into System time sone(GMT Most Likely) and run as System Admin.

Instead of getDayOfWeekLocalTime use getDayOfWeek

-Anurag

Mark Manders
Mega Patron

Just return, not return false, because you aren't doing anything with it being false. 
Or even better, make it

 

if!(day==6 || day==7){

your logic

}

 

to have it only run on weekdays


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Thanks @Mark Manders,

 

I tried this but the issue persists. During testing, I changed the value from 6 to 1 to verify.

 

(function() {
    var gdt = new GlideDateTime();

    // Get the day of the week in UTC (1=Monday, 7=Sunday)
    var day = gdt.getDayOfWeekUTC();

    // Check if today is not Saturday (6) or Sunday (7)
    if (!(day == 6 || day == 7)) {
        return true; // Run the job on weekdays
    }
})();

 

Ct111
Giga Sage

Try testing the below script in background and see if it gives desired result or not.

 

answer = checkSch();

gs.print('Response was' +answer);

 

function checkSch(){

var gdt = new GlideDateTime();

// if the day of week is not 6 and not 7 it means it is weekday

if(gdt.getDayOfWeelLocalTime() != 6 && gdt.getDayOfWeekLocalTime() != 7)

return true;

else

return false;

}

 

 

I hope this helps.