If you would like to upgrade to a newer long-term support version of Studio Pro, see Moving from Mendix Studio Pro 8 to 9.
Work with Lists in a Microflow
Introduction
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 Studio Pro 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.
Preparing the Data Structure, GUI and Example Data
To see the results of this how-to it is necessary that you set up a test project with test data.
Before you continue, make sure that you know how to create the following:
- Domain models – if you need more information, see How to Create a Basic Data Layer
- Overview and detail pages – if you need more information, see How to Create Your First Two Overview and Detail Pages
- Menu items – if you need more information, see How to Set Up the Navigation Structure
-
Create the following domain model:
-
Create overview and detail pages to manage objects of type Customer and Order.
-
Create menu items to access the Order and the Customer overview pages.
-
Add the following customer data to your app:
-
Add the following order data to your app:
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.
-
Create a new microflow by right-clicking the module and selecting Add > Microflow.
-
Name the Microflow IVK_SetOrderToComplete.
-
Save the new menu item by clicking OK. You should see an empty Microflow like this:
-
Add an Action button to the toolbar of the orders overview:
-
Double-click the action button and in its properties, enter Set Processing to Complete for the Caption.
-
For On click, select Call a microflow, and then select the IVK_SetOrderToComplete microflow.
-
Open the IVK_SetOrderToComplete microflow by right-clicking the new button and selecting Go to microflow.
-
Open the Toolbox. It should be on the bottom right of .
-
Drag a Retrieve action from the toolbox to the line between the green start and red end event. This inserts a retrieve action activity.
-
Double-click the retrieve activity to open its properties.
-
Select From database for the Source option.
-
Set the following properties:
- For Entity, select Order_
- For List, enter OrderList
With the currents settings your retrieve action gets every order in the database, using the XPath expression in the following steps you will filter the results that come back from the database. - For Entity, select Order_
-
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. -
Your properties screen should look like this:
With the currents settings your retrieve action gets all the ‘Processing’ orders in the database. In the next section you will edit this list of orders.
You should see a microflow like this:
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.
-
Open the IVK_SetOrderToComplete microflow created in the previous section.
-
Drag a Loop action from the Toolbox to the line behind the OrderProcessingList action activity.
A loop is used to iterate over a list of objects. For each object the flow inside the loop is executed. For each object the flow inside the loop is executed. The flow starts at the element that has no incoming sequence flows. A loop can contain all elements used in microflows, with the exception of start and stop events. Additionally, a loop (and only a loop) can contain break events and continue events. The iterator, which looks the same as a parameter, represents the current object in the list for each iteration. Beneath it the name of the object is shown in black and the entity type of the object in blue. For more information, see Loop. -
Double-click the loop activity and select the OrderList to iterate over.
-
Drag a Change object activity inside the loop:
-
Double-click the change activity to open its properties.
-
Select the IteratorOrder at the Object drop-down menu and click the New button. This will open the Edit Change Item editor.
-
Set the following properties:
- For Member select OrderStatus.
- For Value enter
MyFirstModule.OrderStatus.Complete
.
- For Member select OrderStatus.
-
Click OK. Your properties screen should look like this:
-
Set Commit and Refresh in Client to Yes to commit your changes to the database and refresh your list in the client so your changes will be visible, then click OK. Your microflow should look like this:
Or, to optimize the number of commits, you can perform the commit outside of the loop. This way, there will be a single commit to the database instead of one per order:
-
Re-deploy your application.
-
Click the Set Processing to Complete button. The orders with status ‘Processing’ will now be changed to ‘Complete’.
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 create a variable, which will be changed for every iteration in the loop.
-
Add a new microflow button to your order data grid with the following details:
- Caption of the button: Calculate Total Order Price
- Name of the microflow: IVK_CalculateTotalPriceOrders
-
In the new microflow, add a Retrieve activity for Orders and a loop for this list to the microflow.
-
Drag a Create variable before Retrieve0 OrdersList.
-
Double-click the Create … variable to open its properties and configure it as follows:
- For Data type select Decimal.
- In the value editor, enter
0
. - For Variable enter CalculatedTotalPrice.
-
Add a Change variable activity inside the loop.
-
Double-click the Change variable activity to open its Properties and set the following properties:
- For Variable select CalculatedTotalPrice.
- For Value enter
$CalculatedTotalPrice + $IteratorOrder/TotalPrice
.
By iterating over the list, the price of every order will be added one by one to the ‘CalculatedTotalPrice’ variable - For Variable select CalculatedTotalPrice.
-
Drag a Show Message action from the toolbox to the end of the microflow.
-
Double-click the message activity to open its properties and set the following properties:
- For Template enter Total calculated price: {1}.
- For Parameters add
toString($CalculatedTotalPrice)
.
- For Template enter Total calculated price: {1}.
-
Click OK. You should see a Microflow like this:
-
Re-deploy your application.
-
Click the Calculate total order price button and you will see the price of all the orders added up.
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.
-
Open the IVK_CalculateTotalPriceOrders microflow and remove the loop and the CalculatedTotalPrice variable.
-
Add an Aggregate list activity after the OrderList.
-
Double-click the aggregate list activity to open its properties and set the following properties:
- For List select OrderList.
- For Function select Sum (see the documentation for the description of the other functions).
- For Attribute select TotalPrice.
- For Variable enter a descriptive name like SumTotalPrice.
-
Click OK.
-
Double-click the message activity and replace the
$CalculatedTotalPrice
variable in the Parameters expression with the$SumTotalPrice
variable. -
Click OK. Your microflow should look like this:
-
Re-deploy your application.
-
Click the Calculate total order price button and you will see the same price of all the orders added up.
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’.
-
Open the microflow IVK_SetOrderToComplete.
-
Open the OrderList retrieve activity.
-
Add an XPath constraint over the association to customer, constraining on the city (Rotterdam) of this customer.
-
Click OK and re-deploy your application.
-
Open the application in the browser.
-
Click the Set Processing to Complete button. All the orders from customers in Rotterdam are set to Complete.