As a powerful and flexible ERP system, Business Central allows for customization to meet the unique needs of businesses. One key aspect of this customization is the use of events, which enable developers to subscribe to actions or changes in the system and execute custom code in response. In this blog post, we’ll explore how events can be leveraged in Business Central and provide an example of how they can be used to add custom functionality.

What are Events in Business Central?

Events are notifications that are raised when certain actions or changes occur in Business Central. These can be used to trigger custom code or procedures that provide additional functionality or automation. There are two types of events in Business Central: platform events and manual events.

Platform events are predefined events that are raised by the system in response to specific actions or changes, such as creating a new record or updating an existing one. Manual events, on the other hand, are events that are defined and raised by custom procedures.

How to Subscribe to Events in Business Central?

Developers can subscribe to events in Business Central by using the EventSubscriber attribute in the AL language. This attribute specifies the event that the procedure is subscribing to and the order in which it should be executed relative to other subscribers.

For example, to subscribe to the OnAfterInsertEvent platform event for the Sales Header table, you can use the following code:

[EventSubscriber(ObjectType::Table, Database::SalesHeader, 'OnAfterInsertEvent', '1000', true, true)]
procedure SalesHeader_OnAfterInsertEvent(var Rec: Record "Sales Header")
begin
    // Custom code here
end;

In this example, the EventSubscriber attribute is used to subscribe to the OnAfterInsertEvent event for the Sales Header table. The procedure is specified as the subscriber, with a priority of 1000. The true, true parameters specify that the procedure should be executed synchronously and should not be called if the transaction is rolled back.

How to Raise Events in Business Central?

Developers can also raise events themselves using the EventPublisher data type in the AL language. This allows custom procedures to raise events when certain conditions are met, allowing other procedures to respond accordingly.

For example, to raise a custom event when a sales order is approved, you can use the following code:

var
    Event: Codeunit "MyEventPublisher";
begin
    if SalesHeader.Status = SalesHeader.Status::Approved then
        Event.RaiseCustomEvent('OnSalesOrderApproved', SalesHeader);
end;

In this example, the EventPublisher data type is used to raise a custom event called OnSalesOrderApproved when a sales order is approved. The SalesHeader record is passed as a parameter to the event subscribers.

Example: Using Events to Add Custom Validation

Now, let’s take a look at an example of how events can be used to add custom validation to the system. In this scenario, we want to perform additional validation when a sales order is created in the system.

First, we’ll subscribe to the OnAfterInsertEvent platform event for the Sales Header table:

[EventSubscriber(ObjectType::Table, Database::SalesHeader, 'OnAfterInsertEvent', '1000', true, true)]
procedure SalesHeader_OnAfterInsertEvent(var Rec: Record "Sales Header")
begin
    // Perform additional validation here
end;
Next, we'll implement the logic for our custom procedure:
[EventSubscriber(ObjectType::Table, Database::SalesHeader, 'OnAfterInsertEvent', '1000', true, true)]
procedure SalesHeader_OnAfterInsertEvent(var Rec: Record "Sales Header")
var
  Customer: Record "Customer";
begin
  // Get the customer record
if Customer.Get(Rec."Sell-to Customer No.") then
begin
  // Check if the customer has exceeded their credit limit
if Rec.Amount > Customer."Credit Limit" then
  Rec."Credit Limit Exceeded" := true;
end;
end;


In this example, we’re using the `OnAfterInsertEvent` event to perform additional validation when a sales order is created. We retrieve the associated customer record and check if the order amount exceeds their credit limit. If it does, we set a custom field on the Sales Header record to indicate that the credit limit has been exceeded.

By leveraging events in Business Central, we can easily extend the functionality of the system and add custom validation or automation. Whether using platform events or manual events, developers can subscribe to actions and execute custom code in response, allowing for a highly flexible and customizable ERP solution. Conclusion Events are a powerful feature in Business Central that can be used to extend the functionality of the system and add custom validation or automation. By subscribing to events and executing custom code in response, developers can tailor the ERP solution to meet the unique needs of their business. Whether using platform events or manual events, events provide a high degree of flexibility and customization, making Business Central a powerful tool for businesses of all sizes.

If you’re new to events in Business Central, start by exploring the available platform events and consider how they might be used in your organization. Many common tasks, such as validating data or triggering workflows, can be accomplished using platform events.

For more complex or specific requirements, manual events can be created and subscribed to within your custom code. This provides a high degree of flexibility and allows you to tailor the system to meet your unique needs.

When using events in Business Central, it’s important to remember to test thoroughly and consider the impact on other areas of the system. Changes made to event subscribers or event publishers can have unintended consequences, so it’s important to be diligent in testing and reviewing changes.

In summary, events are a powerful and flexible feature in Business Central that can be used to extend the functionality of the system and add custom validation or automation. By leveraging events, businesses can create a tailored ERP solution that meets their unique needs and provides a competitive advantage in the marketplace.

If you enjoyed reading this post, please leave a comment below and let us know your thoughts. We would love to hear your feedback and suggestions for future blog topics.

We also invite you to share this post with your colleagues and friends who might find it helpful. You can use the social media icons below to share this post on your favorite social media platform.

And lastly, don’t forget to subscribe to our website itbeast.in for more blogs on information technology. We regularly publish informative and engaging content on topics related to IT and technology.

Here are some links to our popular blog posts and social media accounts:

Thank you for your support, and we look forward to sharing more informative content with you in the future.

Best regards,

The itbeast.in team.