top of page

How to Apply Customization Code in Microsoft Dynamics BC's Existing Codeunit?

Business Central Logo is opened up and looks like a circuit from inside, showing it's customizability.

Co-authored by Tejal Bhatt- Dynamics 365 Consultant

Customization is required for almost any enterprise application. Because one enterprise's needs are different from another and off-the shelf does not always fit. Moreover, an enterprise needs to upgrade continuously. It means efficiency as well as improved consumer engagement and experience.

Customization enables users to make choices and define preferences within a system, with the goal of granting users' greater control over their application experience. Customization enables users to select their preferred information, layout, functionality, or design.

There are numerous ways to personalize Microsoft Dynamics BC in order to provide you and your clients with the tools, functionality, and data you require for your work. It is likely that these modifications will make your workday significantly more productive.

However, you cannot directly add triggers, functions, and events in MS D365 default code-unit.

Nevertheless, we have a solution for it. In order to apply customizations to the existing BC code, you must follow the steps outlined in this blog and you can add any customization in no time.

Let's get started. First, we will take a closer look at what a codeunit is for the sake of clarity.

What is a Codeunit in Dynamics BC?

A container for AL code that may be utilized in several application objects is referred to as a “Codeunit”. The implementation of a business logic is often done as codeunits, and objects that need to carry out a particular kind of logic will call the relevant codeunit.

Dynamics BC: Add a custom code in 5 easy steps!

Step 1 – Creating new .AL file

The only way to get customized features in an existing codeunit is using event subscription. To accomplish this, we must first establish a new code unit extension to which we may add our code.

To create a new code unit, select “New AL File Wizard” and create a new .al file as displayed in the image 1 below.

Image 1: New File from New AL File Wizard

New File from New AL File Wizard

Step 2 – Locating the event

Once you have created the new .AL file search for the event you want to assign, press Alt+Shift+E in VS code as shown in image 2 to open the search bar. Type in the name of your event. For instance, we have used “OnBeforePostSalesDoc”.

Image 2: Search for event with Alt+Shift+E

Search for event with Alt+Shift+E

If you do not know which event name was used for your customization, you can use the event recorder to locate the name of the relevant event.

Image 3: How to find event recorder?

How to find event recorder?

If you need help in locating event recorder and using it, read this doc.

Step 3 – Select the event you want to edit

To demonstrate the process of customization, we've assigned ourselves a sample task. Task is as follows - prior to submitting the sales invoice, we've ensured that the external document number should not be empty. The system will not allow posting a sales invoice without entering the external document number. For that we will select event “OnBeforePostSalesDoc Codeunit Sales-Post” as displayed in screen shot below.

Image 4: Selecting event - “OnBeforePostSalesDoc Codeunit Sales-Post”

Selecting event - “OnBeforePostSalesDoc Codeunit Sales-Post”

Step 4 – Add your code

Select the event you wish to customize and write your code in that event as displayed in below screen shot.

Image 5: Entering code in the event

Entering code in the event

Step 5 – Done. Simply, run the event.

Your custom code will be executed as soon as the event is fired. Our sample task result shows that the document number is missing and will not allow posting of a sales invoice without it, as can be seen in the image below.

Image 6: Document number error, code is successfully run.

Document number error, code is successfully run.

Customization Jedi

You can design and implement any customization you can think of in the Business Central environment using this method, like a Jedi bending the forces of nature. Personalization is a need for clients. Often, developers are unable to achieve customization because of the constraints of intrinsic features in the software they are developing. No more. Use this guide to personalize and add any feature Dynamics BC, and you'll be able to wow your clients.

511 views0 comments


bottom of page