Create a custom action to generate an array of strings from a list of records
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.
始める前に
このタスクについて
- 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.
手順
- オプション:
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 Users With Role Array Of Strings Application Global Accessible From All application scopes 注: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 Role Type Reference.Role [Reference.sys_user_role] 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 Role [sys_user_has_role] Conditions [Role][is][action->Role] 注:Select the Role data pill from the Input Variables.Order by Role Sort Type a to z Max Results 3 注: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 userRoleRecords Value [step->Look Up Records step->User Role records] 注:Select the User Role records data pill from the Look Up Records step.注:You can select the User Role 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 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); - From Output Variables, select Create Variable.
-
Configure the output variable with these values.
Label Name Type Mandatory users users Array.String True - Expand the users Array.String, and rename the child string to user.
-
Configure the child item with these values.
Label Name Type Mandatory user user String False - From the Action Outline, select Outputs > Create Output.
-
Configure the Action Output with these values.
Label Name Type Mandatory users users Array.String True - Expand the users Array.String, and rename the child string to user.
-
Configure the child item with these values.
Label Name Type Mandatory user user String False -
Select Exit Edit Mode.
The System displays the output fields you created.
-
For users, select [step->Script step->users].
注:You can select the Script step users 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 Role admin -
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.
The system displays output data in JSON format.For this example, the users array contains three users who have the admin role.{ "users": [ "System Administrator", "Rob Phillips", "Fred Luddy" ] }