Codeunit object

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.

Snippet support

Typing the shortcut tcodeunit will create the basic layout for a codeunit object when using the Business Central in Visual Studio Code.

Codeunit example

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;
}

Codeunit self-reference keyword

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.