Enable users with coding experience to
write inline scripts that set and modify input values during the configuration
of an action or flow. Use inline scripts to modify input values that require
small format conversions, data transformations, or math operations.
Flow Designer displays a Script button
when you configure these components.
Action instance inputs when you configure the action for a flow
Action instance outputs when you configure the action for a flow
Flow logic inputs when you configure the flow logic for a flow
Flow logic outputs when you configure the flow logic for a flow
Step inputs when you configure the step for an action.
Subflow inputs when you configure the subflow for a flow.
Subflow ouputs when you configure the subflow for a flow.
Inline scripts must return values in the same data type as the input expects. For example, an
inline script for a Record input must return a GlideRecord object and an inline script for a
Date input must return a date-time value. Always test actions and flows containing inline
scripts, and verify that there are no runtime errors in the flow execution details.
Script writers should be familiar with Now Platform table structures and field types . In addition, they should know how to work with record and system data using
the ServiceNow API.
Benefits
Inline scripts offer these benefits.
Enable simple data conversion or transformation without having to create custom
actions or flows.
Identify which input data a script affects.
Restricted access to scripting features to users or groups who are knowledgeable with
the available ServiceNow
APIs.
Access to inline script
You can grant users access to online scripting by either granting them the
flow_designer_scripting role or the Allow Scripting delegated
development permission. Both the role and the developer permission display a script button
field for each Flow Designer
input.
Script button
When you enable a user to create inline scripts, Flow Designer displays a script button
next to flow and step inputs.
Figure 1. Script buttons display beside inputs
Clicking the Script button opens the Script editor, which replaces
the standard input interface. Enter a script to compute the input value. Script writers can
use the fd_data
object to access data from previous actions and steps.
Ensure your script includes a return
statement with the results of your
script. For example, return shortDesc;
returns the value of the
shortDesc
variable.
Figure 2. Script editor for input
Clicking the Collapse Script button hides the Script editor and
displays a read-only version of the input. Clicking the Expand Script
button displays the Script editor and allows you to edit the script.
Figure 3. Input containing script
Design considerations
Follow these design guidelines to create reusable and maintainable inline scripts.
Write inline script for small non-reusable logic
Use inline script to format or modify the data for specific inputs and use cases. For
reusable logic, create an action or subflow instead.
Create custom actions or subflows for reusable code rather than inline script
Create custom actions or subflows for reusable or complex data logic such as changing
the data type of source data. You may also want to provide custom actions or subflows
for flow designers who are not comfortable with code.
Avoid duplicating action and flow functionality
Avoid writing inline script that duplicates action and flow functionality. For
example, rather than write inline script to perform record operations, use the create
and update record baseline actions.
Avoid data type changes
Avoid runtime errors by verifying that your inline script provides information in the
same data type as the input or output expects.
Create variables by declaring them with the var keyword
Use the var
keyword to declare variables so that they remain within
the proper JavaScript scope. When you create a variable by assigning it a value,
JavaScript may attach it to the global object, which can result in variable values
persisting outside of the local scope and causing errors.
Process records outputs with For Each flow logic and the flow data object
Inline script can only access the records output of a Look Up
Records action from For Each flow logic. Add a Look Up Records action to the flow to
generate the records output. Add a For Each flow logic to the flow to process each
record in the records output. Create an inline script reference to the For Each flow
logic using the fd_data and item objects. For example, this reference assumes the For
Each flow logic is the second item in your flow outline
fd_data._2__for_each.item
.
Use type ahead suggestions to generate references to flow and action data.
Create references to flow and action data using the fd_data object. The script editor
displays type ahead suggestions for existing flow and action data when you type
fd_data . Select a suggestion to build references to flow and
action data. Note: Refer to record data in a For Each flow logic using the
item object.
Licensing considerations
Inline scripts that call integration APIs are subject to IntegrationHub licensing.
Code editor
The code editor provides text editor support for inline
scripts.
The code editor has these features for the supported language
services and
scripts .
Syntax coloring, indentation, line numbers, and automatic creation of closing braces and
quotes
Auto-suggestions and auto-completions
Figure 4. Code editor
Editing tips
To insert a fixed space anywhere in your code, press Tab.
To indent a single line of code, click in the leading white space of the line and then
press Tab.
To indent one or more lines of code, select the code and then press Tab. To decrease the
indentation, press Shift+Tab.
To remove one tab from the start of a line of code, click in the line and press
Shift+Tab.
To declare variables, use the var
keyword so that they remain within the
proper JavaScript scope.