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

See also

Developing extensions
Table extension object
Page extension object
AL development environment
XML comments in code