What are Loop Actions?
Insert excerpt |
---|
| _action_loop |
---|
| _action_loop |
---|
name | loop |
---|
nopanel | true |
---|
|
actions repeat an action or set of actions multiple times for a recordset or array.
Loop Types
In PhixFlow, there are three types of supported looping:
Polling
Repeats the same operation multiple times with the same parameters, until a desired result is obtained.
- Generates a single output record
- Output results are only generated at the end of the iterations
- Example: polling a HTTP API until the results are available
Accumulation
Repeats the same operation multiple times, where each iteration accumulates the progress results and triggers the next iteration with the updated result.
- Generates a single output record
- Example: calling a database (DB) to get a parent record, and then repeating the call with the parent until the root has been reached
Paging
Repeats an operation multiple times with different parameters.
- Generates the next parameters with a set of output records for each iteration of the loop
- Example: reading from a paging API, where each iteration provides the link for the next iteration and a set of output records
Start Loop and End Loop
Loops consist of a
Insert excerpt |
---|
| _action_loop_start |
---|
| _action_loop_start |
---|
nopanel | true |
---|
|
and an
Insert excerpt |
---|
| _action_loop_end |
---|
| _action_loop_end |
---|
nopanel | true |
---|
|
,
joined by a loop connector which returns the result of the
End node back to the
Start node. The Start and End Loop each have their own properties.
On the
Insert excerpt |
---|
| _action_loop_end |
---|
| _action_loop_end |
---|
nopanel | true |
---|
|
, the
Records to output can be set, either
outputting All Records from the loop or just the Final Record. The icon on the end loop node shows which option is set.
Loop Mappings
Attributes passed into a loop are available as passthrough, as well as attributes mapped in from the End Loop. This allows the original passthrough values to be referenced and if the loop is manipulating the incoming attributes, for example, incrementing a number, this is also available.
The mapped attributes that are manipulated by the loop are shown with a loop icon on the Mappings window, and passthrough attributes are shown with the icon of where they have come from, for example, a
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action.
Tip |
---|
Loops wait until all their internal nodes have completed their tasks before beginning the next iteration of the loop. |
Creating Loop Actions
- Click and drag the
Insert excerpt |
---|
| _action_loop |
---|
| _action_loop |
---|
name | loop |
---|
nopanel | true |
---|
|
icon from the toolbar onto the canvas - In the Create Loop Action window:
- Enter a Name
- Select Create Action
- Map attributes from other actions to the
Insert excerpt |
---|
| _action_loop_start |
---|
| _action_loop_start |
---|
nopanel | true |
---|
|
to be processed by the loop - On the
Insert excerpt |
---|
| _action_loop_end |
---|
| _action_loop_end |
---|
nopanel | true |
---|
|
, set:- Records to output: Choose whether to output all records from the loop or just the final record
- Until Expression: An expression that is checked on each cycle of the loop, and once the expression returns true, the loop will end
- Example:
in.myValue == 10
- Max Loops: This is a safety net designed to stop infinite loops. After the number of loops set here, the loops will stop
- This Set a sensible number of max loops
- This default to 10000
Worked Example
Here's a worked example using the Company Data (available from the Learning Centre).
In this example, we are using:
- A Company Bank Holiday API - Loops screen containing a grid backed by an empty Bank_Holiday_Results table, drop down fields for the user to select the Year From and Year To values and a Run button - this screen was created using the Tile no Buttons 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 Bank Holiday API - Loops screen. |
Loop Through Selected Years and Return Bank Holidays
In this example, a user selects a date range (years) to return UK bank holidays. We'll create an Actionflow that takes in these parameters and each cycle of the loop returns the bank holiday dates for that year. For the purposes of this example, our Actionflow will call an API that exists within PhixFlow but this API represents an external API. This is an example of paging where you retrieve a set amount of data per request, this is particular useful for large datasets.
Obtain the API URL
We have provided an Actionflow that acts as a UK Bank Holiday API. This can be called by a Actionflow, in the same way that an external API can be called. The UK Bank Holiday API has been configured to receive a year parameter and use that parameter to pass out the bank holiday dates for the corresponding year.
See 3.11 Setting up an API Endpoint for more details on how this API has been setup or to set one up yourself.
- Search for the Bank Holiday API in the API Endpoint section of the
Insert excerpt |
---|
| _repository |
---|
| _repository |
---|
nopanel | true |
---|
|
for your application
- Right-click the Bank Holiday API and select Edit
- Copy the URL and save it locally to be used later
Actionflow Setup
Create an Actionflow using a
Insert excerpt |
---|
| _action_loop |
---|
| _action_loop |
---|
name | action |
---|
nopanel | true |
---|
|
to poll the API and retrieve the bank holidays for the years selected on the
Company Bank Holidays API - Loops screen.
- Open the Company Bank Holidays API - Loops screen
- Add an Actionflow to the Run button
- Name:
Get Bank Holidays
- Choose the input of the CompanyBankHolidayAPI-Loops form
- Map in the two input fields:
- YearFrom
- YearTo
- On the Mappings window, change the names of these two attributes on the input to allow them to be easily distinguished from two attributes with similar names we will create in the next section
- Change their names to:
- YearFromIn
- YearToIn
Type Conversion
The data coming into the Actionflow from the screen is in the Type: String. Our Actionflow needs this data in Integer format. This can be changed on the field on the screen but can also be modified in the Actionflow, which we will do here.
- Create a
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action- Name:
ConvertToInteger
- Map both attributes from the input to the Calculate action
Use the function toNumber to create two Output Attributes on the Calculate action that convert the strings to integers
Expand |
---|
|
|
Create Loop Action
- Create a
Insert excerpt |
---|
| _action_loop |
---|
| _action_loop |
---|
name | loop |
---|
nopanel | true |
---|
|
action- Name:
Loop Years
- Connect the
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action to the Insert excerpt |
---|
| _action_loop_start |
---|
| _action_loop_start |
---|
nopanel | true |
---|
|
- Map across the YearFrom attribute
Create HTTP Action
- Create a
Insert excerpt |
---|
| _action_http |
---|
| _action_http |
---|
nopanel | true |
---|
|
to call the Bank Holiday API- Name:
Call BH API
- Connect the Start Loop action to the HTTP action
- Map across the YearFrom attribute from the
Insert excerpt |
---|
| _action_loop_start |
---|
| _action_loop_start |
---|
nopanel | true |
---|
|
to the HTTP action- Rename this attribute on the HTTP action to CallingYear
- In the HTTP Action properties, enter the following:
- HTTP Method:
POST
- URL: Paste in the URL you copied earlier
Body:
Code Block |
---|
|
[{
"CallingYear": "${in.CallingYear}"
}] |
Toggle on Log Traffic to log the response from the API in the
Insert excerpt |
---|
| _console |
---|
| _console |
---|
nopanel | true |
---|
|
Expand |
---|
|
|
Create JSON Action
Create a JSON action to read the data coming in from the API.
- Create a
Insert excerpt |
---|
| _action_json |
---|
| _action_json |
---|
nopanel | true |
---|
|
to process the returned bank holiday data from the API- Name:
Returned Bank Holidays
- Connect the HTTP action to the JSON action using the out connector
- Map across the body from the HTTP action
Setup the JSON action to receive the data from the API
- Input Expression:
in.body
- Path:
$..ouput
- Output Attributes:
Code Block |
---|
theme | Emacs |
---|
title | Name: Name, Type: String |
---|
|
_result.Name |
Code Block |
---|
theme | Emacs |
---|
title | Name: UID, Type: String |
---|
|
_result.UID |
Code Block |
---|
theme | Emacs |
---|
title | Name: Country, Type: String |
---|
|
_result.Country |
Code Block |
---|
theme | Emacs |
---|
title | Name: Date, Type: Date |
---|
|
_result.Date |
Save Action
Create a save action to save the bank holiday data to the Bank_Holiday_Results table where it can be viewed on the screen.
- Create a
Insert excerpt |
---|
| _action_save |
---|
| _action_save |
---|
nopanel | true |
---|
|
action to save the data- Name:
SaveBHs
- Type:
Auto Insert/Update
- Table: choose the Bank_Holiday_Results table
- This table has been prepopulated with the attributes required to hold the data
- Click Create Action
- Connect the JSON action to the Save action and map across the attributes being returned by the JSON action
Expand |
---|
|
|
Increment Year By One
After the bank holiday data has been saved, we need the loop to go round again, this time getting the next year in the sequence, for example, if the user has entered 2018 for the YearFrom and 2020 for the YearTo, when the data is first saved, it is just the 2018 data. For the second loop, the 2019 data will be obtained, and so on. The criteria to end the loop is covered in the next section.
- Create a
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action- Name:
Increment +1
- Connect it to the Save action and map across the YearFrom attribute
- Ensure this is the attribute from the
Insert excerpt |
---|
| _action_loop_start |
---|
| _action_loop_start |
---|
nopanel | true |
---|
|
-
- On the Calculate action, add an Output Attribute:
- Name:
LoopYear
- Type:
Integer
- Expression:
in.YearFrom + 1
End Loop Setup
As the loop completes each iteration, criteria can be set on the End Loop to specify when the loop stops. This is specified using the Until Expression or Max Loops (whichever comes first). Our loop obtains one year's bank holidays for each loop, so if 2018 is entered for YearFrom and 2020 for YearTo, three loops are completed.
We want the looping to stop once the YearTo has passed, for this reason we must specify that the loop ends when the year after the YearTo has been reached.
- Connect the Increment +1
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action to the Insert excerpt |
---|
| _action_loop_end |
---|
| _action_loop_end |
---|
nopanel | true |
---|
|
- Map across the LoopYear attribute from the Increment +1
Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action and the YearTo attribute from the ConvertToInteger Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action
- Setup the
Insert excerpt |
---|
| _action_loop_end |
---|
| _action_loop_end |
---|
nopanel | true |
---|
|
as follows:- Records to output:
All records
- This will output all the bank holidays, not just those satisfying the final year in the date range
- Until Expression:
in.LoopYear == in.YearTo+1
- Max Loops:
10
- A loop will be completed per year in the date range
- On the Loop connector between the
Insert excerpt |
---|
| _action_loop_end |
---|
| _action_loop_end |
---|
nopanel | true |
---|
|
and Insert excerpt |
---|
| _action_loop_start |
---|
| _action_loop_start |
---|
nopanel | true |
---|
|
map the LoopYear attribute from the Insert excerpt |
---|
| _action_calculate |
---|
| _action_calculate |
---|
nopanel | true |
---|
|
action to the YearFrom attribute
Expand |
---|
|
|
Testing
- Close all screens and reopen the Company Bank Holidays API - Loops screen
- Select the Year From and Year To criteria and press Run
Expand |
---|
title | View the API Response in the System Console |
---|
|
Insert excerpt |
---|
| 3.11 Setting up an API Endpoint |
---|
| 3.11 Setting up an API Endpoint |
---|
name | LogTraffic |
---|
nopanel | true |
---|
|
|
3.09 Advanced Action Node Settings