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

Sorry I've just noticed that I did not added the logs:

MohamedElsayed_1-1737372892504.png

 

@Mohamed Elsayed 

you are comparing wrong values, it should be 6 and 7 and not 7 and 1

use this in background script and check

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

    // Check if today is not Saturday (7) or Sunday (1)
	gs.info(now.getDayOfWeekUTC());
    if (now.getDayOfWeekUTC() != 7 && now.getDayOfWeekUTC() != 6) {
        gs.info('Today is not weekend');
    }
	else{
		gs.info('Today is a weekend');
	}
})();

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

Thanks @Ankur Bawiskar,

 

I just replaced the Saturday (6) with Monday (1) just to test the script and see if the case will be created or not. In the background-script, it works and show the log value correctly 

MohamedElsayed_1-1737377087207.png

 

@Mohamed Elsayed 

you can keep Monday as 1 and see what result it gives?

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

@Mohamed Elsayed 

Hope you are doing good.

Did my reply answer your question?

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