How to use Business Events | Mendix 10
Tags: webdev, mendix, business, events
Published: 2023-09-05 22:40:36

{% youtube xEYi-BN11Gg %}

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2nbxbjd5fq3o96am5ja.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uo8k41ceat46q5bea7sl.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvygn99kekeupv1x0ur2.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxcaq4o4j4cntsq6idbe.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e214vxeycek9yuq38t34.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fn24711jqd5xwn4vq7af.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gss8ob67jz0cskegm4u7.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10o20bztdamk5s9s7flr.png)

8º As a business event write "OrderCancelled"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mwk791ec62pmo0o06z3.png)

9º Add a new attribute

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjt6o070puah0ra1yfh5.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcpq6p4y20dss3kls2mj.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/855wouvtw3isa33bx6q0.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmtfthk9clye0ayatwhn.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mxpe45f8r94u5d9ku8f.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ewgzm0tjxop5lgeczwi.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/434f5ec6ha2eioj6a1ij.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6noqay47fa1v76r18fqy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hb9nhvs2byx10kuyuipr.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnc76svqzez1128qfkxo.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3otkaakshmsu31ogg8sf.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6r29mhc81d3mmo3lxex.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hwd34j6u92h0l5r4kkb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwot5qdu2zuprg7e82ji.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1x8vi10bk6jsbjpbd78.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9taswai4xptscigwfdvn.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyzp313r8zmwusdcyov8.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1luvjty2x2xk6bms9fz5.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02iqapx2728i7ozkue6n.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0mej28on8qvcnn63axk.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cpgnh8549t6ap73di6x.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbpkusg1pbkv64c19n99.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9bvpcivtelfr1798f9l.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e05kekj2ni94rerbs11n.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezyuiya6vgmegpdf4exj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zy8to5osgjmise9i7l9.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/em4634v3dn2hsyttb7sc.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pn2hoa3dlnuljm155oeu.png)

30º Click to "Publish" the app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gic3gquqqiaytts20kov.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofe7x9q49c83nb1w05qy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2s9vfxfsi1s8bba4e85.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/452doen039fgr3obpzql.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32l1z6w01bjg82j7h5h1.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/537oh3ehfvlo4q6qjfig.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ok8td2royi8eirlvrhes.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4jlb6lhawtv2bxxm443.png)


---

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](https://discord.gg/YHre8dXz3q)

---

**_From the writer_**
If you enjoyed this article you can find more on our [Medium page](https://medium.com/r/?url=https%3A%2F%2Fmendixwithme.medium.com%2F). For great videos, you can visit our [Youtube page](https://medium.com/r/?url=https%3A%2F%2Fwww.youtube.com%2Fchannel%2FUCtRcKyh_4dvK-uCBtnbUWFw).
Are you interested in getting more involved with our community? Join us in our [Discord Community Channel](https://discord.gg/YHre8dXz3q).