Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 4: Data Types and Variables in AL

Objectives

By the end of this chapter you will be able to:

  • Choose appropriate AL primitive and complex data types
  • Declare variables with intent and clear scope
  • Avoid common type-related errors in business logic
  • Prepare strongly typed data handling for table and page work in Chapters 7 and 8

4.1 Primitive data types

AL primitive types include Integer, Decimal, Boolean, Text, Date, and DateTime. Pick the narrowest type that still matches the business meaning of the value. Money and rates belong in Decimal. Compact identifiers, such as document numbers, fit Code[n], and free-form descriptive fields fit Text[n].

4.2 Complex data types

Common complex types include:

  • Record for table rows
  • List and Dictionary for in-memory collections
  • Enums and options for constrained business states

Most business workflows in this book pass Record values into codeunit procedures, especially from Chapter 9 onward.

4.3 Variable declaration and scope

procedure CalculateInstallmentAmount(Principal: Decimal; InterestRate: Decimal): Decimal
var
    InterestAmount: Decimal;
begin
    InterestAmount := Principal * InterestRate;
    exit(Principal + InterestAmount);
end;

Prefer local variables inside procedures. Use a global variable only when procedures in the same object need to share its state.

4.4 Type-safety patterns you will reuse

  • Validate assumptions early with TestField or an explicit Error.
  • Avoid implicit conversions that hide precision or truncation issues.
  • Keep identifier types consistent across objects. For example, a loan number should be Code[20] everywhere.

These habits mean fewer defects later on in table triggers and codeunit logic.

4.5 Type selection flow (visual guide)

When you’re unsure which type to pick, follow this flow:

flowchart TD
    A[What kind of value?] --> B{Identifier with fixed pattern?}
    B -->|Yes| C["Use Code[n]"]
    B -->|No| D{Money or rate?}
    D -->|Yes| E[Use Decimal]
    D -->|No| F{Free-form human text?}
    F -->|Yes| G["Use Text[n]"]
    F -->|No| H{Date only or date+time?}
    H -->|Date only| I[Use Date]
    H -->|Date + time| J[Use DateTime]

Chapter summary

In this chapter you:

  • Reviewed the main AL primitive and complex types
  • Practiced declaring variables with a clear purpose and scope
  • Learned type-safety habits that prevent business-logic bugs later
  • Prepared for strongly typed table and codeunit development

Tasks

  1. Type review (starter set). Classify these fields and justify each choice in one line: Loan No., Installment No., Principal Amount, Paid, Due Date, Payment Date.
  2. Boundary practice. For Code[20], Text[100], and Decimal, write one valid and one invalid sample value for each type.
  3. Variable scope cleanup. Move at least two global variables into a local procedure scope where they are only used once.
  4. Consistent identifier typing. Check every place where Loan No. is used and ensure the type is consistently Code[20].
  5. Validation guard. Add explicit validation so Principal Amount and Interest Amount cannot be zero or negative before calculation.
  6. Try it yourself. Add one extra field of your choice (for example, External Reference) and decide whether it should be Code or Text, with reasoning.

Check your work: compare your answers with the Chapter 4 solutions, then confirm your type choices stay consistent in Chapters 7, 9, and 15.