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 01:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 01:51 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 03:21 AM
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
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 01:52 AM
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.