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

Juhi Poddar
Kilo Patron

Hello @Mohamed Elsayed 

Try the following script :

 

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) {
    // Add your logic here
    gs.log("Executing the code"); // Prevents job execution on weekends
}

 

  • Since the Scheduled Job is configured to run daily, this script ensures that the job will only execute on weekdays.
  • On weekends (Saturday and Sunday), the condition evaluates to false, effectively skipping the logic and preventing further execution.

This approach provides a simple way to ensure the job runs only on business days.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Thanks @Juhi Poddar,

I used it as shown below, I just changed Sat to Monday for the sake of testing and the case still get created.

 

MohamedElsayed_0-1737373669199.png

 

Regards,

Mo

Hello @Mohamed Elsayed 

Try with the script that I have provided.

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) {
    // Add your logic here
    gs.log("Executing the code"); // Prevents job execution on weekends
}

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Hi @Juhi Poddar,

 

Here is the result, but when I use the code and I change the value from 6 to 1 (excluding Monday instead of Sat just for testing), the case gets created.

MohamedElsayed_0-1737376847685.png

 

Hello @Mohamed Elsayed 

when changing the value from 6 to 1, the condition will evaluate to false and therefore log will not get executed.

Here is the updated script, adding else to check:

    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 != 1 && day != 7) {
        //add your logic here
        gs.log("execute the code"); // Do not run the job on weekends
    } else {
        gs.log("do not execute");
    }

Result:

JuhiPoddar_0-1737377382171.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar