Help to Make Scheduled Jobs to Run Only on Weekdays
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 01:39 AM
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?
(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 03:37 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 03:48 AM
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.
Regards,
Mo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 04:02 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 04:41 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 04:50 AM
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:
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