Weekdays-only Scheduled Job

Service-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;

Date Posted:

February 14, 2011

Share This:

12 Comments

  1. Michele King June 29, 2011 at 1:06 am

    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

    • Mark Stanger June 29, 2011 at 1:36 am

      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.

  2. Michele King June 29, 2011 at 1:38 am

    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

  3. Alex Rivera October 20, 2011 at 12:21 pm

    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?

    • Mark Stanger October 20, 2011 at 10:13 pm

      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.

  4. Jim Coyne January 29, 2012 at 9:55 am

    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 now = new Date();
    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?

    • Mark Stanger January 30, 2012 at 4:49 pm

      Hey Jim,

      I think you should be able to do this using ‘isInSchedule’ as shown here.
      http://community.service-now.com/forum/7180

      • Jim Coyne January 30, 2012 at 7:39 pm

        I was hoping not to use a Packages function – I thought we were not really suppose to use them?

        • Mark Stanger January 30, 2012 at 7:43 pm

          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.

  5. Jim Coyne January 31, 2012 at 9:27 am

    Good point. So this works:

    var gr = new GlideRecord('cmn_schedule');
    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. :-)

    • Mark Stanger February 14, 2013 at 2:41 pm

      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.

  6. Mouli September 14, 2015 at 6:30 am

    Is it possible pause the schedule between a particular period in hours?

Comments are closed.

Categories

Tags

Loading

Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.