S
ervice-now allows you to run scheduled jobs at various intervals. Out of box, you are given the option to run scheduled jobs like imports on a specific day, day of the week, month, or at some other custom interval. There are certain intervals that just don’t work very easily with the default setup provided. One of these that can be challenging is the weekday-only schedule. In order to get a scheduled job to run only on weekdays, the recommendation is usually to set up 5 different scheduled jobs, one for each day of the week you want to run on. While this works, it’s really 4 more scheduled jobs that you don’t want to set up and manage. With some simple scripting and the use of the ‘Condition’ field, you can set up a single daily scheduled job to handle this scenario.
In order to get this to work, you’ll need to make sure to add the ‘Conditional’ checkbox and the ‘Condition’ script field to your scheduled job form. Once you do that, you can check the ‘Conditional’ checkbox and add a script like I’ve created below. The script runs each day at the time specified on the job, identifies the day of week based on the user running the job, and returns a 1-7 integer value that you can then use to determine when the job should run. For this scenario, we simply return ‘true’ when the day of week is less than 6!
var answer = false;
//Get the day of week. 1=Monday, 7=Sunday
var now = new GlideDateTime();
//Run only on weekdays
if(now.getDayOfWeek() < 6){
answer = true;
}
answer;
Yearly scheduled job
A yearly scheduled job can be set up in almost the same way. Just set up a monthly scheduled job with a script like I’ve added below. The script just needs to indicate which month of the year the job should run…
var answer = false;
//Get the month of the year. 1=January, 12=December
var now = new GlideDateTime();
//Run only for the first month of the year
month = now.getMonth();
if(month == 1){
answer = true;
}
answer;
Quarterly scheduled job
A quarterly job is the same as the yearly job except it checks for 4 month numbers…
var answer = false;
//Get the month of the year. 1=January, 12=December
var now = new GlideDateTime();
//Run quarterly
month = now.getMonth();
if(month == 1 || month == 4 || month == 7 || month == 10){
answer = true;
}
answer;
Hi, I’m just trying to write a client script on a catalog item to check to see if the date selected falls on a Thursday. I wrote a global business rule with a modification of above to return the day of week to the client script. I’m not sure when passing in the date value where it goes in the script. I keep getting the return value as null. Any help would be greatly appreciated.
Thanks-Michele
It looks like you’ve asked this question on the forums as well…
http://community.service-now.com/forum/937
I’ll let you work through that forum posting since your question really deals with that instead of this solution. I’m guessing that your issue has to do with the use of ‘AJAXEvaluateSynchronously’ though. If you’re using that, you should really use a client-callable script include instead of a business rule and use GlideAJAX instead of AJAXEvaluate. Check out the wiki on GlideAJAX for information.
Yeah, found the forum thread after I posted here but had already done exactly what the forum recommends and still was getting the null value back. I’ll try the script include instead. Thanks-Michele
Hey Mark – I tried the script and it is throwing the following error:
JavaScript parse error at line (5) column (25) problem = missing ; before statement
Error:
Problem at line 8 character 27: Missing ‘;’
If(now.getDayOfWeek() < 6){ Problem at line 9 character 6: Expected to see a statement and instead saw a block. answer = true; Any recommendations?
Sounds like a copy/paste issue with the syntax highlighter on your script field. Make sure that you toggle the syntax highlighter off (using the middle icon at the top of the script field) before pasting. Let me know whether that works or not.
Now is it possible to find out if the current day is an actual work day based on a schedule? For example, I have the following Scheduled Script Execution that runs everyday at 8:00 AM to send out approval reminders:
var day = now.getDay();
// do not run on Saturday or Sunday
if (day != 0 && day != 6) {
u_sendApprovalReminder(); //Script Include
}
The Script Include actually does the sending of the emails. What I would like to do is skip the reminders on a weekday that may also be a company holiday. Is there a scripting method to tell whether a date is an active work day based on a defined schedule?
Hey Jim,
I think you should be able to do this using ‘isInSchedule’ as shown here.
http://community.service-now.com/forum/7180
I was hoping not to use a Packages function – I thought we were not really suppose to use them?
I hear you. ServiceNow dev keeps preaching that but until they give us something else to use we need to use packages calls. This one is fairly common though so they should make sure that whatever they change with it doesn’t cause any disruptions.
Good point. So this works:
if (gr.get('name', 'Weekdays 08:00 - 17:00')) {
var schedule = new Packages.com.glide.schedules.Schedule(gr.sys_id);
if(schedule.isInSchedule(new GlideDateTime())) {
u_sendApprovalReminders();
}
} else {
gs.log('*** Approval Reminder - Schedule not found');
}
BUT only if you have everything defined on the same schedule. I was trying to create a master schedule (Weekdays 8:00 – 17:00) and then have a child schedule with the actual company holidays defined on it to exclude those days. Guess I’ll have to stick to just one schedule. 🙂
Heads up! ‘Packages’ calls are going to be phased out starting with the Calgary release. There’s a migration utility that ServiceNow is going to provide, but you can replace ‘Packages.com.glide.schedules’ with ‘GlideSchedule’ starting in Calgary to make it work without a Packages call.
Is it possible pause the schedule between a particular period in hours?