Event-Driven Architecture: How to Create Custom Business Events in Business Central
Introduction
Events are notifications that signal something has happened in the system.
A record getting created, updated or deleted, a sales order getting posted, etc are all examples of events.
An event driven architecture provides asynchrony and decoupled communication between different components of the system supporting flexibility, scalability and modularity.
In Business Central, we have two main types of events - Business and Integration.
Integration Events are related to things within Business Central.
They are use for extending the functionality of the Base Application without modifying it directly.
Business Events are related to things outside of Business Central.
They are used for notifying external systems (e.g., via Power Automate or Logic Apps) regarding any changes that happen within Business Central.
Microsoft allows us to create custom Integration and Business Events based on our requirements.
In this blog, we'll see how to create a custom Business Event and use it to notify our Business Manager whenever a user logs in.
References
Configuration
I'll create a new project in Visual Studio Code using AL Go.
I'll create a simple Enum Extension.
Then I'll create a codeunit which will hold my custom Business Event.
In this codeunit, firstly I've defined my Business Event using the "External Business Event" decorator which takes the name, description, category and version as parameters.
And then as I am targeting it to be triggered when a User logs in so I'll subscribe the event and then call my custom method inside it.
And that's it!
Once you deploy your extension to Business Central, your custom event will be available to be subscribed.
I've written a simple flow on Power Automate to consume it.
I'll choose the trigger "When a business event occurs" from the Business Central connector and sign in to the appropriate tenant.
We can see that the parameters that we added on the custom Business Event are available here to be used.
I tried to add some documentation to these parameters but I couldn't figure out a way to get it to work.
Conclusion
An event-driven approach makes your setup more flexible, easier to manage, and better prepared for future changes.
In Business Central, adding a custom event is super simple and once you’ve done that, connecting it to something like Logic Apps becomes very easy.
What’s great is that you don’t need to write complex code or worry about breaking things later. If your process changes down the line, you can just update the logic that listens to the event; no need to touch the core functionality.
That means less stress, cleaner code, and a much more maintainable system in the long run.
That means less stress, cleaner code, and a much more maintainable system in the long run.
Comments
Post a Comment