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!

Weekdays Scheduled Import

//Return 'true' to run the job
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…

//Return 'true' to run the job
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…

//Return 'true' to run the job
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;