Create a custom action to generate an array of objects from a list of records
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
About this task
- 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.
Procedure
- 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.For example, create an application called My Application.
- Navigate to All > Process Automation > Workflow Studio.
- On the homepage, select Actions.
-
Select New > Action
The system displays the Action Properties dialog.
-
Enter these sample values.
Field Value Name Create Contacts Array Of Objects 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. -
Select Build action.
The system displays the Workflow Studio interface.
-
From the Action Outline, select Inputs > Create Input
The system displays a new action input.
-
Configure the action input with these values.
Field Value Label Department Type Reference.Department [Reference.cmn_department] Mandatory True -
From the Action Outline, select Add a new step.
The system displays a list of available steps.
- Select Look Up Records
-
Configure the step with these values.
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 Note:This example limits the Max Results setting to three records just for demonstration purposes. -
From the Action Outline, select Add a new step.
The system displays a list of available steps.
- Select Script.
- From the Input Variables section, select Create Variable.
-
Configure the input variable with these values.
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.Note:You can select the User records data pill from the data panel or from the Data Pill Picker button. -
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); - From Output Variables, select Create Variable.
-
Configure the output variable with these values.
Label Name Type Mandatory contacts contacts Array.Object True - Expand the contacts Array.Object, and rename the child object to contact.
-
From the row for the contact object, select the Add Child Item icon
.
-
Configure the child item with these values.
Label Name Type Mandatory first name first_name String True -
From the row for the contact object, select the Add Child Item icon
.
-
Configure the child item with these values.
Label Name Type Mandatory last name last_name String True -
From the row for the contact object, select the Add Child Item icon
.
-
Configure the child item with these values.
Label Name Type Mandatory email address email_address String True - From the row for the contact Object, select Toggle Advanced Inputs.
-
From the Advanced Options, select Save As
Template.
The system displays the Save As Template dialog.
-
For Enter a Name, enter
contact.
- Click Save.
- From the Action Outline, select Outputs > Create Output.
-
Configure the Action Output with these values.
Label Name Type Mandatory contacts contacts Array.Object True - Expand the contacts Array.Object.
- From the row for the contact Object, select Toggle Advanced Inputs.
-
From the Advanced Options, select Structure > Start from Template.
The system displays Template.
-
For Template, select the template you previously
saved.
For example, select Global: contact.
-
Select Exit Edit Mode.
The System displays the output fields you created.
-
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.
- Click Save.
-
Select Test.
The system displays the Test Action dialog.
-
Enter the following test value:
Input Value Department Development -
Select Run Test.
The system runs the action with the test values provided.
-
Select Your test has finished running. View the Action execution
details.
The system displays the action execution details.
-
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).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" } ] } }