What are Loop Actions?
Loop actions repeat an action or set of actions multiple times.
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 Start Loop Action and an End Loop Action, joined by a loop connector which returns the result of the End node to the Start node. Each part of the Loop has its own set of properties described below.
The key concept of a loop is that the attributes passed into it 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, passthrough attributes will be shown with the icon of where they have come from, for example a calculated action.
Loops wait until all their internal nodes have completed their tasks before beginning the next iteration of a loop.
Creating Loop Actions
- Click and drag the Loop icon from the toolbar onto the canvas
- In the Create Loop Action window:
- Enter a Name
- Select Create Action
- On the Start Loop action,
- On the End Loop action
Worked Example
Here's a worked example using the Company Data (available from the Learning Centre).
In this example, we are using:
- A Company Management screen containing a grid of the Companies data and a card container of their associated Employees - this screen was created using the Multi-Tile template
- A Company Merger Pop up screen containing:
- two manual drop down fields of the company name's of the companies to be merged
- two string fields showing the company ID's of the companies to be merged
- a button to initiate the Actionflow - this screen was created using the Tile no Buttons template
- A Company Orders screen containing two grids of the Orders and OrderLines data - this screen was created using the Multi-Tile template
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 Management screen.
Bulk Update Employees and Orders for Company Acquisition
In this example, a company is being merged into another company and we need to update the employees and orders to reflect the change.
GIF
Create API End Point Actionflow
In this example, we'll set up an Actionflow that will act as an UK Bank Holiday API that can be called by another Actionflow. This Actionflow will be configured to receive a year parameter and use that parameter to pass out the bank holiday dates for the corresponding year. If you have completed the worked example on 3.01 Setting up an API End Point, the setup is very similar.
Actionflow Setup
This Actionflow will use a Loop Action to poll the API setup above to 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
- Name:
- 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 to:
- YearFromIn
- YearToIn
Type Conversion
The data coming into the Actionflow is in the Type, String. Our Actionflow needs this data in Integer format.
- Create a Calculate action
- Name: ConvertToInteger
- Map both attribvutes 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
Create Loop Action
- Create a Loop action
- Name: Loop Years
- Connect the Calculate action to the Start Loop Action
- Map across the YearFrom attribute
Create HTTP Action
- Create a HTTP to call the Bank Holiday API
- Name: Call BH API
- Map across the YearFrom attribute from the Start Loop Action to the HTTP action
- Rename the 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 and saved locally
Body:
[{ "CallingYear": "${in.CallingYear}" }]
Toggle on Log Traffic
Create JSON Action
- Create a JSON to process the returned bank holiday data from the API
- Name: Returned Bank Holidays
- Connect the HTTP action to the JSON action
- Map across the body from the HTTP action
Setup the JSON action to receive the data from the API
Save Action
- Create a Save action to save the data
- Name: SaveBHs
- Type: Auto Insert/Update
- Table: click the Table icon
- Under Tables in the Repository, click to create a new table
- Name: Bank_Holiday_Results
- Add the attributes from the UK_Bank_Holidays table to this table
- Select this table on the Create Save Action drop down
- Click Create Action
- Under Tables in the Repository, click to create a new table
- Connect the JSON action to the Save action and map across the attributes being returned by the JSON action
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 first hits the Save action, it is just the 2018 data. For the second loop, the 2019 data will be obtained.
- Create a Calculate action
- Name: Increment +1
- Connect it to the Save action and map across the YearFrom attribute
- Ensure this is the attribute from the Start Loop Action
- On the Calculate action, add an Output Attribute:
- Name: LoopYear
- Type: Integer
- Expression: in.YearFrom + 1
End Loop Setup
- Connect the Increment +1 Calculate action to the End Loop Action
- Map across the LoopYear attribute from the Increment +1 Calculate action and the YearTo attribute from the ConvertToInteger Calculate action
- Setup the End Loop Action as follows:
- On the Loop connector between the End Loop Action and Start Loop Action map the LoopYear attribute to the YearFrom attribute
Testing
- Close all screens and reopen the