Get started with dynamic outputs
Create a sample action that builds dynamic outputs for use in a flow.
Before you begin
Role required: action_designer or admin
Procedure
Create connection and credential records for dynamic outputs
This connection & credential alias will provide the base URL and user account needed to configure the REST steps of your data gathering actions.
Create a data gathering action to get a record schema
This data gathering action will convert a single record into a JSON object for a record dynamic output.
Create a data gathering action to get an array of records schema
This data gathering action will convert a list of records record into JSON array of objects for a records dynamic output.
Create a custom action to test dynamic outputs
This custom action illustrates two types of dynamic output data. One dynamic output generates an object for a single record. Another dynamic output generates an array of objects for a list of records.
Parent Topic:Dynamic outputs
Related topics
Create a data gathering action for a dynamic object
Dynamic output configuration options
Create connection and credential records for dynamic outputs
Create the aliases, connections, and credentials that REST steps will use to connect to your local instance.
Before you begin
Role required: admin
Procedure
Navigate to All > Connections & Credentials > Credentials.
Select New, select Basic Auth Credentials, and enter these field values.
For Name, enter
Local Admin.For User name, enter a user account with access to Flow Designer and the REST API.
For example, enter
admin.For Password, enter the account password.
Select Submit to create the credential record.
Navigate to All > Connections & Credentials > Connection & Credential Aliases.
Select New and enter these fields values.
For Name, enter Local Instance.
Accept the default value of HTTP for the Connection type.
Select Submit to create the Connection & Credential Alias record.
Select the alias you created.
For example, select Local Instance.
From the Connections related list, select New, and enter these field values.
For Name, enter
My Instance.For Credential, select the basic authentication credential record you created.
For example, select the Local Admin credential.
For Connection URL, enter the base URL for your instance including the forward slash at the end.
Include the URL prefix https:// and add a slash character at the end of the URL.
For example,
https://example.service-now.com/.Select Submit to create the HTTP(s) Connection record.
Create a data gathering action to get a record schema
Create a data gathering action to look up a table schema and convert into a JSON object.
Before you begin
Role required: action_designer or admin
About this task
In this task, you create a data gathering action that collects the schema for a record on your instance. The goal is to create a complex object for use as a dynamic output. This data gathering action consists of the following:
- A REST step to gather table schema data for a selected table. The REST step Response Body is in JSON format.
- A script step to transform the REST step's JSON Response Body into a dynamic object. The dynamic object consists of JSON name-value pairs, where there is an entry for each field in the table.
- An output variable named
outputof type JSON to store the dynamic object.
Note: This task re-creates the demo actions that are installed when you Request an Integration Hub plugin for your instance.
Procedure
Navigate to All > Process Automation > Workflow Studio.
On the homepage, select Actions.
Select New and select New Action.
On the Action Properties screen, in the Name field, enter
Get ServiceNow Object Schema (Dynamic).Select Submit.
In the Action Outline, select Inputs.
In the Action Input header, select Create Input.
In the Label and Name fields, enter
Table.In the Type field, select
String.To make this input required, toggle the Mandatory slider so that it is active.
In the Action Outline, select the add a new step icon (
Add a new step icon\) under Inputs and select **REST Step**.
Under the REST step header, fill in the following fields.
Field Value Connection Leave the Use Connection Alias option selected. Connection Alias select the create new record icon (
Create new record icon\) to create a new [Create an HTTP\(s\) connection](../../platform-security/connections-and-credentials/create-https-connection.md), or use an existing connection for your instance. The **Credential** for the HTTP\(s\) connection must use [Basic authentication credentials](../../platform-security/connections-and-credentials/r_BasicAuthCredentialsForm.md). Additionally, the **Connection URL** must be the base URL for your instance, including the forward slash at the end. For more information on connections and credentials, see [Getting started with connections](../../platform-security/connections-and-credentials/connection-information.md) and [Getting started with credentials](../../platform-security/connections-and-credentials/credentials-getting-started.md).|
|Build Request|Leave the **Manually** option selected.|
|Resource Path|Enter `api/now/processflow/table/` and then select the data pill picker \(
Data pill picker\). Select **Inputs** > **Table**. Next, enter `/schema`.|
|HTTP Method|Enter `GET`|
|Query Parameters|select the plus icon \(
Plus icon to add a new query parameter. Then, in the **Name** field, enter `get_choices` and `true` in the **Value** field.|
- In the Action Outline, select the add a new step icon (
Add a new step icon\) under your REST step and select the **Script** step.
1. In the Input Variables section, select **Create Variable**.
2. In the **Name** field, enter `payload`.
3. Next to the **Value** field, select the data pill picker \(
Data pill picker\) and select **REST Step** > **Response Body**.
4. In the **Script** field, enter the following code.
```
(function execute(inputs, outputs) {
var payload = JSON.parse(inputs.payload);
var columns = payload.result.data.columns;
var schema = columns.map(function(column) {
var value = {
label: column.label,
name: column.name,
type: getCOType(column.definition.base_type),
};
if (column.definition.type === 'choice') {
value.type = 'choice';
value.choices = column.definition.choices;
}
if (column.definition.base_type === 'GUID') {
value.children = [
{ label: 'Link', name: 'link', type: 'string' },
{ label: 'Value', name: 'value', type: 'string' },
];
}
return value;
});
outputs.schema = {
data: {
type: 'object',
children: schema,
},
};
function getCOType(type) {
if (type === 'GUID') return 'reference';
return type;
}
})(inputs, outputs);
```
5. In the Output Variables section, select **Create Variable**.
6. In the **Label** and **Name** fields, enter `schema`.
7. In the **Type** field, select `JSON`.
In the Action Outline, select Outputs.
In the Action Output header, select Create Output.
In the Label and Name fields, enter
output.In the Type field, select
JSON.In the Action Output header, select Exit Edit Mode.
Next to the Value field, select the data pill picker (
Data pill picker\) and select **Script Step** > **schema**.
In the Action header, select Save and then select Test to test the action.
On the Test Action screen, enter
incidentfor the Table input.Select Run Test.
Check the action's execution details.
Your data gathering action runs successfully if the runtime value for
fieldsis a complex object in a format that is similar to the following abbreviated example.{ "data": { "type": "object", "children": [ { "name": "active", "label": "Active", "type": "boolean" }, { "name": "activity_due", "label": "Activity due", "type": "datetime" }, ...In the Action header, select Publish to make the Get ServiceNow Object Schema (Dynamic) action available to other flows and actions within the Global scope.
Create a data gathering action to get an array of records schema
Create a data gathering action to generate an array of objects from a list of records.
Before you begin
Role required: action_designer or admin
About this task
In this task, you create a data gathering action that collects the schema for a record on your instance. The goal is to create a complex object for use as a dynamic output. This data gathering action consists of the following:
- A REST step to gather table schema data for a selected table. The REST step Response Body is in JSON format.
- A script step to transform the REST step's JSON Response Body into a dynamic object. The dynamic object consists of a JSON array of objects, where each source record is converted into one object of the array.
- An output variable named
outputof type JSON to store the dynamic object.
Note: This task re-creates the demo actions that are installed when you Request an Integration Hub plugin for your instance.
Procedure
Navigate to All > Process Automation > Workflow Studio.
On the homepage, select Actions.
Select New and select New Action.
On the Action Properties screen, in the Name field, enter
Get ServiceNow Array.Object Schema (Dynamic).Select Submit.
In the Action Outline, select Inputs.
In the Action Input header, select Create Input.
In the Label and Name fields, enter
Table.In the Type field, select
String.To make the input required, toggle the Mandatory slider so that it is active.
In the Action Outline, select the add a new step icon (
New step icon\) under Inputs and select the **REST** step.
Under the REST step header, fill in the following fields.
Field Value Connection Leave Use Connection Alias selected. Connection Alias select the create new record icon (
Create new record icon\) to create a new [Create an HTTP\(s\) connection](../../platform-security/connections-and-credentials/create-https-connection.md), or use an existing connection for your instance. The **Credential** for the HTTP\(s\) connection must use [Basic authentication credentials](../../platform-security/connections-and-credentials/r_BasicAuthCredentialsForm.md). Additionally, the **Connection URL** must be the base URL for your instance, including the forward slash at the end.|
|Build Request|Leave **Manually** selected.|
|Resource Path|Enter `api/now/processflow/table/` and then select the data pill picker \(
Data pill picker\). Select **Inputs** > **Table**. Finally, enter `/schema`|
|HTTP Method|Enter `GET`|
|Query Parameters|select the plus icon \(
Plus icon to add a new query parameter. Then, in the **Name** field, enter `get_choices` and `true`in the **Value** field.|
- In the Action Outline, select the Add a new step(
New step icon\) icon under your REST step and select the **Script** step.
1. In the Input Variables section, select **Create Variable**.
2. In the **Name** field, enter `payload`.
3. Next to the **Value** field, select the data pill picker \(
Data pill picker\) and select **REST Step** > **Response Body**.
4. In the **Script** field, enter the following code.
```
(function execute(inputs, outputs) {
var payload = JSON.parse(inputs.payload);
var columns = payload.result.data.columns;
var schema = columns.map(function(column) {
var value = {
label: column.label,
name: column.name,
type: getCOType(column.definition.base_type),
};
if (column.definition.type === 'choice') {
value.type = 'choice';
value.choices = column.definition.choices;
}
return value;
});
outputs.schema = {
data: {
type: 'array.object',
attributes: {
child_type: 'object',
},
children: schema,
},
};
function getCOType(type) {
if (type === 'GUID') return 'string';
return type;
}
})(inputs, outputs);
```
5. In the Output Variables section, select **Create Variable**.
6. In the **Label** and **Name** fields, enter `schema`.
7. In the **Type** field, select `JSON`.
In the Action Outline, select Outputs.
On the Action Output header, select Create Output.
Enter
outputin the Label field and Name field.Select
JSONfor the Type field.Select Exit Edit Mode.
Next to the Value field, select the data pill picker (
Data pill picker\) and select **Script Step** > **schema**.
In the Action header, select Save and then select Test to test the action.
On the Test Action screen, in the Table field, enter
incident.Select Run Test.
Check the action's execution details.
Your data gathering action runs successfully if the runtime value for
fieldsoutput is a complex object that contains an array of key-value pairs forlabel,name, andvalueas shown in the following abbreviated example.{ "data": { "type": "array.object", "children": [ { "name": "active", "label": "Active", "type": "boolean" }, { "name": "activity_due", "label": "Activity due", "type": "datetime" }, ...
In the Action header, select Publish to make the Get ServiceNow Array.Object Schema (Dynamic) action available to other actions within the Global scope.
Create a custom action to test dynamic outputs
Create a sample action to dynamically generate two action outputs, Record and Records which refresh dynamically when the value for the Table input changes.
Before you begin
Role required: action_designer or admin
About this task
This custom action uses two data gathering actions to populate dynamic outputs.
Procedure
- In the main header, select the create flow, subflow, or action icon (
Create flow, subflow, or action icon\) and select **Action**.
1. In the Action Properties modal, in the **Name** field, enter `Get ServiceNow Records (Dynamic)`.
2. Select **Submit**.
In the Action Outline, select Inputs.
In the Action Input header, select Create Input.
In the Label and Name fields, enter
Table.In the Type field, select
Dynamic Choice.To make the input required, toggle the Mandatory slider so that it is active.
Select the Toggle advanced inputs icon (
Toggle advanced inputs icon to display the advanced options for the `Table` input.
6. In the **Default value** field, enter `incident`.
7. Under Dynamic Options, in the **Action** field, select **Get ServiceNow Tables - Dynamic**.
8. Select **Create Input** to create another action input.
9. In the **Label** and **Name** fields, enter `NumberOfRecords`.
10. In the **Type** field, select **Integer**.
11. To make the input required, toggle the **Mandatory** slider so that it is active.
12. Select the Toggle advanced inputs icon \(
Toggle advanced inputs icon to display the advanced options for the `Table` input.
13. In the **Default value** field, enter `3`.
- In the Action Outline, select the add a new step icon (
New step icon\) under Inputs and select the **REST** step.
Under the REST step header, fill in the following fields.
Field Value Connection Leave Use Connection Alias selected. Connection Alias Select the create new record icon (
Create new record icon\) to create a new [Create an HTTP\(s\) connection](../../platform-security/connections-and-credentials/create-https-connection.md), or use an existing connection for your instance. The **Credential** for the HTTP\(s\) connection must use [Basic authentication credentials](../../platform-security/connections-and-credentials/r_BasicAuthCredentialsForm.md). Additionally, the **Connection URL** must be the base URL for your instance, including the forward slash at the end.|
|Build Request|Leave **Manually** selected|
|Resource Path|Enter `api/now/table/` and then select the data pill picker \(
Data pill picker\). Select **Inputs** > **Table**.|
|HTTP Method|Enter `GET`|
|Query Parameters|Select the plus icon \(
Plus icon to add a new query parameter. Then, enter `sysparm_limit` in the **Name** field. Next to the **Value** field, select the data pill picker \(\[Omitted image "data\_pill\_picker.png"\] Alt text: Data pill picker\) and then select **Inputs** > **NumberOfRecords**.|
- In the Action Outline, select the add a new step icon (
New step icon\) under **Inputs** and select the **Script** step.
1. In the Input Variables section, select **Create Variable**.
2. In the **Name** field, enter `payload`.
3. Next to the **Value** field, select the data pill picker \(
Data pill picker\) and select **REST Step** > **Response Body**.
4. In the **Script** field, enter the following code.
```
(function execute(inputs, outputs) {
var response = JSON.parse(inputs.payload);
var records = response.result;
outputs.record = records[0];
outputs.records = JSON.stringify(records);
})(inputs, outputs);
```
5. In the Output Variables section, select **Create Variable**.
6. In the **Label** and **Name** fields, enter `record`.
7. Select **JSON** for the **Type** field.
8. Toggle the **Mandatory** slider so that it is active.
9. Select **Create Variable** to create another output variable for the script step.
10. In the **Label** and **Name** fields, enter `records`.
11. In the **Type** field, select `JSON`.
12. To make the input required, toggle the **Mandatory** slider so that it is active.
In the Action Outline, select Outputs.
In the Action Output header, select Create Output.
In the Label and Name fields, enter
Records.In the Type field, select Dynamic Object.
Select the Toggle advanced inputs icon (
Toggle advanced inputs icon to display the advanced options for the `Records` output.
5. Under Dynamic Options, select **Get ServiceNow Array.Object Schema \(Dynamic\)** as the **Action**.
6. To make the Table input dependent on another input, toggle the **Depends-On Another Input** slider to make it active.
7. In the **Table**e field, select **Table**.
8. In the Action Output header, select **Exit Edit Mode**.
9. Next to the **Value** field, select the data pill picker \(
Data pill picker\) and select **Script Step** > **records**.
10. In the Action Output header, select **Edit Outputs** > **Create Output** to create another action output.
11. In the **Label** and **Name** fields, enter `Record`.
12. In the **Type** field, select `Dynamic Object`.
13. Select the Toggle advanced inputs icon \(
Toggle advanced inputs icon to display the advanced options for the `Record` output.
14. Under Dynamic Options, in the **Action** field, select **Get ServiceNow Object Schema \(Dynamic\)**.
15. To make the Table input dependent on another input, toggle the **Depends-On Another Input** slider to make it active.
16. In the **Table** field, select **Table**.
17. In the Action Output header, select **Exit Edit Mode**.
18. For the **Value**, select the data pill picker \(
Data pill picker\) and select **Script Step** > **record**.
In the Action header, select Save and then select Test to test the action.
On the Test Action screen, select any dynamically generated choice value for the Table input.
Select Run Test.
Check the action's execution details.
Your action runs successfully if the runtime value for
Recordis a properly formatted complex object and the runtime value forRecordsis a properly formatted complex object array.
In the Action header, select Publish to make the Get ServiceNow Records (Dynamic) action available to flows within the Global scope.
Result
Use the Get ServiceNow Records Dynamic sample action in a flow.
You can now add the Get ServiceNow Records (Dynamic) action to a flow. This sample action dynamically generates two action outputs, Record and Records, which can be accessed as data pills in the data panel. The data pills refresh dynamically when the value for the Table input changes.