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
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
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"
4º Choose "New Mendix Team Server" and change the App Name to "BusinessEvents3"
5º After load the project, right click on the "OrderManagement"
module, go to "Add other" and click on "Business event service"
6º Select "Create a new business event service" and change the Document name to "BE_OrderStatusUpdateService"
7º Click "Add" under the tab "Business event definitions"
8º As a business event write "OrderCancelled"
9º Add a new attribute
10º Write as a Name "OrderID" and select "Integer" as a Type
11º On the step 2, under the tab "Other apps can" select the checkbox "Subscribe to events"
12º Go to the domain model and check if an entity was created. If yes, all went well.
13º Inside the folder "OverviewPages", click on the "Order_NewEdit" page
14º Right click on the "Save" button and click to go to the microflow
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
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"
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"
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".
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)
20º Check if your final microflow is similiar to the one on image below.
21º Go back to the Business Event and click on the button "Export AsyncAPI Document"
22º Click in the button "Publish" on the top of Studio Pro
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"
24º Right click on the module "OrderFulfillment" -> Add Other -> Business event service.
25º Select "Use an existing business event service". As a Document name write "BE_OrderStatusUpdateService" and choose the yaml file that we just downloaded.
26º Under the tab Subscribe, click in "Add"
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.
28º Go to the "HandleBE_OrderCancelled" microflow. Add a retrieve action with the following properties.
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"
30º Click to "Publish" the app.
31º Click in View App in both projects to see them on the browser
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")
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")
34º Return to Manage Customer Orders project and select the object created. Change the "Order Status" to "Cancelled" and Save.
35º Go to the Shipping app and refresh the page. If everything went well, the Shipping Status should be "Cancelled"
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)