Scheduled Script API - ServiceNow Fluent
The Scheduled Script API defines scheduled script executions [sysauto_script] that run at a specific time or on a recurring basis. Scheduled script executions are also referred to as scheduled jobs.
For general information about scheduled script executions, see Scheduled jobs.
ScheduledScript object
Create a scheduled script execution [sysauto_script] to define a server-side script that runs on a schedule.
| Name | Type | Description |
|---|---|---|
| $id | String or Number | Required. A unique ID for the metadata object. When you build the application, this ID is hashed into a unique sys_id. For more information, see ServiceNow Fluent language constructs. Format: |
| name | String | Required. The name of the scheduled script. |
| active | Boolean | Required. Flag that indicates whether the scheduled script is enabled. Valid values:
Default: true |
| script | Script | Required. A server-side script that runs on a schedule. This property supports a function from a JavaScript module, a reference to another file in the application that contains a script, or inline JavaScript. Format:
|
| condition | String | A JavaScript conditional statement that specifies the fields and values that must be true for the script to run.
This property supports a function from a JavaScript module, a reference to another file in the application that contains a script, or inline JavaScript. The script must return a Boolean value, which executes the scheduled script when true. Warnung:
Conditional scripts for scheduled report emails and Performance Analytics data collection jobs are executed in the sandbox. Therefore, function definitions are not allowed. Some API calls and keywords are also not allowed. For more information, see Script sandbox evaluator. After upgrade, jobs with conditional scripts that contain these disallowed API components finish with errors. Format:
|
| runType | String | The time interval to use for running the scheduled script. Valid values:
|
| runTime | Object | Required if the runType property is daily, weekly, monthly, once, or periodically. The time of day on a 24-hour
clock and the time zone on which the scheduled job runs.The default time zone is Coordinated Universal Time (UTC). Valid values for timeZone:
Default: 00:00:00 |
| runDayOfWeek | String | Required if the runType property is weekly. The day of the week on which the scheduled script runs.Valid values:
|
| runDayOfMonth | Number | Required if the runType property is monthly. The day of the month on which the scheduled script runs.Minimum value: 1 Maximum value: 31 |
| runPeriod | Object | Required if the runType property is periodically. The designated repeating interval on which the scheduled script runs. At least one of the properties must have a non-zero
value. |
| businessCalendar | String | Required if the runType property is business_calendar_start or business_calendar_end. The sys_id of a business calendar [business_calendar] that is used to determine
a start date or end date for the scheduled script. |
| $meta | Object | Metadata for the application metadata. With the installMethod property, you can map the application metadata to an output directory that loads only in specific
circumstances. Valid values for installMethod:
|
| $override | Object | Fields and their values provided to configure fields that aren't included in a ServiceNow Fluent API or to override an existing field. Warnung: Type safety isn't enforced for fields configured with the $override property. |
In the following example, the script runs daily at 12 p.m. if a user has the admin role.
import { ScheduledScript } from '@servicenow/sdk/core';
import { dailyJob } from '../server/scripts.js'
ScheduledScript({
$id: Now.ID['daily_scheduled_script'],
name: 'daily_scheduled_script',
active: true,
condition: 'gs.getUser().hasAssignedRole("admin");',
runType: 'daily',
runTime: {
hour: 12
},
script: dailyJob,
})