What is a For Each Action?
The
Insert excerpt |
---|
| _action_foreach |
---|
| _action_foreach |
---|
nopanel | true |
---|
|
action iterates over a set of records or list of values, and generates an output record for each one. It is most commonly used to split data, such as an array, into individual records, or it can be used in conjunction with a
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action to firstly convert a user-inputted list into an array for the
Insert excerpt |
---|
| _action_for_each |
---|
| _action_for_each |
---|
nopanel | true |
---|
|
action to process - see worked example below.
Creating For Each Actions
- Click and drag the
Insert excerpt |
---|
| _action_foreach |
---|
| _action_foreach |
---|
nopanel | true |
---|
|
icon from the toolbar onto the canvas - Enter a name in the Create For Each Action window and select Create Action
- In the
Insert excerpt |
---|
| _action_foreach |
---|
| _action_foreach |
---|
nopanel | true |
---|
|
action Insert excerpt |
---|
| _property_settings |
---|
| _property_settings |
---|
nopanel | true |
---|
|
on the right, populate the following:
Field | Description | Example Value |
---|
Input Expression | The input expression provides the source of the data to be iterated over. Typically, this will be a simple expression pointing at an Input Attribute, using the syntax, Connector.AttributeName. If an empty record set or empty list is provided then no output will be generated. This field is mandatory (PhixFlow version 11.1 +). | // Receives a list of Invoice IDs, these are iterated over using in.InvoiceID
|
Input Attributes | Attributes mapped into the For Each action will appear here. If adding a new input attribute, a Name and Type are required. See Understanding Data Types. |
|
Output Attributes | Attributes mapped out of the For Each action will appear here. If adding a new output attribute, a Name, Type and Expression are required. This is a PhixScript expression. Values returned by the Input Expression are available in this expression as: To work in a larger popup window, click Insert excerpt |
---|
| _editor_expand |
---|
| _editor_expand |
---|
nopanel | true |
---|
| .See also: | Name: Invoices Type: String Expression: _result.value |
HTML Comment |
---|
|
Insert excerpt |
---|
| For Each Node |
---|
| For Each Node |
---|
name | page |
---|
nopanel | true |
---|
|
|
Worked Example
Here's a worked example using the Company Data (available from the Learning Centre).
In this example, we are using:
- A Company For Each Example screen containing:
- an multi-line string field and Save button that will be used by the user to enter a list
- a string field that will be used to create a filter
- a grid backed by the ForEachExample table - this screen was created using the Multi-Tile template
Tip |
---|
If you are completing this chapter as part of the Actionflow course and using a training instance, the data and screens have already been pre-loaded into the Actionflow Advanced Application. For this example, we'll be working on the Company For Each Example screen. |
Split a List Into Individual Records
In this example, we need a quick way to input a stock delivery into our application that can be processed later. We'll take a list of values inputted by a user and convert these into individual records.
Image RemovedImage Added
Actionflow Setup
- Open the Company For Each Example screen
- Add an
Insert excerpt |
---|
| _actionflow |
---|
| _actionflow |
---|
nopanel | true |
---|
|
to the Save button- Name:
SplitIntoRecords
- Configure the Input as the CompanyForEachExample form
- Map the attribute Text into the Actionflow
Calculate Action - Convert List to Array
Use the split function in a
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action to split the
Text attribute by the comma separator. This will convert the list entered by the user into an array.
Expand |
---|
|
- Create a
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
| action - Connect the to the Calculate action
- Map across the Text attribute
- In the Calculate action, create an Output Attribute:
- Name:
Split - Type:
Structured Data - Expression:
split(in.Text,",")
|
Expand |
---|
|
|
Calculate Action - Create Session ID (Optional)
Create a SessionID attribute in a
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action to allow identification of records created at the same time by the same user, which we will use for filtering.
Expand |
---|
|
- In the Calculate action, create another Output Attribute:
- Name:
SessionID - Type:
String - Expression:
now() + _user.username
Insert excerpt |
---|
| _finish |
---|
| _finish |
---|
nopanel | true |
---|
|
|
Expand |
---|
|
|
For Each Action - Convert Array to Records
Use a
Insert excerpt |
---|
| _action_for_each |
---|
| _action_for_each |
---|
nopanel | true |
---|
|
action to convert the array into individual records.
- Create a
Insert excerpt |
---|
| _action_for_each |
---|
| _action_for_each |
---|
nopanel | true |
---|
|
action - Name:
Convert to Records
- Connect it to the Calculate action and map across the Split attribute
- On the For Each action, enter the following:
- Input Expression:
in.Split
- Input Attributes:
Split
- Create two Output Attributes:
- Name:
Name
- Type:
String
- Expression:
trim(_result.value)
- The trim function is used here to remove the whitespace after the comma separator in the list
- If this was not used and the list "one, two, three" was entered, the records "two" and "three" would be saved with prefixed whitespace
- Name:
LastUpdated
- Type:
Datetime
- Expression:
now()
Insert excerpt |
---|
| _finish |
---|
| _finish |
---|
nopanel | true |
---|
|
Expand |
---|
|
|
Create a Sequence Number for UID (Optional)
Create a Sequence in your application to give each record a unique value. Alternatively, the UID attribute can be left unmapped and PhixFlow will automatically create a unique identifier.
- Open the
Insert excerpt |
---|
| _repository |
---|
| _repository |
---|
nopanel | true |
---|
|
for your application - Expand the
Insert excerpt |
---|
| _sequences |
---|
| _sequences |
---|
nopanel | true |
---|
|
option, then click Insert excerpt |
---|
| _add_icon |
---|
| _add_icon |
---|
nopanel | true |
---|
|
- Name:
Unique_ID
- Start Value:
1
- Block Size:
1
Insert excerpt |
---|
| _finish |
---|
| _finish |
---|
nopanel | true |
---|
|
Add the Sequence Number to the UID Attribute (Optional)
Create an Output Attribute, UID, on the
Insert excerpt |
---|
| _action_for_each |
---|
| _action_for_each |
---|
nopanel | true |
---|
|
action using the
Sequence you created above.
Expand |
---|
|
- On the For Each action, create an Output Attribute:
- Name:
UID - Type:
Integer - Expression:
nextValue('Unique_ID')
Insert excerpt |
---|
| _finish |
---|
| _finish |
---|
nopanel | true |
---|
|
|
Expand |
---|
|
|
Save Action
Use a
Insert excerpt |
---|
| _action_save |
---|
| _action_save |
---|
nopanel | true |
---|
|
to save the data to the ForEachExample table.
Expand |
---|
|
- Create a
Insert excerpt |
---|
| _action_save |
---|
| _action_save |
---|
nopanel | true |
---|
| action for the ForEachExample table - Connect it to the For Each action
- Map across all attributes from the For Each action
- Map across the SessionID attribute from the Calculate action, if created
- In the Save action
Insert excerpt |
---|
| _property_settings |
---|
| _property_settings |
---|
nopanel | true |
---|
| , change the Type to Insert
|
Output - Map SessionID Onto Screen For Filtering (Optional)
If you have created a SessionID attribute, an output will need to be created to map this value onto the SessionID String Field on the screen, so that it can be used for filtering.
Expand |
---|
|
- Under Connections, create an output connection point to the CompanyForEachExample form
- Hover over the Save action and click, out, then select the
Insert excerpt |
---|
| _output |
---|
| _output |
---|
nopanel | true |
---|
| node - Map across the SessionID attribute from the table (not the Calculate action) to the output
- Click on the output connector under Connections and map the SessionID attribute from the output to the screen
|
Expand |
---|
|
|
Filter Setup (Optional)
- On the Company For Each Example screen, open the view configuration for the grid
Create a Default Filter that filters the view by the values in the SessionID field
Expand |
---|
|
|
Testing
- On the Company For Each Example screen, the screen
- Enter a list into the Text field, e.g. Extra Small, Small, Medium, Large, etc. and click Save
- Your list will be converted into individual records in the grid
- Enter another list and you will see that the previous list disappears as the grid is being filtered by the SessionID
- Once you have confirmed your default filter is working, the SessionID area and its header can be hidden