Events
Power BI DataViz World Championships
Feb 14, 4 PM - Mar 31, 4 PM
With 4 chances to enter, you could win a conference package and make it to the LIVE Grand Finale in Las Vegas
Learn moreThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
A codeunit is a container for AL code that you can use in many application objects. You typically implement business logic in codeunits and call the codeunit from the object that needs to perform that specific logic.
Typing the shortcut tcodeunit
will create the basic layout for a codeunit object when using the Business Central in Visual Studio Code.
This codeunit example checks whether a given customer has registered a shoe size. If not, the customer is assigned a shoe size of 42. The example is for illustrational purposes only, the customer table doesn't have a ShoeSize
field by default.
The codeunit can be used both as a direct call to codeunit.run(customer)
or as a call to the procedure inside the codeunit createcustomer.CheckSize(customer)
.
codeunit 50113 CreateCustomer
{
TableNo = Customer;
trigger OnRun();
begin
CheckSize(Rec);
end;
procedure CheckSize(var Cust : Record Customer)
begin
if not Cust.HasShoeSize() then
Cust.ShoeSize := 42;
end;
}
The this
keyword can be used in codeunits in AL as a self-reference, and it allows passing the current object as an argument to methods. Additionally, using the this
keyword enhances readability in larger methods by clearly indicating whether a variable is in the global or local scope. Learn more about the this
keyword in Use the this keyword for codeunit self-reference.
Developing extensions
Table extension object
Page extension object
AL development environment
Documenting your code with XML comments
Use the this keyword for codeunit self-reference.
Events
Power BI DataViz World Championships
Feb 14, 4 PM - Mar 31, 4 PM
With 4 chances to enter, you could win a conference package and make it to the LIVE Grand Finale in Las Vegas
Learn moreTraining
Module
Work with codeunits in Dynamics 365 Business Central - Training
Do you want to create your own codeunit and learn how to access the functions within that codeunit? If so, this module is the ideal place to start.
Certification
Microsoft Certified: Dynamics 365 Business Central Developer Associate - Certifications
Demonstrate you have the skills to design, develop, test, and maintain solutions based on Dynamics 365 Business Central.