{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":40061,"path":"platform-administration/time-configuration/t_ScheduleAScriptExecution.md","title":"Automatically run a script of your choosing","bundle":null,"doc_type":null,"release":"australia","body":"\n# Automatically run a script of your choosing\n\nSchedule both conditional and non-conditional scripts. If Domain Separation is installed in the instance, you can also select, filter, sort, and schedule scripts based on their assigned domains.\n\n## Before you begin\n\nRoles required: system\\_scheduler\\_admin\n\n## About this task\n\nThe following is an example of a conditional script. It runs the scheduled job only if there are active incidents older than 30 days.\n\n```\n// Only run this Scheduled Job if there are active Incidents over 30 days old\nvar ga = new GlideAggregate('incident');\nga.addAggregate('COUNT');\nga.addQuery('active', 'true');\nga.addQuery('sys_created_on', '<', gs.daysAgo(30));\nga.query();\nga.next();\nga.getAggregate('COUNT') !== '0'\n```\n\n## Procedure\n\n1.  Navigate to **All** &gt; **System Definition** &gt; **Scheduled Jobs**.\n\n2.  Select **New**.\n\n3.  Select **Automatically run a script of your choosing**.\n\n4.  On the form, fill in the fields.\n\n<table id=\"t_ScheduleScriptExecution\"><thead><tr><th>\n\nField\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nName\n\n</td><td>\n\nName that identifies this scheduled job.\n\n</td></tr><tr><td>\n\nActive\n\n</td><td>\n\nOption that indicates that scheduled job is active and should be executed at the specified date and time.\n\n</td></tr><tr><td>\n\nApplication\n\n</td><td>\n\nName of the application that contains the script. **Global** appears if the script is in the global scope.\n\n</td></tr><tr><td>\n\nConditional\n\n</td><td>\n\nOption for enabling the running of the scheduled job if certain conditions are met in the associated script.\n\n</td></tr><tr><td>\n\nCondition\n\n</td><td>\n\nA conditional script that determines if a scheduled job should run. The last expression of the script should evaluate to a Boolean \\(true/false\\) value. This text box appears only if you select **Use conditions**. **Warning:** Conditional scripts for scheduled report emails and Performance Analytics data collection jobs are executed in the sandbox. Therefore, function definitions aren't allowed. Some API calls and keywords are also not allowed.For more information, see [Script sandbox evaluator](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/scripts/script-sandbox.md).\n\nAfter upgrade, jobs with conditional scripts that contain these disallowed API components finish with errors.\n\n</td></tr><tr><td>\n\nRun\n\n</td><td>\n\nTime interval to use for running the scheduled job:\n\n-   **Daily**: Runs daily, at a designated time.\n-   **Day and Month in Year**: Runs yearly on a specific day and month \\(for example, July 14\\), at a designated time.\n-   **Day in Week in Month in Year**: Runs yearly on a specific day of the week in a specific week of a specific month \\(for example, the second Monday in October\\), at a designated time.\n-   **Week in Month**: Runs monthly during a specific week of the month \\(for example, the third week of each month\\), on designated days and at a designated time.\n-   **Weekly**: Runs on a weekly basis, at a designated time and day of the week.\n-   **Monthly**: Runs on a monthly basis, at a designated time and day of the month.\n-   **Periodically**: Runs on a designated repeating interval.\n-   **Once**: Runs for a single occurrence only.\n-   **On Demand**: Runs immediately on demand.\n-   **Business Calendar: Entry Start**: Runs on the starting entry dates for the business calendar that you select in the **Business Calendar** field. A scheduled job runs for the starting date of each of the business entries that you defined for the business calendar.\n\nFor example, if the business calendar represents a fiscal year, and the starting date of each entry is a fiscal month, the scheduled job runs on the first day of each month.\n\n-   **Business Calendar: Entry End**: Runs for the ending date for the business calendar that you select in the **Business Calendar** field. This selection runs in the same manner as **Business Calendar: Entry Start**, but for the end dates of the associated business calendar entries.\n\n**Note:** When you select **Business Calendar: Entry Start** or **Business Calendar: Entry End**, you can apply an offset factor to schedule the job to run before or after the time span of the selected business calendar. If you select a business calendar and all its business calendar spans are in the past, the job would never run since that is an invalid configuration. To learn more, see the **Offset type** and **Offset** fields.\n\nTo learn more about creating and using business calendars and defining business calendar entries, see [Creating business calendars](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/business-calendars.md) and [Define business calendar entries](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/define-business-calendar-entries.md).\n\n</td></tr><tr><td>\n\nDay\n\n</td><td>\n\nDay on which the scheduled job should run.\n\n-   If **Run** is set to **Weekly**, select the day of the week. For example, select Wednesday.\n-   If **Run** is set to **Monthly**, select the day of the month. For example, select 25 for the 25th day of the month.\nThis field appears only if you select **Monthly** or **Weekly** in the **Run** field.\n\n</td></tr><tr><td>\n\nRepeat Interval\n\n</td><td>\n\nDuration of the repeat interval for each scheduled job execution. Enter the duration in the number of days, hours, or minutes. For example:\n\n-   To run the scheduled job every four days, enter `04` in the **Days** field.\n-   To run it every 26 hours, enter `26` in the **Hours** field.\n-   If it should repeat at an interval of 13:30:25, enter `13` in the **Hours** field, and then enter `30` and `25` in the two unlabeled fields after it.\n\n\n</td></tr><tr><td>\n\nTime zone\n\n</td><td>\n\nTime zone to use with the **Time** field entry when you specify the time at which the scheduled job should run. Select a time zone entry:\n\n-   **-None-**: Use the default time zone for the logged-in user who is creating the scheduled job.\n\nFor example, the scheduled job runs at 04:45 p.m. US/Pacific time if it’s the user's assigned time zone, and you enter `16:45` into the **Time** field.\n\n-   **Use System Time Zone**: Use the default system time zone that is specified for the instance in which it runs.\n\nFor example, the scheduled job runs at 10:15 p.m. London time if Europe/London is the default system time zone for the instance, and you enter `22:15` in the **Time** field.\n\n-   Actual time zone.\n\nFor example, the scheduled job runs at 1:30 p.m. in the US Eastern time zone if you select **US/Eastern**, and enter `13:30` in the **Time** field.\n\n</td></tr><tr><td>\n\nTime\n\n</td><td>\n\nTime of day at which the scheduled job should run, expressed in hours, minutes, and seconds on a 24-hour clock. The selection that you make in the **Time zone** field determines the time zone for this entry.\n\n</td></tr><tr><td>\n\nDay\n\n</td><td>\n\nDay on which the scheduled job should run.\n\n-   If **Run** is set to **Weekly**, select the day of the week. For example, select Wednesday.\n-   If **Run** is set to **Monthly**, select the day of the month. For example, select 25 for the 25th day of the month.\nThis field appears only if you select **Monthly** or **Weekly** in the **Run** field.\n\n</td></tr><tr><td>\n\nRepeat Interval\n\n</td><td>\n\nDuration of the repeat interval for each scheduled job execution. Enter the duration in the number of days, hours, or minutes. For example:\n\n-   To run the scheduled job every four days, enter `04` in the **Days** field.\n-   To run it every 26 hours, enter `26` in the **Hours** field.\n-   If it should repeat at an interval of 13:30:25, enter `13` in the **Hours** field, and then enter `30` and `25` in the two unlabeled fields after it.\n\n\n</td></tr><tr><td>\n\nStarting\n\n</td><td>\n\nAllows you to define the beginning window of time when you would like your job to start running. The job will actually run at the time specified in **Run time**. Select the calendar date and time. The **Starting** field appears only if you select one of the following **Run** types in the **Run** field:-   **Daily**\n-   **Day and Month in Year**\n-   **Day in Week in Month in Year**\n-   **Monthly**\n-   **Once**\n-   **Periodically**\n-   **Week in Month**\n-   **Weekly**\nFor the run type **Periodically**, the **Starting** time is the date and time of the first scheduled job generation.\n\n</td></tr><tr><td>\n\nBusiness Calendar\n\n</td><td>\n\nBusiness calendar entry that you’re using to determine the business calendar start or end date for the scheduled job. This field appears only if you select **Business Entry: Start Date** or **Business Entry: End Date** in the **Time** field.\n\n</td></tr><tr><td>\n\nRun as \\[Optional\\]\n\n</td><td>\n\nSelect another user to run the script execution as. Configure the form to add this field if it is not present.\n\n</td></tr><tr><td>\n\nRun this script\n\n</td><td>\n\nName of the script to run at the scheduled date and time. For example, copy script logic from a business rule, or call a script include.\n\n</td></tr></tbody>\n</table>5.  If Domain Separation is installed in this instance, the following fields also appear for selecting, sorting, and scheduling jobs by domain.\n\n    **Note:** The Domain Support - Domain Extensions Installer plugin installs these fields. To learn more, see [Request domain separation](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-security/t_ActivateDomainSeparation.md).\n\n<table id=\"table_m2n_yyf_tlb\"><thead><tr><th>\n\nField\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nDomain iterator\n\n</td><td>\n\nCheck box that enables this job to run across multiple domains. If selected, the **Domain source table** and **Domain source filter** fields appear.\n\n</td></tr><tr><td>\n\nDomain source table\n\n</td><td>\n\nName of the domain-separated table that is the source of the domains in which the scheduled job should run. For example, select **Department \\(cmn\\_department\\)** if you want to use it as the source of the assigned domains used to schedule scripts. The scheduled job determines these domains from the sys\\_domain field of the source table records. To learn more, see [Domain assignment](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-security/c_DomainAssignment.md). **Note:** It's a good idea to create a domain-separated table solely dedicated to sourcing domains, unless you have a use case that warrants using an existing one. If you do create a new one, simply select it using this field.\n\n</td></tr><tr><td>\n\nDomain source filter\n\n</td><td>\n\nOptional condition you can specify to filter the records queried from the selected domain source table. 1.  Select the field to use for filtering records.\n2.  Add filtering conditions and operators to narrow the domain selection criteria. To learn more, see [Filters](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-user-interface/c_Filters.md).\n\n\n</td></tr></tbody>\n</table>    The following processing takes place when scheduling a job in a domain-separated instance:\n\n    -   It first queries the selected domain source table.\n    -   If you specified a condition in the **Domain source filter** field, it applies the filtering condition to narrow down the resulting set of records.\n    -   Finally, it retrieves its list of unique domains from the sys\\_domain columns of these records.\n6.  Select **Update** to update the scheduled job, **Execute Now** to execute the scheduled script immediately, or **Delete** to delete the job.\n\n\n**Parent Topic:**[Create a scheduled job](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/t_CreateAScheduledJob.md)\n\n**Related topics**  \n\n\n[Advanced options for scheduled jobs](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/advanced-options-for-scheduled-jobs.md)\n\n[Create a scheduled job](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/t_CreateAScheduledJob.md)\n\n[Enable run types for scheduled job child tables](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/customize-run-times-for-scheduled-jobs.md)\n\n[Personalize the system date format](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/t_PersonalizeTheSystemDateFormat.md)\n\n[Set a system time zone](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/t_SetASystemTimeZone.md)\n\n[Domain separation for service providers](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-security/domain-sep-landing-page.md)\n\n"}}