Working with Lists in a Microflow
Introduction
In this document, you will learn how to enrich the data management capabilities of your microflow by using a list. A list consists of objects of the same type, which can be filtered by using an XPath constraint. For example, you can configure your microflow to retrieve a list of completed orders from the database. You can also further process this data, for example, by calculating the total value of those orders.
This document teaches you how to do the following:
- Retrieve and filter a list of objects from the database
- Update multiple objects by iterating over a list
- Calculate the total list value
Preparing the Data Structure, GUI, and Example Data
Before you continue, you should first set up a test app, and populate it with test data. To do so, follow these steps:
-
Create a domain model with the following entities:
-
Customer
Attribute name Attribute type CustomerID String Name String Address String ZipCode String City String -
Order
Attribute name Attribute type Number Integer Date Date and time TotalPrice Decimal OrderStatus Enumeration
One Customer entity can be associated with many Orders, so set the association between the entities accordingly.
-
-
Create overview and detail pages to manage the Customer and Order objects.
-
Create menu items to access the Customer and Order overview pages.
-
Add the following Customer data to your app:
Name Address Zip code City Olav Gedempte Zalmhaven 34 3050 TE Rotterdam Tim Kornoeljestraat 14 2514 RT Den Haag Peter Meloenstraat 123 2565 PE Den Haag Harry Emmerreklaan 25 1458 PE Utrecht -
Add the following Order data to your app:
Number Customer Date Total price Order status 1 Harry 1/28/2022 345.00 Open 2 Olav 12/30/2021 1234.60 Processing 3 Peter 1/5/2022 23.60 Open 4 Tim 1/4/2022 586.90 Complete 5 Olav 1/21/2022 25.60 Open 6 Peter 1/16/2022 154.00 Complete
Retrieving and Filtering a List of Objects from the Database
Use a microflow with a Retrieve activity to retrieve a list of objects, and then filter that list by applying an XPath constraint. For example, the microflow can retrieve all orders from the database, and then filter that list to only the orders with the Processing status.
-
Create a new microflow by right-clicking your module and selecting Add > Microflow.
-
In the Add Microflow dialog box, in the Name field, enter IVK_SetOrderToComplete, and then click OK.
-
On the Orders overview page, add an Action button to the toolbar.
-
Double-click the Action button and change the Caption to Set Processing to Complete.
-
In the On click list, 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 and search for the Retrieve activity.
-
Drag a Retrieve activity from the Toolbox to the line between the start and end events.
-
Double-click the Retrieve activity, and then set the following properties:
- Source – select From database
- Entity – select Order
- List – enter OrderList
-
To filter the list to only orders with the status Processing, in the XPath constraint field, add the following XPath expression:
[OrderStatus = 'Processing']
.
Sales.Order_Customer/Sales.Customer/City = 'Rotterdam'
.
Updating Multiple Objects by Iterating over a List
After retrieving a list of orders with the status Processing, use a loop to iterate over this list and change the status of each object to Complete.
-
Open the IVK_SetOrderToComplete microflow that you created in the previous section.
-
Drag a Loop activity from the Toolbox, and place it between the OrderProcessingList activity and the end event of the microflow.
-
Double-click the Loop activity.
-
In the Iterate over list, select OrderList, and then click OK.
-
Drag a Change object activity into the Loop activity.
-
Double-click the Change object activity.
-
In the Object list, select IteratorOrder, and then click New.
-
In the Edit Change Item dialog box, configure the following settings:
- Member – select OrderStatus
- Value – enter MyFirstModule.OrderStatus.Complete
-
Click OK, and then click OK again.
-
Drag a Commit activity from the Toolbox and place it after the Loop activity.
-
Double-click the Commit activity.
-
In the Commit Object(s) dialog box, configure the following settings:
- Object or List – select OrderList
- Refresh in Client – set to Yes; this settings refreshes your list in the client so that your changes are visible
-
Click OK, and then save the microflow.
-
Run your application locally.
-
On the Orders overview page, click Set Processing to Complete.
Calculating the Total List Value by Using a Variable and a Loop
To calculate the total sum of all your orders via a loop, create a variable which will be modified by every iteration of the loop.
-
On the Orders overview page, add a new Call microflow button with the following settings:
- Caption – enter Calculate Total Order Price
- Name – enter IVK_CalculateTotalPriceOrders
-
In the IVK_CalculateTotalPriceOrders microflow, add a Retrieve activity from the Toolbox to the line between the start and end events.
-
Double-click the Retrieve activity, and then set the following properties:
- Source – select From database
- Entity – select Order
- List – enter OrderList
-
Drag a Loop activity from the Toolbox, and place it between the Retrieve activity and the end event of the microflow.
-
Double-click the Loop activity.
-
In the Iterate over list, select OrderList, and then click OK.
-
Drag a Create variable activity from the Toolbox and place it before the Retrieve activity.
-
Double-click the Create variable activity and configure the following settings:
-
Data type – select Decimal
-
Value – enter 0
-
Variable – enter CalculatedTotalPrice
-
-
Drag a Change variable activity into the Loop activity.
-
Double-click the Change variable activity and configure the following settings:
-
Variable – select CalculatedTotalPrice
-
Value – enter
$CalculatedTotalPrice + $IteratorOrder/TotalPrice
With the above settings, as the loop iterates over the list, it adds the price of every order to the CalculatedTotalPrice variable.
-
-
Drag a Show Message activity from the Toolbox and place it after the Loop activity.
-
Double-click the Show message activity and configure the following settings:
- Template – enter Total calculated price: {1}
- Parameters – enter toString($CalculatedTotalPrice)
-
Click OK, and then save the microflow.
-
Rerun your application locally.
-
On the Orders overview page, click Calculate total order price.
Calculating the Total List Value by Using an Aggregate Function
Instead of a loop, you can also calculate the total price by using the aggregate list function. Use the aggregate list to calculate values such as the maximum, minimum, sum, average, and total number of objects over a list of objects.
-
In the IVK_CalculateTotalPriceOrders microflow, remove the loop and the CalculatedTotalPrice variable.
-
Add an Aggregate list activity after the OrderList.
-
Double-click the Aggregate list activity, and then set the following properties:
- List – select OrderList
- Function – select Sum
- Attribute – select TotalPrice
- Variable – enter a descriptive name, for example, SumTotalPrice
-
Click OK.
-
Double-click the Show message activity.
-
In the Parameters section, delete the $CalculatedTotalPrice variable and add the $SumTotalPrice variable.
-
Click OK, and then save the microflow.
-
Rerun your application locally.
-
On the Orders overview page, click Calculate total order price.