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:
Recordfor table rowsListandDictionaryfor 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
TestFieldor an explicitError. - 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
- 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. - Boundary practice. For
Code[20],Text[100], andDecimal, write one valid and one invalid sample value for each type. - Variable scope cleanup. Move at least two global variables into a local procedure scope where they are only used once.
- Consistent identifier typing. Check every place where
Loan No.is used and ensure the type is consistentlyCode[20]. - Validation guard. Add explicit validation so
Principal AmountandInterest Amountcannot be zero or negative before calculation. - Try it yourself. Add one extra field of your choice (for example,
External Reference) and decide whether it should beCodeorText, 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.