Create a custom action to generate an array of strings from a list of records

task brazil Updated Sep 10, 2026 4 min read create-custom-action-array-strings.md
Raw file servicenow.com

Generate an array of strings from a list of User Role records. Learn how to use a Script step to iterate through a list of records.

Before you begin

Role required: admin of flow_designer

About this task

Use this example to see demonstrations of these operations and steps.

  • Create an action input for a Role record.
  • Look up a maximum of three User Role records that have the Role action input.
  • Configure a Script step to process a list of User Role records.
  • Create a script input variable containing the list of User Role records.
  • Write script that creates an empty users array.
  • Write script that iterates through the list of User Role records.
  • Write script that populates the users array with the current user field value.
  • Create script output variables for the users array and child user string.
  • Output the generated users array of strings as a data pill.
  • Test the action with three sample users.

Procedure

  1. Create an application to store your work.

    You can use App Engine Studio to plan, create, and deploy applications. For more information about building a custom application, see .

    For example, create an application called My Application.

  2. Navigate to All > Process Automation > Workflow Studio.

  3. On the homepage, select Actions.

  4. Select New > Action

    The system displays the Action Properties dialog.

  5. Enter these sample values.

    Field Value
    Name Create Users With Role Array Of Strings
    Application Global
    Accessible From All application scopes

    Note: If you created an application to store and deploy your custom action, use that application instead of global.

  6. Select Build action.

    The system displays the Workflow Studio interface.

  7. From the Action Outline, select Inputs > Create Input

    The system displays a new action input.

  8. Configure the action input with these values.

    [Omitted image "array-strings-config-inputs.png"] Alt text: Create an action input that stores a role record.

    Field Value
    Label Role
    Type Reference.Role [Reference.sys_user_role]
    Mandatory True
  9. From the Action Outline, select Add a new step.

    The system displays a list of available steps.

  10. Select Look Up Records

  11. Configure the step with these values.

    [Omitted image "array-strings-config-look-up-records-step.png"] Alt text: Configure the Look Up Records step condition to use the role input variable.

Field

Value

Table

User Role [sys_user_has_role]

Conditions

[Role][is][action->Role]Note: Select the Role data pill from the Input Variables.

Order by

Role

Sort Type

a to z

Max Results

3

**Note:** This example limits the **Max Results** setting to three records just for demonstration purposes.
  1. From the Action Outline, select Add a new step.

    The system displays a list of available steps.

  2. Select Script.

  3. From the Input Variables section, select Create Variable.

  4. Configure the input variable with these values.

    [Omitted image "array-strings-config-script-step-input-variable.png"] Alt text: Configure the Script step input variable to use the User Role records data pill from the Look Up Records step.

Field

Value

Name

userRoleRecords

Value

[step->Look Up Records step->User Role records]Note: Select the User Role records data pill from the Look Up Records step.

**Note:** You can select the **User Role records** data pill from the data panel or from the Data Pill Picker button.
  1. For Script, enter the following text.

    JavaScript
    (function execute(inputs, outputs) {  //Create an empty array  var usersArray = [];  var i = 0;  //Iterate through the list of User Role records  while(inputs.userRoleRecords.next()) {    //Query User Role records for user field value    var user = inputs.userRoleRecords.getDisplayValue('user');    //Add current user name to array    usersArray[i] = user;    i += 1;  }  outputs.users = usersArray;})(inputs, outputs);
  2. From Output Variables, select Create Variable.

  3. Configure the output variable with these values.

    [Omitted image "array-strings-config-script-step-output-variable.png"] Alt text: Create an array of strings called users. Within the array create a string variable called user.

    Label Name Type Mandatory
    users users Array.String True
  4. Expand the users Array.String, and rename the child string to user.

  5. Configure the child item with these values.

    Label Name Type Mandatory
    user user String False
  6. From the Action Outline, select Outputs > Create Output.

  7. Configure the Action Output with these values.

    [Omitted image "array-strings-config-outputs.png"] Alt text: Create an action output called users to store an array of strings.

    Label Name Type Mandatory
    users users Array.String True
  8. Expand the users Array.String, and rename the child string to user.

  9. Configure the child item with these values.

    Label Name Type Mandatory
    user user String False
  10. Select Exit Edit Mode.

    The System displays the output fields you created.

  11. For users, select [step->Script step->users].

    [Omitted image "array-strings-config-outputs-users.png"] Alt text: Configure the action output variable to use the users data pill from the Script step.

    Note: You can select the Script step users data pill from the data panel or from the Data Pill Picker button.

  12. Click Save.

  13. Select Test.

    The system displays the Test Action dialog.

  14. Enter the following test value:

    [Omitted image "array-strings-test-action-role.png"] Alt text: Select the admin role to test your action.

    Input Value
    Role admin
  15. Select Run Test.

    The system runs the action with the test values provided.

  16. Select Your test has finished running. View the Action execution details.

    The system displays the action execution details.

  17. Review the runtime value for the action Output data.

    [Omitted image "array-strings-execution-details.png"] Alt text: Execution details page listing three user names in the users array.

    The system displays output data in JSON format.

    For this example, the users array contains three users who have the admin role.

    JSON
    {    "users": [        "System Administrator",        "Rob Phillips",        "Fred Luddy"    ]}

Result

You have a custom action that looks up the Users who have a given role and converts those users into an array of user name strings.

What to do next

Customize this action to use your own logic.

Parent Topic:Script support for complex data

Search documentation
Search document titles and file paths on the brazil branch