Data Types and Variables in AL

Master primitive and complex AL data types, variable declaration, and initialization patterns.

25 minBeginner

Learning Objectives

  • Use primitive types — Integer, Decimal, Boolean, Text, DateTime
  • Work with complex types — Record, Array, Option/Enum
  • Declare and initialize variables correctly in AL

Primitive Data Types

AL supports these fundamental data types:

TypePurposeExample
IntegerWhole numbersCount: Integer
DecimalNumbers with fractionsAmount: Decimal
BooleanTrue/FalseIsActive: Boolean
TextStrings (fixed length)Name: Text[100]
CodeAlphanumeric codesCustomerNo: Code[20]
DateCalendar datesPostingDate: Date
DateTimeDate and timeCreatedAt: DateTime
GUIDGlobally unique identifierRecordId: Guid

Complex Data Types

TypePurpose
RecordRepresents a row in a table
ArrayCollection of elements of the same type
Option / EnumPredefined set of values (Enum preferred in modern AL)

Variable Declaration

Variables are declared with the var keyword:

VariableExamples.Codeunit.al
procedure ProcessCustomer()
var
    CustomerName: Text[100];
    TotalAmount: Decimal;
    IsProcessed: Boolean;
    CustomerRec: Record Customer;
begin
    CustomerName := 'Contoso Ltd.';
    TotalAmount := 1000.50;
    IsProcessed := false;
end;

Text Length

Always specify Text length: Text[100] not Text. Unbounded Text is only used in specific scenarios.

Record Variables

Record variables are the most common complex type — they represent table rows:

var
    SalesHeader: Record "Sales Header";
    SalesLine: Record "Sales Line";
begin
    if SalesHeader.Get(SalesHeader."Document Type"::Order, 'SO-001') then
        Message('Found order for %1', SalesHeader."Sell-to Customer Name");
end;

Enum vs Option

Modern AL uses Enum instead of legacy Option:

enum 50100 "Approval Status"
{
    value(0; Open) { }
    value(1; Approved) { }
    value(2; Rejected) { }
}

Option is Legacy

Option fields still exist in the base application but new development should use Enum objects.

Key Revision Points (MB-820)

  • Know primitive vs complex data types
  • Record variables are used for all table operations
  • Enum replaces Option in modern AL development
Open Source

Help improve this platform

NAVBC Learning is open source. Report issues, suggest improvements, or join discussions on GitHub.