Codeunits in Microsoft Dynamics 365 Business Central
In Microsoft Dynamics 365 Business Central, there are a lot of codeunits already available. Every codeunit is a collection of different functions. If they are global functions, they can be called from within your own code. There are codeunits that help you work with XML data, help you with importing and exporting data, setting up number series, and so on. We will talk about some codeunit fundamentals, see how we can create functions and variables in a codeunit, and how we can run a codeunit from a page.
A codeunit is a core object of the Microsoft Dynamics 365 Business Central application, together with Tables and Pages.
A CodeUnit contains:
A codeunit groups different procedures in one object. These procedures can be accessed from within other procedures and triggers on pages, tables, and other codeunits.
Creating a Codeunit
Create a new file, call it in a standard format, and then we can use the code snippet which is T, codeunit type, which will create a codeunit. Provide the Number and name of the codeunit.
Figure 1 - Creating a new file for a codeunit in Microsoft Dynamics 365 Business Central on premises
When we create a new codeunit, the always on run trigger will be created. To create some new procedures, just type procedure, the name of your procedure, then there will be a begin and an end. And in between the begin and the end, you can start writing your code. To create your variables, you can go to the end of your codeunit, and here we will find the global variables. So it is a global variable available for all procedures in the codeunit. If you would like to create a local variable, you go into the procedure, type var, and then you can create your variable of type.
Figure 2 - Creating local and global variables, and a procedure for a codeunit in Dynamics Business Central on premises
If you would like to create a function, you go into the procedure and type the necessary code for the function. As an example, below I am using the addition function, so am passing two inputs; number1 integer and number2 integer. We have declared two integer values, so when I call function add it will pass those values. When the function is executed the Message gets executed and displays the result.
Figure 3 – Function for add in Dynamics Business Central on premises
We need to run this codeunit so we can make a page extension by using the page extension snippet. Specify where you want to place it. In the action, we will see that there's a trigger with the name on action. So, when you click an action or when you click a button, it will trigger the on action trigger. Now, in the trigger we can create a variable like we did with the codeunit. Using that variable, we can call the codeunit. After finishing these steps lets publish it by hitting F5 or the shortcut Ctrl + G to select > AL: Publish.
Figure 4 –PageExtension for the codeunit in Dynamics Business Central on premises
If you have any questions about codeunits, page extensions, or other Dynamics NAV or Business Central questions for any version, contact ArcherPoint.
Read more "How To" blogs from Archer Point for practical advice on using Microsoft Dynamics Business Central or NAV. If you are interested in Dynamics NAV/Business Central development, check out our collection of NAV Development Blogs.