You can schedule a script execution to perform them on a regular basis with a specified periodicity.
To create a scheduled script job, complete the steps below:
- Navigate to System Scheduler → Schedule Script.
- Click New and fill in the fields.
- Click Save or Save and Exit to apply changes.
Schedule script form fields
Field | Mandatory | Description |
---|---|---|
Name | Y | Specify the scheduled script job name. |
Run as | Y | Select a user to run the script execution. |
Active | N | Select this checkbox to make the job active or inactive. |
Job | N | Specify a job to execute. |
Script | N | Type a script to execute. |
Run frequency | Y | Available options:
|
Run period | Y | This field appears when the Periodically option is selected in the Run frequency field. Customize the period (days, hours, minutes, or seconds) in a preferred combination using the input fields. |
Run time | Y | This field appears when the Daily, Weekly, Monthly options are selected in the Run frequency field. Specify the time when to execute a script. To run a script regularly at the same time, specify the time. |
Run date and time | Y | This field appears when Periodically, Once options are selected in the Run frequency field. Specify the date and time of the job execution. You can set the current date and time by clicking Now in the date-picker. |
Day of week | Y | This field appears when the Weekly option is selected in the Run frequency field. Specify a day of the week when the job should run. For example, select Friday. |
Day of month | Y | This field appears when the Monthly option is selected in the Run frequency field. Specify a day of the month when the job should run. For example, select 13 for the 13th day of the month. |
As an alternative to scheduled scripts, use system events. In the system event scripts, the planning of a future event and the execution of the main script (which is specified in the Script field of Schedule script) is performed. See the Events article to learn more.
Features of configuring schedule scripts
Before creating a script for changing/deleting configuration records, create a separate user in the User table and install the desired application.
Example Expand source(function executeScheduleScript() { const ITSM_APPLICATION = '156950344216170038'; const RUN_AS_USER_ID = '155964310500000026'; // Schedule script -> Run as -> ID const preference = new SimpleRecord('sys_user_preference'); preference.addQuery('name', 'application'); preference.addQuery('user_id', RUN_AS_USER_ID); preference.setLimit(1); preference.query(); if (preference.next()) { preference.value = ITSM_APPLICATION; preference.update(); } else { preference.user_id = RUN_AS_USER_ID; preference.name = 'application'; preference.value = ITSM_APPLICATION; preference.insert(); } // update/delete vcs enabled records ... })()
You cannot use the getAccessToken() method to get the token of the current user (specified in the Run as field) in the script body. To get a token, use a sample:
Example Expand sourceconst tokenRec = new SimpleRecord('user_token'); tokenRec.addQuery('user_id', '155964310500000026'); // Schedule script -> Run as -> ID tokenRec.orderByDesc('sys_created_at'); tokenRec.setLimit(1); tokenRec.query(); tokenRec.next(); const tokenValue = tokenRec.token;
- Create scheduled scripts in an inactive state. Once the script configuration is complete, change the activity via a script form.
- Transfer scripts between instances in an inactive state.
Non-standard use cases of schedule scripts
When executing a server script with updating many records, the 504 message may occur. The error is related to the execution time limit for REST requests (Time Out).
Use a scheduled script for updating part of the records. The script example:
const MESSAGE_PREFIX = '[Update User Records]'; const TABLE_NAME = 'user'; const TABLE_QUERY_CONDITION = '(field_name!=field_value)'; // query condition const QUERY_LIMIT = 500; let lastRecord = '1'; const lastTwoMinutes = new SimpleDateTime(); lastTwoMinutes.addSeconds(-120); // 2 minutes const logMessage = new SimpleRecord('sys_log'); logMessage.addQuery('level', 'debug'); // debug level logMessage.addQuery('sys_created_at', '>', lastTwoMinutes.getValue()); logMessage.addQuery('message', 'startswith', MESSAGE_PREFIX); logMessage.orderByDesc('sys_created_at'); logMessage.setLimit(1); // newest record logMessage.selectAttributes('message'); logMessage.query(); if (logMessage.next()) { if (logMessage.message.match(/(?<=Last record: )(\d{18})/g)) { lastRecord = logMessage.message.match(/(?<=Last record: )(\d{18})/g)[0]; } } ss.debug(`${MESSAGE_PREFIX}\nLast record: ${lastRecord}`); const record = new SimpleRecord(TABLE_NAME); record.orderBy('sys_id'); record.addQuery('sys_id', '>', lastRecord); record.addEncodedQuery(TABLE_QUERY_CONDITION); record.setLimit(QUERY_LIMIT); record.query(); let updatedRecords = []; while (record.next()) { lastRecord = record.sys_id; // record.field_name = 'field_value'; const updatedRecordId = record.update(); if (updatedRecordId) { updatedRecords.push(updatedRecordId); } } ss.debug(`Last record: ${lastRecord}`); ss.info(`${updatedRecords.length} records updated:\n\n${JSON.stringify(updatedRecords, null, 2)}`);
- Instead of line 33 of the script, specify the setting of the values of the record in the TABLE_NAME table.
- Before setting the frequency of the scheduled script launches and setting the final QUERY_LIMIT sample size, it is necessary to determine the approximate duration of one update cycle.
- No labels