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

surajnikam111
Tera Guru
Tera Guru

Hi @Mohamed Elsayed ,

 

I think you should use answer= true and answer = false instead of return true/false.

I hope this helps and resolve your issue. 

 

Regards,

Suraj 

Ankur Bawiskar
Tera Patron
Tera Patron

@Mohamed Elsayed 

try this -> use getDayOfWeekUTC()

var answer = false;
var now = new GlideDateTime();
if(now.getDayOfWeekUTC() !=6 && now.getDayOfWeekUTC() !=7 )
{      answer = true;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar,

I tried this, but the issue persists. During testing, I changed the value from 1 (Sunday) to 2 (Monday) to verify.

(function() {
    var answer = false;
    var now = new GlideDateTime();

    // Check if today is not Saturday (7) or Sunday (1)
    if (now.getDayOfWeekUTC() != 7 && now.getDayOfWeekUTC() != 1) {
        answer = true;
    }

    return answer;
})();

@Mohamed Elsayed 

did you run in background script and see what it returned?

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

No result 

 

MohamedElsayed_0-1737372542392.png