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
- To make this tutorial more accessible, we’ll use the learning path project “Create a Business Event Service in Studio Pro”.
- The first project is located in section 3.1.1 called “Get Ready for the First App: Order Management”. Click on “Starter app — OrderManagement” to download
3. 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
4. Open an instance of Mendix Studio Pro and click on “Import App Package”. Select the file regarding the first project with the name “Business Events-V10-L3.1.1”
5. Choose “New Mendix Team Server” and change the App Name to “BusinessEvents3”
6. After loading the project, right-click on the “OrderManagement” module, go to “Add other” and click on “Business event service”
7. Select “Create a new business event service” and change the Document name to “BE_OrderStatusUpdateService”
8. Click “Add” under the tab “Business event definitions”
9. As a business event write “OrderCancelled”
10. Add a new attribute
11. Write as a Name “OrderID” and select “Integer” as a Type
12. On step 2, under the tab “Other apps can” select the checkbox “Subscribe to events”
13. Go to the domain model and check if an entity was created. If yes, all went well.
14. Inside the folder “OverviewPages”, click on the “Order_NewEdit” page
15. Right-click on the “Save” button and click to go to the microflow
16. 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
17. 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”
18. Following the happy way, add a “Create Object” action.
Select as an Entity “OrderCancelled” and add the attribute “OrderID” with the value “$Order/OrderID”
19. 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 canceled”. Fill the Business event with the value “$NewOrderCancelled”.
20. Before the end event, add a “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)
21. Check if your final microflow is similar to the one in the image below.
22. Go back to the Business Event and click on the button “Export AsyncAPI Document”
23. Click on the button “Publish” on the top of Studio Pro
24. 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”
25. Right-click on the module “OrderFulfillment” -> Add Other -> Business event service.
26. Select “Use an existing business event service”. As a Document name write “BE_OrderStatusUpdateService” and choose the yaml file we just downloaded.
27. Under the tab Subscribe, click on “Add”
28. To the business event, select “OrderCancelled”.
Business event entity: “CBE_OrderCancelled”
Handler Microflow: “HandleBE_OrderCancelled”
After clicking “ok”, check on the domain model if the entity was created.
29. Go to the “HandleBE_OrderCancelled” microflow. Add a retrieve action with the following properties.
30. Add a Change Object activity. Select as an Object the retrieved object “Shipping”. Change the “ShippingStatus” and fill in the value “OrderFulfillment.ShippingStatus.Cancelled”
31. Click to “Publish” the app.
32. Click in View App in both projects to see them on the browser
33. On the Manage Customer Orders project, click on new to create a new object.
Order date: Select any date
Total amount: Any amount
Order Status: “Pending” (Can’t be “Cancelled”)
34. Go to the Shipping app and create a new object.
Order ID: It 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”)
35. Return to the Manage Customer Orders project and select the object created. Change the “Order Status” to “Cancelled” and Save.
36. Go to the Shipping app and refresh the page. If everything went well, the Shipping Status should be “Cancelled”
You’ve made it to the end!
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
Read more
From the writer
If you enjoyed this article you could find more on our Medium page. For great videos, you can visit our YouTube page.
Are you interested in getting more involved with our Discord community? Join us on our Discord Community Channel.
From the Publisher -
Inspired by this article to bring your ideas to life with Mendix? Sign up for a free account! You’ll get instant access to the Mendix Academy, where you can start building your skills.
For more articles like this one, visit our Medium page. And you can find a wealth of instructional videos on our community YouTube page.
Speaking of our community, join us in our Slack community channel. We’d love to hear your ideas and insights!