Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Insert excerpt
_Banners
_Banners
nameactionflow
nopaneltrue

What are Loop Actions?

Insert excerpt
_action_loop
_action_loop
nameloop
nopaneltrue
 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
nopaneltrue
  and an 
Insert excerpt
_action_loop_end
_action_loop_end
nopaneltrue
,joined by a loop connector which returns the result of the End node back 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  node. The Start and End Loop each have their own properties.

On the 

Insert excerpt
_action_loop_end
_action_loop_end
nopaneltrue
, 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.

Image Added

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 will be are shown with the icon of where they have come from, for example a calculated action., a

Insert excerpt
_action_calculate
_action_calculate
nopaneltrue
 action.

Image Added

Tip

Loops wait until all their internal nodes have completed their tasks before beginning the next iteration of a the loop.

Creating Loop Actions

hiddentrue
  1. Click and drag the 
    Insert excerpt
    _action_loop
    _action_loop
    nameloop
    nopaneltrue
     icon from the toolbar onto the canvas
  2. In the Create Loop Action window:
    1. Enter a Name
    2. Select Create Action
  3. Map attributes from other actions to the 
  4. On the Start Loop action, 
  5. On the End Loop action
    HTML Comment
    Insert excerpt
    _action_loop_start
    _action_loop_start
    nopaneltrue
    to be processed by the loop
  6. On the 
    Insert excerpt
    _action_loop_end
    _action_loop_end
    nopaneltrue
    , set:
    1. Records to output: Choose whether to output all records from the loop or just the final record
    2. Until Expression: An expression that is checked on each cycle of the loop, and once the expression returns true, the loop will end
      1. Example: in.myValue == 10
    3. Max Loops: This is a safety net designed to stop infinite loops. After the number of loops set here, the loops will stop
      1.  Set a sensible number of max loops
      2. 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:  

  • 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 templateCompany 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 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 templateCompany Orders screen containing two grids of the Orders and OrderLines data - 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 ManagementBank Holiday API - Loops screen.

Bulk Update Employees and Orders for Company Acquisition

Loop Through Selected Years and Return Bank Holidays

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 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 public API. This is an example of paging where you retrieve a set amount of data per request, this is particular useful for large datasets.

Image Added

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. If you have completed the worked example on 

See 3.01 11 Setting up an API End Point, the setup is very similar.

Expand
titleHow?
Create a new Insert excerpt_actionflow_actionflownopaneltrue from the Actionflow home page
  1. Image Removed 
  2. Name: Bank Holiday API
In the toolbar, click  Insert excerpt_property_settings_property_settingsnopaneltrue
  • In the API section of the properties, enable API End-Point
  • This allows the Actionflow to receive an incoming API
  • Enable Allow Anonymous Connections
  • Insert excerpt_save_savenopaneltrue your changes

    Setup the Input Connection Point Input Parameters

    Click on the Insert excerpt_input_inputnopaneltrue Connection Point
  • In the Properties that open, in the Input Parameters section, add the following attributes (the names are case sensitive):
  • NameTypeDescriptionbodyStringprovides the information sent in the body of the API call.contentTypeStringProvides details of the Content TypeheadersStringProvides the information sent in the API call header.urlStringProvides the complete URL that was used to call the API. This can be used to pass parameters to the API.  

    JSON Setup

    Create a  Insert excerpt_action_json_action_jsonnopaneltrue action to receive the parameter
    1. Name: ReceiveYear
    2. Input Expression: in.body
    3. Path: $
    4. Create an Output Attribute on the JSON action
      1. Name: YearReceived
      2. Type: String
      3. Expression: _result.Year
    Connect the  Insert excerpt_input_inputnopaneltrue to the   Insert excerpt_action_json_action_jsonnopaneltrue action 
  • Map across the body attribute from the input
  • View Setup

    Create a Insert excerpt_action_view_action_viewnopaneltrue action for the UK_Bank_Holidays table
  • Configure the Output Attributes that the API should return
    1. UID
    2. Name
    3. Date
    4. Country
  • Connect the JSON action to the View action and map across the YearReceived attribute
  • On the  Insert excerpt_action_view_action_viewnopaneltrue action, add a filter:
    1. Name: SelectedYearOnly
    2. Filter Details: Image Removed
    Create an output connector between the  Insert excerpt_action_view_action_viewnopaneltrue action and the  Insert excerpt_output_outputnopaneltrue node
  • Map across all attributes coming from the View to the output

  • Obtain the API URL

    Open the Actionflow Insert excerpt_property_settings_property_settingsnopaneltrue and in the API section, copy the URL and save it locally to be used later
    Expand
    titleCheckpoint

    Image Removed

    Actionflow Setup

    This Actionflow will use API Endpoint for more details on how this API has been setup or to set one up yourself.

    This is a copy of the JSON body response, which will help with understanding the syntax used for the Output Attributes on the JSON action, when calling the API.

    1. Search for the Bank Holiday API in the API Endpoint section of the
      Insert excerpt
      _repository
      _repository
      nopaneltrue
       for your application
      1. Image Added
    2. Right-click the Bank Holiday API and select Edit
    3. Copy the URL and save it locally to be used later

    This is a copy of the JSON body response, which will help with understanding the syntax used for the Output Attributes on the JSON action, when calling the API.

    Code Block
    titleJSON API Body Response
    {
      "getBH" : [ {
        "UID" : "20190101e",
        "Name" : "New Year?s Day",
        "Date" : "2019-01-01T00:00:00.000Z",
        "Country" : "england-and-wales"
      }, {
        "UID" : "20190419e",
        "Name" : "Good Friday",
        "Date" : "2019-04-19T00:00:00.000Z",
        "Country" : "england-and-wales"
      }, {
        "UID" : "20190422e",
        "Name" : "Easter Monday",
        "Date" : "2019-04-22T00:00:00.000Z",
        "Country" : "england-and-wales"
      }, {
        "UID" : "20190506e",
        "Name" : "Early May bank holiday",
        "Date" : "2019-05-06T00:00:00.000Z",
        "Country" : "england-and-wales"
      }, {
        "UID" : "20190527e",
        "Name" : "Spring bank holiday",
        "Date" : "2019-05-27T00:00:00.000Z",
        "Country" : "england-and-wales"
      }, {
        "UID" : "20190826e",
        "Name" : "Summer bank holiday",
        "Date" : "2019-08-26T00:00:00.000Z",
        "Country" : "england-and-wales"
      },

    Actionflow Setup

    Create an Actionflow using a

    Insert excerpt
    _action_loop
    _action_loop
    nameaction
    nopaneltrue
     to poll the API setup above to and retrieve the bank holidays for the years selected on the Company Bank Holidays API - Loops screen.

    1. Open the Company Bank Holidays API - Loops screen
    2. Add an Actionflow to the Run button
      1. Name: Get Bank Holidays
    3. Choose the input of the CompanyBankHolidayAPI-Loops form
    4. Map in the two input fields:
      1. YearFrom
      2. YearTo
    5. 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
      1. Change their names to: 
        1. YearFromIn
        2. 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.

    1. Create a 
      Insert excerpt
      _action_calculate
      _action_calculate
      nopaneltrue
       action
      1. Name: ConvertToInteger
    2. Map both attribvutes attributes from the input to the Calculate action
    3. Use the function toNumber to create two Output Attributes on the Calculate action that convert the strings to integers

      Expand
      titleHow?

    Create

    Loop Action

    1. Create a 
      Insert excerpt
      _action_loop
      _action_loop
      nameloop
      nopaneltrue
       action
      1. Name: Loop Years
    2. Connect the 
      Insert excerpt
      _action_calculate
      _action_calculate
      nopaneltrue
       action to the 
      Insert excerpt
      _action_loop_start
      _action_loop_start
      nopaneltrue
    3. Map across the YearFrom attribute
    Create

    HTTP Action

    1. Create a 
      Insert excerpt
      _action_http
      _action_http
      nopaneltrue
       to call the Bank Holiday API
      1. Name: Call BH API
    2. Connect the Start Loop action to the HTTP action
    3. Map across the YearFrom attribute from the 
      Insert excerpt
      _action_loop_start
      _action_loop_start
      nopaneltrue
       to the HTTP action
      1. Rename
      the
      1. this attribute on the HTTP action to CallingYear
    4. In the HTTP Action properties, enter the following:
      1. HTTP Method: POST
      2. URL: Paste in the URL you copied earlier and saved locallyearlier 
      3. Body: 

        Code Block
        themeEmacs
        [{
            "CallingYear": "${in.CallingYear}"
        }]


      4. Toggle on Log Traffic to log the response from the API in the 

        Insert excerpt
        _console
        _console
        nopaneltrue

        Expand
        titleCheckpoint


    JSON Action

    Create

    JSON Action

    a JSON action to read the data coming in from the API.

    1. Create a 
      Insert excerpt
      _action_json
      _action_json
      nopaneltrue
       to process the returned bank holiday data from the API
      1. Name: Returned Bank Holidays
    2. Connect the HTTP action to the JSON action using the out connector
    3. Map across the body from the HTTP action
    4. Setup the JSON action to receive the data from the API

      Expand
      titleHow?

      Image Removed

    Save Action

    1. Create a  Insert excerpt_action_save_action_savenopaneltrue action to save the data
      1. Name: SaveBHs
      2. Type: Auto Insert/Update
      3. Table: click the  Insert excerpt_tables_tablesnopaneltrue icon Under Tables in the  Insert excerpt_repository_repositorynopaneltrue, click Input Expression: in.body
      4. Path: $.getBH
      5. Output Attributes:

        1. Code Block
          themeEmacs
          titleName: Name, Type: String
          _result.Name



        2. Code Block
          themeEmacs
          titleName: UID, Type: String
          _result.UID



        3. Code Block
          themeEmacs
          titleName: Country, Type: String
          _result.Country



        4. Code Block
          themeEmacs
          titleName: Date, Type: Date
          substring(_result.Date,1,10)


    Image Added

    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.

    1. Create a 
      Insert excerpt
      _
      add
      action_
      icon
      save
      _
      add
      action_
      icon
      save
      nopaneltrue
       to create a new tableName:  action to save the data
      1. Name: SaveBHs
      2. Type: Auto Insert/Update
      3. Table: choose the Bank_Holiday_Results
      4. Add the attributes from the UK_Bank_Holidays table to this table
      5. Image Removed
      6. Select this table on the Create Save Action drop down table
        1. This table has been prepopulated with the attributes required to hold the data
      7. Click Create Action
    2. Connect the JSON action to the Save action and map across the attributes being returned by the JSON action 
    Expand
    titleCheckpoint


    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 hits the Save actionsaved, 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.

    1. Create a 
      Insert excerpt
      _action_calculate
      _action_calculate
      nopaneltrue
       action
      1. Name: Increment +1
    2. Connect it to the Save action and map across the YearFrom attribute
      1. Ensure this is the attribute from the  
        Insert excerpt
        _action_loop_start
        _action_loop_start
        nopaneltrue
      2.  
    3. On the Calculate action, add an Output Attribute:
      1. Name: LoopYear
      2. Type: Integer
      3. 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.

    1. Connect the Increment +1 
      Insert excerpt
      _action_calculate
      _action_calculate
      nopaneltrue
       action to the 
      Insert excerpt
      _action_loop_end
      _action_loop_end
      nopaneltrue
    2. Map across the LoopYear attribute from the Increment +1 
      Insert excerpt
      _action_calculate
      _action_calculate
      nopaneltrue
       action and the YearTo attribute from the ConvertToInteger  ConvertToInteger 
      Insert excerpt
      _action_calculate
      _action_calculate
      nopaneltrue
       action
    3. Setup the 
      Insert excerpt
      _action_loop_end
      _action_loop_end
      nopaneltrue
       as follows:
      1. Records to output: All records
        1. This will output all the bank holidays, not just those satisfying the final year in the date range
      2. Until Expression: in.LoopYear == in.YearTo+1
      3. Max Loops: 10
        1. A loop will be completed per year in the date range
    4. Image Modified
    5. On the Loop connector between the 
      Insert excerpt
      _action_loop_end
      _action_loop_end
      nopaneltrue
       and 
      Insert excerpt
      _action_loop_start
      _action_loop_start
      nopaneltrue
       map the LoopYear attribute to the YearFrom  from the
      Insert excerpt
      _action_calculate
      _action_calculate
      nopaneltrue
       action to the YearFrom  attribute 


    Expand
    titleCheckpoint

    Testing

    1. Close all screens and reopen the Company Bank Holidays API - Loops screen
    3.05 Custom Confirmation Message Configuration
    1. Select the Year From and Year To criteria and press Run


    Expand
    titleView the API Response in the System Console

    Insert excerpt
    3.11 Setting up an API Endpoint
    3.11 Setting up an API Endpoint
    nameLogTraffic
    nopaneltrue



    Image Added