Creating a Scheduled Job

From ServiceNow Wiki
Home > Administer > Core Configuration > Time > Creating a Scheduled Job
Jump to: navigation, search
Time
Related Topics
Knowledge.gif Get the Book

1 Overview

Scheduled Jobs are automated pieces of work that can be performed at either a particular time, or on a recurring schedule. These kinds of tasks can be automated:

  • Run and distribute a report
  • Generate a record (incident, change, configuration item, etc.) from a template
  • Run a business rule and do whatever the rule contains

Note: The method described below creates a scheduled job on the Schedule Job [sysauto] table (System Definition > Scheduled Jobs). Create all new scheduled jobs using this method. Some existing scheduled jobs are found on the Schedule Item [sys_trigger] table (System Scheduler > Scheduled Jobs). Do not create new scheduled jobs on the Schedule Item table.

2 Creating a Scheduled Job

To create a scheduled job, navigate to System Definition > Scheduled Jobs and click New. Select the appropriate type of scheduled job.

image:new_action.png


The fields presented will depend on the type of scheduled job required.

2.1 Scheduling a Report

Scheduled Reports are distributed via email. Scheduled reports created by an individual whose user account is deactivated might not display any data. To ensure that the desired data is displayed, an active user must recreate the scheduled report.

Populate the following fields to schedule a report:

Field Input Value
Name A name to identify this scheduled job.
Report A reference to the report to be scheduled. Calendar reports and Pivot Table reports are not currently supported for automatic emailing.


For information on creating or modifying reports, see Generating Reports.

Users The users to send the report to at the scheduled date and time. The user must have Notification set to "Email" on their user record to receive reports. To force users to receive emails, use the Email Addresses field.
Groups The groups to send the report to at the scheduled date and time.
Email addresses Any other email addresses to send the report to, separated by commas. These emails will always receive the report, even if there's a matching user record for that address that says "Do not notify".
Active If true, this report will be sent at the scheduled date and time.
Run The type of schedule to send the report on. Choices are:
  • Daily
  • Weekly
  • Monthly
  • Periodically
  • Once
Day
  • If Run is Weekly, the day of the week.
  • If Run is Monthly, the day of the month.
Repeat Interval If Run is Periodically, the duration between each scheduled report. The duration can be days, hours, or minutes.
Time If Run is Weekly or Monthly the time of day, on a 24 hour clock.
Starting The date and time of the first scheduled report.
Priority The numerical priority of a scheduled job. Set essential jobs to a priority value below 100 and nonessential jobs to a priority above 100. If 70% or more of all scheduled jobs are Overdue, any jobs marked with a value above 100 do not run.
Subject The subject line for the email.
Introductory Message The body of the email.
Run as The user creating and running the scheduled job. To have the scheduled job assigned to system behavior instead of the person creating the scheduled job, create a system or “dummy” user and add that user to this field.
Type The file-type of the attached report. Choices are:
  • PDF-landscape
  • PDF
  • Excel
  • CSV
  • PNG
Include Detail If checked, will include details on the records in the report.
Zip Output If checked, the report will be compressed in a .zip file.

As a workaround for scheduling calendar and pivot table reports for email distribution, consider using the publish option. Publish creates a URL for the report and displays the address above the report form. You can create an email notification with this URL and send the link to people who need to see the report, or you can send the url for the calendar report to a distribution list. For details about publishing reports, see Publishing Reports for more information.

2.2 Scheduling Entity Generation

To schedule the generation of an entity, populate the following fields:

Field Input Value
Name A name to identify this scheduled entity generation.
Active If true, the entity will be generated at the scheduled date and time.
Run The type of schedule to generate the entity on. Choices are:
  • Daily
  • Weekly
  • Monthly
  • Periodically
  • Once
Day
  • If Run is Weekly, the day of the week.
  • If Run is Monthly, the day of the month.
Repeat Interval If Run is Periodically, the duration between each scheduled generation.
Time If Run is Weekly or Monthly the time of day, on a 24 hour clock.
Starting The date and time of the first scheduled generation.
Conditional If checked, the entity will only be generated if certain conditions are met.
Condition If Conditional is checked, a script determines under what conditions the scheduled script is executed. The last expression of the script should evaluate to a Boolean (true/false) value. See Example Conditional Script.
Generate this A reference to a template for a record.

2.3 Scheduling Script Execution

Field Input Value
Name A name to identify this scheduled script execution.
Active If true, the script will be executed at the scheduled date and time.
Run The type of schedule to execute the script on. Choices are:
  • Daily
  • Weekly
  • Monthly
  • Periodically
  • Once
Day
  • If Run is Weekly, the day of the week.
  • If Run is Monthly, the day of the month.
Repeat Interval If Run is Periodically, the duration between each script execution.
Time If Run is Weekly or Monthly the time of day, on a 24 hour clock.
Starting The date and time of the first scheduled script.
Priority The numerical priority of a scheduled job. Consider setting async business rules to a priority of 100 or above to ensure any data import scheduled jobs have time to run first.
Application The application that contains the script (available starting with the Fuji release).
Conditional If checked, the script will only be executed if the Conditions evaluate to true.
Condition If Conditional is checked, a script determines under what conditions the scheduled script is executed. The last expression of the script should evaluate to a Boolean (true/false) value. See Example Conditional Script.
Run this script The script to run at the scheduled date and time.

2.4 Example Conditional Script

The following is an example of a conditional script. This example runs the scheduled job only if there are active Incidents older than 30 days.

// Only run this Scheduled Job if there are active Incidents over 30 days old
var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT');
ga.addQuery('active', 'true');
ga.addQuery('sys_created_on', '<', gs.daysAgo(30));
ga.query();
ga.next();
ga.getAggregate('COUNT') !== '0'

3 Scheduling Special Cases

3.1 Scheduling for the End of the Month

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 will not be executed in months (like February) which are shorter than those dates.
  • If an event is scheduled for the 31st, it will be executed 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 will run on February 28th or February 29th in a Leap Year.

3.2 Scheduling for Weekdays

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

function checkWeekdays() {
	var now = new Date();
	var day = now.getDay();
	var result = false;
	if(day != 0 && day != 6) {
		result = true;
	}
	return result;
}
checkWeekdays();

3.3 Executing Scheduled Jobs from Scripts

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

Note
Note: This API call changed in the Calgary release:
  • SncTriggerSynchronizer replaces Packages.com.snc.automation.TriggerSynchronizer

The new script object calls apply to the Calgary release and beyond. For releases prior to Calgary, substitute the packages call as described above. Packages calls are not valid beginning with the Calgary release. For more information, see Scripting API Changes.

//Execute a scheduled script job
var rec = new GlideRecord('sysauto_script');
rec.get('name', 'YOUR_JOB_NAME_HERE');
SncTriggerSynchronizer.executeNow(rec);

This script can be run 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 that SncTriggerSynchronizer does not provide methods to execute scheduled jobs in the future.

3.4 Running Scheduled Jobs Imported from Another Instance

To prevent unexpected data changes, the system does not automatically create Schedule Item [sys_trigger] records for Scheduled Jobs [sysauto] imported from an XML file such as an update set. To run a scheduled job imported from another instance, update the scheduled job record.

4 Viewing Schedule Items

Schedule items are individual instances of a scheduled job. To see which scheduled jobs will run today, navigate to System Scheduler > Today's Scheduled Jobs. The table displays each schedule item that will be run. It is usually inadvisable to modify the schedule items themselves. It is best to modify the scheduled jobs themselves.


Was this article helpful?
Yes, I found what I needed
No, I need more assistance