Working With Lists in a Microflow

8 minutes to read Download PDF Edit

In this how-to, you will learn how to work with a list of objects in a microflow. To manage this list you will first retrieve a filtered list of objects from the database. Mendix utilizes XPath constraints to apply filters. To learn more about XPath, see XPath Contains in the Mendix Reference Guide. Secondly, you will iterate over the retrieved list of objects and calculate the total price of all the orders in a Loop. You will end this how-to with an alternative to calculating aggregated values over a list of objects.

1. Preparing the Data Structure, GUI and Example Data

To see the results of this how-to it is necessary that you setup a test project with test data.

Before you continue, make sure that you know how to create:

  • Domain models, if you need more info, take a look at this how-to.
  • Overview and detail pages, if you need more info, take a look at this how-to.
  • Menu items, if you need more info, take a look at this how-to.
  1. Create the following domain model:
  2. Create overview and detail pages to manage objects of type Customer and Order.
  3. Create menu items to access the Order and the Customer overview pages.
  4. Add the following customer data to your app:
  5. Add the following order data to your app:

2. Retrieving a Filtered List of Objects from the Database

In the previous section you have set up a basic data structure and created some sample data. In this section you will retrieve all the ‘Processing’ orders. To achieve this you will add a microflow button to the ‘Orders’ overview. In this microflow you will add a ‘Retrieve from database’ ‘Action activity’ with an XPath constraint. The XPath constraint will filter the retrieved list to only the ‘Invoiced’ orders.

  1. Add a new microflow to your module.
  2. Name the Microflow _IVKSetOrderToComplete.
  3. Save the new menu item by clicking OK.

    You should see an empty Microflow like this:

  4. Add a Microflow button to the toolbar of the orders overview and change its caption to Set Processing to Complete.

  5. Right click the Microflow button and click Select Microflow from the context menu.

  6. Select the IVK_SetOrderToComplete Microflow and click select.

  7. Open the **IVK_SetOrderToComplete **Microflow created in the first steps by double clicking at it in the Project Explorer. You should see the empty Microflow again:

  8. Open the Toolbox. It should be on the bottom right of the Desktop Modeler.

  9. Drag a Retrieve action from the toolbox to the line between the green start and red end event. This inserts a retrieve action activity.

  10. Double click the retrieve activity to open its properties.

  11. Select From database as source option.

  12. Set the following properties:
    a. For _Entity select Order
    b. For Name enter OrderList

  13. Add the following XPath expression in the XPath constraint field: [OrderStatus = ‘Processing’]. This expression will filter the list to only orders with the status Processing.

  14. Enter a descriptive name for the list variable. Your properties screen should look like this:

    You should see a Microflow like this:

3. Iterate Over a List of Objects

In the previous section you retrieved a list of orders with the status ‘Processing’. In this section you will iterate over this list and change the status of each object individually to ‘Complete’. To do so you will use a ‘Loop’ to iterate over the ‘OrderProcessingList’ and use the ‘Change object’ activity to change the status of the order object.

  1. Open the IVK_SetOrderToComplete microflow created in the previous section.
  2. Drag a Loop action from the toolbox to the line, behind the OrderProcessingList action activity.

  3. Double click the loop activity and select the **OrderProcessingList **to iterate over.

  4. Drag a Change object activity inside the loop:

  5. Double click the change activity to open its properties.

  6. Select the IteratorOrder at the **Variable **drop down and click the New button.

    You will see the following properties screen:

  7. Set the following properties:
    a. For Member select Orderstatus.
    b. For Value enter _MyFirstModule.OrderStatus.Complete.
    Click OK. Your properties screen should look like this:

  8. Click **OK. **Your microflow should look like this:

  9. Re-deploy your application.

  10. Click the Set Processing to Complete button. The orders with status ‘Processing’ will now be changed to ‘Complete’.

4. Calculating a Total List Value Using a Variable and a Loop

In the previous section you iterated over a filtered list of objects using a ‘Loop’. In this section you will use a loop to calculate the total sum of all your orders. To calculate this sum you will generate a variable, which will be changed for every iteration in the loop.

  1. Add an empty microflow to your order datagrid and name it _IVKCalculateTotalPriceOrders.
  2. Name the microflow button Calculate total order price. If you don’t know how to do this please look at step 1-6 of section 2.0 of this how-to.
  3. Add a Retrieve activity for Orders and a loop for this list to the microflow.
  4. Drag a Create variable before the Orderslist.
  5. Double click the variable to open its properties.
  6. Set the following properties:
    1. For Data typeselect Float/Currency
    2. For **Value **enter 0
    3. For Variable enter CalculatedTotalPrice
  7. Add a Change variable inside the loop.

  8. Double click the change variable activity to open its properties.

  9. Set the following properties:
    a. For **Variable **select CalculatedTotalPrice
    b. For **Value **enter _$CalculatedTotalPrice + $IteratorOrder/TotalPrice

    Your properties screen should look like this:

  10. Drag a Show Message action from the toolbox to the end of the microflow.

  11. Double click the message activity to open its properties.

  12. Set the following properties:

    1. For Template enter Total calculated price: {1}.
    2. For **Parameters **add _toString($CalculatedTotalPrice). _
  13. Click OK. You should see a Microflow like this:

  14. Re-deploy your application.

  15. Click the Calculate total order price button and you will see the price of all the orders added up.

5. Calculate a Total List Value Using an Aggregate Function

In the previous section you iterated over a list to add the value of single object to a total price variable. In this section you will use the ‘aggregate list’ function to calculate the total price instead of using a loop. The aggregate list can be used to calculate aggregated values such as the maximum, minimum, sum, average and total amount of objects over a list of objects.

  1. Open the IVK_CalculateTotalPriceOrders microflow and remove the loop and the CalculatedTotalPrice variable.
  2. Add an Aggregate list activity after the Orderlist.
  3. Double click the aggregate list activity to open its properties.
  4. Set the following properties:
    a. For **Variable **select OrderList.
    b. For **Function **select _Sum

    c. For Attribute select TotalPrice
    d. For Variable enter a descriptive name like SumTotalPrice.

  5. Click OK.

  6. Double click on the message activity.

  7. Replace the $CalculatedTotalPrice variable in the Parameters Expression by the **$SumTotalPrice **variable

  8. Click OK. Your microflow should look like this:

  9. Re-deploy your application.

  10. Click the Calculate total order price button and you will see the same price of all the orders added up.

6. Filter List of Orders on the City of the Associated Customers

In the previous sections you filtered the list of orders from database on attributes of the order entity itself. In this section you will constrain on attributes over the associated customer object. In the example of this section you will set the order status of all customers in Rotterdam to the status ‘Complete’.

  1. Open the microflow IVK_SetOrderToComplete.
  2. Open the OrderProcessingList activity.
  3. Add an XPath constraint over the association to customer, constraining on the city (Rotterdam) of this customer.
  4. Click OK and re-deploy your application.
  5. Open the application in the browser.
  6. Click the Set Processing to Complete button. All the orders from customers in Rotterdam are set to Complete.