Script step
Summarize
Summary of Script step
The Script step in Workflow Studio allows ServiceNow customers to add custom JavaScript within reusable actions. This capability extends beyond the core action steps, enabling execution of specific behaviors tailored to unique business needs. It is accessible to users with theactiondesignerrole and is available as a Workflow Studio action step.
Show less
Note that using the Script step with a MID Server requires an Integration Hub subscription.
Key Features
- Runtime Environment Options: Choose where the script runs:
- Instance: Default; runs on the ServiceNow instance with full API and data access.
- MID Server: Runs on a MID Server for access to MID Server scripts and APIs; requires configuring MID Server selection.
- Vanilla (Core JavaScript): Runs on instance or MID Server but limited to core JavaScript APIs, no ServiceNow API access.
- MID Server Selection: If MID is chosen, you can specify how to select the MID Server:
- Any available MID Server
- Connection Alias—select a predefined connection alias to manage credentials centrally
- Inline Selection—define host, application, and capabilities directly
- Input and Output Variables: Define inputs to receive data from the action and outputs to expose data back to the workflow. These variables map JavaScript data to Workflow Studio data pills, enabling integration with other steps.
- Script Development: Use global
inputsandoutputsobjects to access variables within the script. Avoid naming inputs with reserved system field names to prevent conflicts. Scripts convert data to strings by default but can handle JSON string manipulation. - Error Handling: Configure whether the workflow continues or evaluates errors if the script step fails, supporting robust error management.
Practical Application
This step is ideal when existing core steps do not fulfill specific scripting needs within an action. For example, you can create JSON payloads in your script and pass them as outputs to subsequent REST steps (which require Integration Hub). It enables dynamic and flexible data processing directly within Workflow Studio.
Usage Considerations
- Running scripts on a MID Server requires Integration Hub subscription and proper MID Server configuration.
- Workflow Studio executes scripts within the domain context that triggers the workflow, maintaining domain separation.
- Use connection aliases where possible to simplify credential management across multiple environments.
Add custom JavaScript to execute within a reusable action. While most core actions and steps fit common use cases, you can build a Script step to execute behavior not satisfied by the core steps.
Roles and availability
Fields
The Script step includes separate input and output variables that enable you to map JavaScript data to Workflow Studio data. By defining input and output variables within the step, you can define what Workflow Studio data is available within your script, and which scripting variables are available to other steps in your action.
| Field | Description |
|---|---|
| Required Runtime | The runtime environment required to support the script.
Choices include:
The runtime you select determines the JavaScript objects and methods displayed in the Context-sensitive help. Note: This field is only visible when Integration Hub is activated. |
| Select MID Server Using | Specify the MID Server selection process to use. Choices include:
Note: This field is only visible when Integration Hub is activated, and you select MID from Required Runtime. |
| Connection Alias | Connection & Credential alias record that the system uses to run the action step. Users with the flow_designer or admin role can create or select an associated Connection record.
Using an alias eliminates the need to configure multiple credentials and connection information profiles when using an action in multiple environments. Likewise, if the connection information changes, you don't
need to update your custom action. To learn more about connections and credentials, see credentials, connections, and aliases. Only aliases of connection type Basic are supported. Note: This field is only visible when Integration Hub is activated, and you select Use Connection Alias from Select MID Server Using. |
| Host | The fully-qualified domain name of the MID Server where the system runs the action step. For example, mid-server.domain.com.
Note: This field is only visible when Integration Hub is activated, and you select Use Inline Selection from Select MID Server Using. |
| MID Selection | Option to select a specific MID Server or MID Cluster. Choose any one of the following options.
|
| MID Cluster | Data pill for the MID Cluster you want to use. This field is available when MID is selected from the Required Runtime list, and Use Inline Selection is selected from the Select MID Server Using list. |
| MID Application | Specify the application the MID Server must support to be eligible for selection. The system runs the action step from a MID Server that supports the selected application. This field is only visible when Integration Hub is activated, Auto-Select MID Server is selected from the MID Selection list, and you select Use Inline Selection from Select MID Server Using. |
| Capabilities | Capabilities the MID Server must support to be eligible for selection. The system runs the action step from a MID Server that supports the selected capabilities. This field is only visible when Integration Hub is activated, Auto-Select MID Server is selected from the MID Selection list, and you select Use Inline Selection from Select MID Server Using. |
| Specific MID Server | Data pill of the required MID Server. This field is only visible when Integration Hub is activated, Specific MID Server is selected from the MID Selection list, and you select Use Inline Selection from Select MID Server Using. |
| Input variables | Name-value pairs that represent data from the action, enabling you to use action inputs and data from other steps within a script. |
| Script | Script that executes within the action. To access input and output variables in your script, use the global objects inputs and outputs. For example,
inputs.myVariable.Note: Script
step input and output names can't include any of the following reserved system names:
The Script step always converts data stored in the inputs and outputs global objects into strings. If your Script step needs to work with JSON data, you can use the
inputs global object to convert the JSON data into a string. Alternatively, you can define a JavaScript variable as a string rather than a JavaScript object. For example, this script
illustrates two ways you can output JSON data.
By default, Workflow Studio run scripts on the instance. To run script from a MID Server requires an Integration Hub subscription. Workflow Studio runs script from the domain from which it is triggered or initiated. See Domain separation and Workflow Studio. For available classes and methods, see the JavaScript API context-sensitive help or the API reference. |
| Output variables | Map JavaScript output to Workflow Studio data pills. Define output variables when you want other steps in the action to use the script output. |
Action error evaluation
- If this step fails
- Data type: Choice
Option to continue running the next step or go to error evaluation. To use the step status code or message for a custom action error condition, see Action error evaluation.
Example
This example builds a JSON payload that can be easily updated or changed and added to a subsequent REST step.
By creating an output variable that represents the payload, you can drag the [Payload] data pill into the REST step Body field.