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 02:00 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 02:31 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 03:16 AM
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;
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 03:21 AM
did you run in background script and see what it returned?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2025 03:29 AM
No result