Script support for complex data
Summarize
Summary of Script support for complex data
This guide explains how to create and handle complex data structures using scripts within ServiceNow workflows, particularly when working with data from streams, REST responses, or Look Up Records steps. Scripting is essential for parsing and mapping these data sources into complex objects that workflows can utilize effectively.
Show less
Key Features
- Data Sources Requiring Script: Scripts are used to process data from Data Stream actions, REST step responses, and Look Up Records steps to create complex data objects.
- Parsing Data Streams: Use parser scripts with appropriate JavaScript methods (e.g., JSON - Scoped class) to map stream values into complex objects. Parser scripts access input, output, and targetObject properties.
- REST Step Responses: Convert REST responses to complex objects by creating script input variables mapped to the REST payload, then parsing with JavaScript (e.g., JSON.parse()). Complex data mapped to string inputs is auto-converted to JSON strings, eliminating the need for extra scripting to create REST requests.
- Look Up Records Step: Use Script steps with input variables to iterate through record lists using JavaScript loops. While Look Up Records can be processed with flow logic like For each, scripting allows more complex data transformations.
- Dot-Walking Object Structures: Access elements within complex objects by dot-walking through the object’s structure using property names (not labels). Arrays require specifying an element by its JavaScript index.
General Guidelines
- Map complex data directly to string inputs to leverage automatic JSON string conversion without scripting.
- Save complex object templates for reuse across actions, flows, and scripts, improving consistency and efficiency.
- Create script input variables to access data from prior steps or inputs, mapping them to appropriate data pills.
- Define script output variables matching your script’s complex data structures to store generated data.
- When producing complex data from custom actions, map action outputs to your script output variables to expose this data downstream.
Practical Application
ServiceNow customers can use these scripting techniques to efficiently handle complex data transformations within workflows, enabling integration with external data streams, REST APIs, and record sets. This empowers automation of data processing tasks that require detailed object manipulation and iteration beyond standard flow logic.
Examples include:
- Generating arrays of objects or strings from record lists using Script steps.
- Parsing REST responses dynamically to extract and structure data for further processing.
- Leveraging templates and dot-walking to maintain clear and reusable data models.
Create and reference complex data from a script. Use a script when your source data comes from a data stream, a REST step response, or a Look Up Records step.
Use script to create complex data when data comes from these sources.
| Data source | Create/map complex data from |
|---|---|
| Data Stream action response stream | Script Parser step |
| REST step response | Script step |
| Look Up Records step |
Data Stream action response stream
Data Stream actions use a parser script to map stream item values to complex object values. When writing a parser script, use JavaScript methods appropriate to the data stream format. For example, use the JSON - Scoped class to parse or encode a JSON data stream.
Parser scripts have access to the data stream input and output objects as well as a targetObject property. See Data Stream actions for more information about parsing a response stream to create complex data.
REST step response
You can convert a REST step response into one or more complex objects by parsing it with a Script step. To access a response from a Script step, you must create an input script variable and map it to the response payload from the prior REST step. See Script step for more information about creating script input variables.
Write a script that maps REST response values to complex object values. When writing REST response script, use JavaScript methods appropriate to the response format such as the JSON parse() method.
You do not need to use a Script step to create a REST request from complex data. You can generate complex data in a prior action or step and then map it to a string input of the REST step. At run time, the action or flow converts the complex data into a JSON representation.
For example, see the script steps used in Get started with dynamic inputs for the data gathering actions. The data gathering actions for getting table and field names both use a Script step to parse a REST response into a JSON object. Both data gathering actions also create output variables that store complex data as JSON objects.
Look Up Records step
While flows can use For each flow logic to process a list of records,
actions require a Script step. The Script step replaces the For each flow
logic with JavaScript such as a For or While loop.
To access record data from a Script step, you must create an input script variable and map it to the record data from the prior look up step. See Script step for more information about creating script input variables.
See Create a custom action to generate an array of objects from a list of records for an example action that converts a list of user records into an array of contact objects.
Dot-walking object structures
You can reference elements from the structure of an object by dot-walking the path of the structure. All complex data paths start with the name of the data source, which is either the global object for inputs, the global object for outputs, or the name of the array or object you created in script.
Next in the path are the names of each structural element referenced separated by period characters (also known as dots). Listing the names of structural elements is identical to dot-walking a reference field where you list the table structure to a particular reference field.
For example, suppose that you define a contact object as an Output variable. The object has the following structure.
| Place in structure | Label | Name | Type |
|---|---|---|---|
| Parent | Contact | contact | Object |
| Child | First name | first_name | String |
| Child | Last name | last_name | String |
| Child | Email Addresses | email_addresses | Array.Object |
| Grandchild | Email Address | email_address | Object |
| Great grandchild | Type | type | Choice |
| Great grandchild | String | ||
| Child | Telephone | telephone_number | Array.Object |
| Child | Mailing Addresses | mailing_address | Array.Object |
The dot-walk path to the First name structural element would be
outputs.contact.first_name while the path to the Email
structural element would be outputs.contact.email_addresses[0].email since you
must specify an individual element of the array by its JavaScript index value.
General guidelines
Keep these general guidelines in mind when scripting with complex data.
- Use string inputs to convert complex data into a JSON string
- When you map complex data to a string input, Workflow Studio automatically converts it into a JSON string. Instead of writing a script, you can add a string input to a REST step and map it to complex data from a prior action or step.
- Save your objects as templates
- Save your objects as templates so you can reuse them in other actions, flows, and Script steps.
- Create script input variables to access prior data
- Create a script input variable for any data you want to access from the action input or a prior step. Map the script input variable to the input or step data pill. For example, map the script input variable to a list of user records you looked up in a prior step.
- Create a script output variable to store complex data
- Create a script output variable to store any complex data your script creates. The script output variables must match the values defined in the script. For example, create a contacts array of objects to store multiple contact objects. Save the contact object as a template so you can reuse it.
- Map the action output to the script output variable
- When you want a custom action to output complex data, add an action output and map it to the data pill for your Script step output variable. For example, create a contacts array and load the contact object template you saved earlier. Map the action output to the contacts array produced by your Script step.