DEV Community

MendixWithMe
MendixWithMe

Posted on

How to use Business Events | Mendix 10

Mendix Business Events enable applications to announce important occurrences and choose to receive such updates independently. Unlike direct communication methods like REST or Web Services, Business Events facilitate event notifications between apps without direct app-to-app interaction. 
In this blog post, I will teach you how to use Business Events in Mendix!

Step by Step

1º To make this tutorial more accessible, we'll use the learning path projects "Create a Business Event Service in Studio Pro".
The first project is located on the 3.1.1 section called "Get Ready for the First App: Order Management".
Click on "Starter app - OrderManagement" to download

Image description

2º Download the second project on the 4.1.1 section called "Get Ready for the Second App: OrderFulfillment"
Click on "Starter app - OrderFulfillment" to download

Image description

3º Open an instance of Mendix Studio Pro and click in "Import App Package". Select the file regarding the first project with the name "Business Events-V10-L3.1.1"

Image description

Image description

4º Choose "New Mendix Team Server" and change the App Name to "BusinessEvents3"

Image description

5º After load the project, right click on the "OrderManagement"
module, go to "Add other" and click on "Business event service"

Image description

6º Select "Create a new business event service" and change the Document name to "BE_OrderStatusUpdateService"

Image description

7º Click "Add" under the tab "Business event definitions"

Image description

8º As a business event write "OrderCancelled"

Image description

9º Add a new attribute

Image description

10º Write as a Name "OrderID" and select "Integer" as a Type

Image description

11º On the step 2, under the tab "Other apps can" select the checkbox "Subscribe to events"

Image description

12º Go to the domain model and check if an entity was created. If yes, all went well.

Image description

13º Inside the folder "OverviewPages", click on the "Order_NewEdit" page

Image description

14º Right click on the "Save" button and click to go to the microflow

Image description

15º Drag and drop a retrieve action to the microflow and fill the options with the following information:
Source: From Database
Range: First
XPath: [OrderID = $Order/OrderID]
Object Name: OrderBeforeChange

Image description

Image description

16º After the close page action, add a decision and write as a caption "OrderStatus changed to Cancelled".
For the expression: 
"$Order/OrderStatus != $OrderBeforeChange/OrderStatus
and
$Order/OrderStatus = OrderManagement.OrderStatus.Cancelled"

Image description

Image description

17º Following the happy way, add a "Create Object" action.
Select as a Entity "OrderCancelled" and add the attribute "OrderID" with the value "$Order/OrderID"

Image description

18º On the toolbox search for "Publish business event" and add to your microflow. Double click on the action and as a Subject write "An order is cancelled". Fill the Business event with the value "$NewOrderCancelled".

Image description

Image description

19º Before the end event, add an "Log Message" activity.
Log Level: Info
Log node name: 'OrderManagement"
Template: {1}.{2} is created at {3}
Parameters: {1} - $NewOrderCancelled/ServiceName
{2} - $NewOrderCancelled/EventName
{3} -toString($NewOrderCancelled/createdDate)

Image description

20º Check if your final microflow is similiar to the one on image below.

Image description

21º Go back to the Business Event and click on the button "Export AsyncAPI Document"

Image description

Image description

22º Click in the button "Publish" on the top of Studio Pro

Image description

23º Open a new instance of Studio Pro and Import a new app Package.
Click on the "Business events - V10 - L4.1.1" file. Create the project with the name "Businessevents4"

Image description

24º Right click on the module "OrderFulfillment" -> Add Other -> Business event service.

Image description

25º Select "Use an existing business event service". As a Document name write "BE_OrderStatusUpdateService" and choose the yaml file that we just downloaded.

Image description

26º Under the tab Subscribe, click in "Add"

Image description

27º To the business event, select the "OrderCancelled". 
Business event entity: "CBE_OrderCancelled"
Handler Microflow: "HandleBE_OrderCancelled"
After click "ok", check on the domain model if the entity was created.

Image description

Image description

28º Go to the "HandleBE_OrderCancelled" microflow. Add a retrieve action with the following properties.

Image description

Image description

29º Add a Change Object activity. Select as a Object the retrieved object "Shipping". Change the "ShippingStatus" and fill with the value "OrderFulfillment.ShippingStatus.Cancelled"

Image description

Image description

30º Click to "Publish" the app.

Image description

31º Click in View App in both projects to see them on the browser

Image description

Image description

32º On the Manage Customer Orders project, click in new to create a new object.
Order date: Select any date
Total amount: Any amount
Order Status: "Pending" (Can't be "Cancelled")

Image description

33º Go to Shipping app and also create a new object.
Order ID: Should be the same number of the object created on the Order app
Carrier: Any Carrier
Actual Delivery date: Select any date
Shipping Cost: Any Amount
Shipping Status: "Pending" (Can't be "Cancelled")

Image description

34º Return to Manage Customer Orders project and select the object created. Change the "Order Status" to "Cancelled" and Save.

Image description

Image description

35º Go to the Shipping app and refresh the page. If everything went well, the Shipping Status should be "Cancelled"

Image description


This is the end of the tutorial.
I'm creating a mendix community on discord to make life easier for devs. You can ask questions, help other people and have access to all the tutorials.
If you want to be part of this community, you can do so through this invite.
Discord: https://discord.gg/YHre8dXz3q


From the writer
If you enjoyed this article you can find more on our Medium page. For great videos, you can visit our Youtube page.
Are you interested in getting more involved with our community? Join us in our Discord Community Channel.

Top comments (0)