Special cases in job schedules

  • Release version: Xanadu
  • Updated January 30, 2025
  • 2 minutes to read
  • Summarize
    Summarized using AI
    This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.

    Summary of Special cases in job schedules

    This content addresses specific considerations and best practices for scheduling jobs in ServiceNow, focusing on special cases like end-of-month scheduling, weekday-only schedules, and running scheduled jobs from scripts. It highlights important behaviors and limitations that ServiceNow customers should understand to avoid common pitfalls and effectively manage scheduled jobs.

    Show full answer Show less

    End of the month schedules

    Because calendar months vary in length, scheduling jobs on specific dates near the end of the month requires caution:

    • Scheduling on the 29th or 30th is discouraged since shorter months (like February) may not contain those dates.
    • Scheduling on the 31st triggers execution on the last day of any month, adapting automatically for shorter months (e.g., February 28 or 29).

    Weekday schedules

    To run scheduled scripts exclusively on weekdays, use a script that checks the current day of the week and returns true only for Monday through Friday. A sample script is provided that sets answer to true or false accordingly.

    Important: For scheduled report emails, conditional scripts run in a sandbox environment where function definitions and some API calls are restricted. For complex conditions involving Glide classes, create a Scheduled Job to handle all logic and trigger the Scheduled Report programmatically using the provided GlideRecord and gs.executeNow() method.

    Executing scheduled jobs from scripts

    To programmatically execute a scheduled job triggered by an event, retrieve the job record from the appropriate table (such as sysautoscript, scheduledimportset, sysautotemplate, or sysautoreport) by name, then execute it immediately using SncTriggerSynchronizer.executeNow(). Note that this method only supports immediate execution and cannot schedule jobs for future execution.

    Running scheduled scripts and jobs imported from another instance

    Scheduled Script Execution and Scheduled Jobs are treated as data records and are excluded from update sets by default, as indicated by the updatesync attribute on their dictionary entries. While these records can be exported and imported via XML, Schedule Item (systrigger) records are not automatically created during import. Therefore, to activate scheduled jobs or scripts imported from another instance, you must manually update the corresponding job or script record in the target instance to trigger its schedule.

    Some special cases require care in job scheduling.

    End of the month schedules

    Because months have different lengths, take care when scheduling jobs for the end of the month.

    • Scheduling an event for the 29th or 30th is not recommended, because the scheduled job is executed in months (like February) which are shorter than those dates.
    • If an event is scheduled for the 31st, it executes on the last day of the month, even if the month is shorter.

    For example, something scheduled to run on the 31st of the month runs on February 28 or February 29 in a leap year.

    Weekday schedules

    For scheduled scripts, use the following script to run only on weekdays:

    Warning:
    Conditional scripts for scheduled report emails are executed in the sandbox. Therefore, function definitions are not allowed. Some API calls and keywords are also not allowed.
    
    var isWeekday;
    var today = new Date();
    var dayOfWeek = today.getDay(); // Get day of the week(0 = Sunday, 1 = Monday, ... , 6 = Saturday)
    switch (dayOfWeek) {
    case 0: // Sunday
    case 6: // Saturday
    isWeekday = false;
    break;
    default:
    isWeekday = true;
    }
    answer=isWeekday;

    If the conditional script on a Scheduled Report is more complex and you need to make use of our Glide classes, then please use the following steps as a workaround:

    1. Create a Scheduled Job and complete all the conditional logic of the Scheduled Report inside the Scheduled Job.
    2. If all the conditional logic is satisfied, you can trigger the Scheduled Report with the following script inside the Scheduled Job.
      var schRpGr = new GlideRecord("sysauto_report");
      schRpGr.get("<sys_id of the scheduled report>");
      gs.executeNow(schRpGr);

    Scheduled jobs from scripts

    To execute a scheduled job triggered by an event, use the following script:

    //Execute a scheduled script job
    var rec = new GlideRecord('sysauto_script');
    rec.get('name', 'YOUR_JOB_NAME_HERE');
    SncTriggerSynchronizer.executeNow(rec);
    You can run the script using one of several tables:
    • scheduled_import_set (Scheduled Import Sets)
    • sysauto_script (Scheduled Script Execution)
    • sysauto_template (Scheduled Template Generation)
    • sysauto_report (Scheduled Report)
    Note:
    SncTriggerSynchronizer does not provide methods to execute scheduled jobs in the future.

    Running scheduled scripts and jobs imported from another instance

    Scheduled Script Execution and Scheduled Jobs are categorized as data records in the ServiceNow AI Platform, which means they are excluded from update sets. To determine if a table is included, navigate to All > System Definition > Dictionary and view the Attribute value for the table in question. Only tables with the attribute update_sync=true are included in update sets.

    You can optionally export and import data records via XML. However, to prevent unexpected data changes, Schedule Item [sys_trigger] records are not created for data records imported from an XML file such as an update set. To run scheduled jobs or scripts imported from another instance, update the corresponding job or script record in the target instance.