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 AL Language extension 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 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
{
    trigger OnRun();
    TableNo = Customer;
    begin
        CheckSize(Rec);
    end;
    procedure CheckSize(var Cust : Record Customer)
    begin
        if not Cust.HasShoeSize() then
            Cust.ShoeSize := 42;
    end;
}

See Also

Developing Extensions
Table Extension Object
Page Extension Object
AL Development Environment