---
sourceDocument: Australia Build workflows
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/build-workflows

 Release :

    - australia

ft:locale :

    - en-US

ft:publication_title :

    - Australia Build workflows

ft:clusterId :

    - crworkflow

bundleId :

    - crworkflow

workflow :

    - Creator


---

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

# Create a custom action to generate an array of objects from a list of records {#ariaid-title1}

* Release version: Australia
* 
* Updated March 12, 2026
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 5 minutes to read

Generate an array of objects from a list of User 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 Department record.
* Look up a maximum of three User records for the Department action input.
* Configure a Script step to process a list of User records.
* Create a script input variable containing the list of User records.
* Write script that creates an empty contacts array.
* Write script that iterates through the list of User records.
* Write script that creates a contact object and maps User record field values to the contact object.
* Write script that populates the contacts array with the current contact object.
* Create script output variables for the contacts array and child contact object.
* Save the contact object as a template.
* Output the generated contacts array of objects as a data pill.
* Test the action with a sample department.
{#create-custom-action-array-objects__ul_c5v_rdw_lhb}

## Procedure

1. **Optional:** 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 [Building apps in App Engine Studio](https://www.servicenow.com/docs/access?context=aes-app-creation&version=australia&pubname=australia-application-development&ft:locale=en-US).  
   For example, create an application called <kbd class="ph userinput">My Application</kbd>.
2. Navigate to AllProcess AutomationWorkflow Studio.
3. On the homepage, select Actions.
4. Select NewAction  
   The system displays the Action Properties dialog.
5. Enter these sample values.  
   {#create-custom-action-array-objects__table_tjt_xxp_lhb__entry__2}

   | Field | Value |
   |-|-|
   | Name | Create Contacts Array Of Objects |
   | Application | Global |
   | Accessible From | All application scopes |
   [ ]

   {#create-custom-action-array-objects__table_tjt_xxp_lhb}  
   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 InputsCreate Input  
   The system displays a new action input.
8. Configure the action input with these values.  
   {#create-custom-action-array-objects__table_ijj_myp_lhb__entry__2}

   | Field | Value |
   |-|-|
   | Label | Department |
   | Type | Reference.Department \[Reference.cmn_department\] |
   | Mandatory | True |
   [ ]

   {#create-custom-action-array-objects__table_ijj_myp_lhb}
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.  
    {#create-custom-action-array-objects__table_cvp_l5p_lhb__entry__2}

    | Field | Value |
    |-|-|
    | Table | User \[sys_user\] |
    | Conditions | \[Department\]\[is\]\[action-\>Department\] Note: Select the Department data pill from the Input Variables. |
    | Order by | Name |
    | Sort Type | a to z |
    | Max Results | 3 |
    [ ]

    {#create-custom-action-array-objects__table_cvp_l5p_lhb}  
    Note:  
    This example limits the Max Results setting to three records just for demonstration purposes.
12. From the Action Outline, select Add a new step.  
    The system displays a list of available steps.
13. Select Script.
14. From the Input Variables section, select Create Variable.
15. Configure the input variable with these values.  
    {#create-custom-action-array-objects__table_uby_5zp_lhb__entry__2}

    | Field | Value |
    |-|-|
    | Name | userRecords |
    | Value | \[step-\>Look Up Records step-\>User Records\] Note: Select the User records data pill from the Look Up Records step. |
    [ ]

    {#create-custom-action-array-objects__table_uby_5zp_lhb}  
    Note:  
    You can select the User records data pill from the data panel or from the Data Pill Picker button.
16. For Script, enter the following text.  

        (function execute(inputs, outputs) {
          //Create an empty array
          var contactsArray = [];
          var i = 0;
          //Iterate through the list of User records
          while(inputs.userRecords.next()) {
            //Create an empty object for each iteration
            var contactObject = {};
            //Query User records to assign object values
            contactObject.first_name = inputs.userRecords.getValue('first_name');
            contactObject.last_name = inputs.userRecords.getValue('last_name');
            contactObject.email_address = inputs.userRecords.getValue('email');
            //Add current object to array
            contactsArray[i] = contactObject;
            i += 1;
          }
          outputs.contacts = contactsArray;
        })(inputs, outputs);

17. From Output Variables, select Create Variable.
18. Configure the output variable with these values.  
    {#create-custom-action-array-objects__table_e3w_hbq_lhb__entry__4}

    | Label | Name | Type | Mandatory |
    |-|-|-|-|
    | contacts | contacts | Array.Object | True |
    [ ]

    {#create-custom-action-array-objects__table_e3w_hbq_lhb}
19. Expand the contacts Array.Object, and rename the child object to <kbd class="ph userinput">contact</kbd>.
20. From the row for the contact object, select the Add Child Item icon ![]().
21. Configure the child item with these values.  
    {#create-custom-action-array-objects__table_d3v_sbq_lhb__entry__4}

    | Label | Name | Type | Mandatory |
    |-|-|-|-|
    | first name | first_name | String | True |
    [ ]

    {#create-custom-action-array-objects__table_d3v_sbq_lhb}
22. From the row for the contact object, select the Add Child Item icon ![]().
23. Configure the child item with these values.  
    {#create-custom-action-array-objects__table_ncv_5bq_lhb__entry__4}

    | Label | Name | Type | Mandatory |
    |-|-|-|-|
    | last name | last_name | String | True |
    [ ]

    {#create-custom-action-array-objects__table_ncv_5bq_lhb}
24. From the row for the contact object, select the Add Child Item icon ![]().
25. Configure the child item with these values.  
    {#create-custom-action-array-objects__table_kht_vbq_lhb__entry__4}

    | Label | Name | Type | Mandatory |
    |-|-|-|-|
    | email address | email_address | String | True |
    [ ]

    {#create-custom-action-array-objects__table_kht_vbq_lhb}
26. From the row for the contact Object, select Toggle Advanced Inputs.
27. From the Advanced Options, select Save As Template.  
    The system displays the Save As Template dialog.
28. For Enter a Name, enter <kbd class="ph userinput">contact</kbd>.  
29. Click Save.
30. From the Action Outline, select OutputsCreate Output.
31. Configure the Action Output with these values.  
    {#create-custom-action-array-objects__table_ojs_pcq_lhb__entry__4}

    | Label | Name | Type | Mandatory |
    |-|-|-|-|
    | contacts | contacts | Array.Object | True |
    [ ]

    {#create-custom-action-array-objects__table_ojs_pcq_lhb}
32. Expand the contacts Array.Object.
33. From the row for the contact Object, select Toggle Advanced Inputs.
34. From the Advanced Options, select StructureStart from Template.  
    The system displays Template.
35. For Template, select the template you previously saved.  
    For example, select Global: contact.
36. Select Exit Edit Mode.  
    The System displays the output fields you created.
37. For contacts, select \[step-\>Script step-\>contacts\].  
    Note:  
    You can select the Script step contacts data pill from the data panel or from the Data Pill Picker button.
38. Click Save.
39. Select Test.  
    The system displays the Test Action dialog.
40. Enter the following test value:  
    {#create-custom-action-array-objects__table_onv_z22_mhb__entry__2}

    | Input | Value |
    |-|-|
    | Department | Development |
    [ ]

    {#create-custom-action-array-objects__table_onv_z22_mhb}
41. Select Run Test.  
    The system runs the action with the test values provided.
42. Select Your test has finished running. View the Action execution details.  
    The system displays the action execution details.
43. Review the runtime value for the action Output data.  
    Although the execution details display the output data as a JSON formatted string, the actual output data type is an array of objects. If you need a string version of your output, you can convert the object into a string using the JSON class. For more information about converting a JSON object into a string, see [Scoped JSON - stringify(Object jsonObject)](https://www.servicenow.com/docs/access?context=JSONScopedAPI&version=australia&pubname=australia-api-reference&section=JSONScoped-stringify_O&ft:locale=en-US).  
    For this example, the contacts object contains an array of contact objects with first name, last name, and email information for three users in the Development department.

        {
            "contacts": 
                "contact": [
                    {
                        "email_address": "allyson.gillispie@example.com",
                        "first_name": "Allyson",
                        "last_name": "Gillispie"
                    },
                    {
                        "email_address": "alva.pennigton@example.com",
                        "first_name": "Alva",
                        "last_name": "Pennigton"
                    },
                    {
                        "email_address": "andrew.och@example.com",
                        "first_name": "Andrew",
                        "last_name": "Och"
                    }
                ]
            }
        }

## Result

You have a custom action that looks up the Users for a given department and converts those users into an array of contact objects.

## What to do next

Customize this action to use your own logic.

*[\>]: and then


