Execute Background Script
execute_scriptRuns server-side JavaScript on a ServiceNow instance through Scripts - Background, with optional app scope and parameter substitution, and captures output via gs.info().
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.info() to produce output (gs.print() also works in global, but prints nothing in a scoped app).
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()). Only 'global' and applications developed on the instance (sys_app) can be used; installed store apps (sys_store_app) cannot — run in global and call their APIs fully-qualified. In a scoped app, use gs.info() for output: gs.print() is global-only.
IMPORTANT: This executes code directly on the ServiceNow instance. Always review scripts before execution and prefer read-only operations unless modification is explicitly intended.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"script": "<script>"
},
"name": "execute_script"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scope | No | The application scope to execute the script in. Accepts: - "global" — the Global scope. - The scope of an application developed on the instance (a sys_app, e.g. "x_myapp_custom") — resolved to its sys_id via the sys_app table. Installed store apps (sys_store_app) are rejected with an explanation. - 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. | |
| script | Yes | The JavaScript code to execute on the ServiceNow instance. Use gs.info() (or, in global only, gs.print()) 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. | |
| 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. |