Execute Background Script
execute_scriptExecute JavaScript on a ServiceNow instance using background scripts with full GlideSystem API access and optional parameter substitution.
Instructions
Execute JavaScript on a ServiceNow instance using Scripts - Background (the /sys.scripts.do endpoint). The script runs server-side with full GlideSystem API access (gs, GlideRecord, GlideAggregate, GlideDateTime, GlideUser, etc.). Use gs.print() or gs.info() to produce output.
SCOPE BEHAVIOR: Scripts execute within the specified application scope. When running in a scoped app (e.g., scope: 'x_myapp_custom'), you can reference that scope's Script Includes and classes directly by name (e.g., MyUtil.doSomething()) without fully-qualifying them. When running in global scope, scoped classes must be fully-qualified (e.g., x_myapp_custom.MyUtil.doSomething()).
IMPORTANT: This executes code directly on the ServiceNow instance. Always review scripts before execution and prefer read-only operations unless modification is explicitly intended.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| instance | No | The ServiceNow instance auth alias to execute the script on. This is the alias configured via `now-sdk auth --add` (e.g., "myinstance", "prod", "test"). The user will typically refer to this by name when saying things like "on my myinstance instance". If not provided, falls back to the SN_AUTH_ALIAS environment variable. | |
| script | Yes | The JavaScript code to execute on the ServiceNow instance. Use gs.print() or gs.info() to output results — these are the only ways to capture output from background scripts. The script runs in the server-side Rhino engine with access to all ServiceNow server-side APIs: GlideRecord, GlideAggregate, GlideDateTime, GlideUser, gs.getUser(), gs.now(), GlideSysAttachment, and more. Scripts execute with the permissions of the authenticated user. | |
| scope | No | The application scope to execute the script in. Accepts either: - A scope name (e.g., "global", "x_myapp_custom", "x_snc_app") — automatically resolved to the corresponding sys_id via the sys_scope table. - A sys_id directly (32-character hex string) if already known. Defaults to "global". Set this to the target app scope when you need to access scoped Script Includes, Business Rules, or other scoped artifacts by their unqualified names. | global |
| params | No | Optional key-value pairs for parameter substitution in the script. Every occurrence of {paramName} in the script text will be replaced with the corresponding value before execution. Useful for safely injecting dynamic values without string concatenation in the script. Example: { "table": "incident", "field": "priority" } replaces {table} and {field} in the script. |