Codeunits in AL
Codeunits in AL are used to encapsulate business logic and system behavior in a structured, reusable, and upgrade-safe way. They are the primary objects responsible for processing, calculations, validations, and orchestration of business processes in Business Central.
This page explains what codeunits are, why they exist, and how they should be used correctly, with practical examples. The goal is to help learners understand where business logic belongs before diving into events, subscribers, or advanced patterns.
What Is a Codeunit in AL?
A codeunit is an AL object that contains procedures (methods) used to perform business logic. Unlike pages or reports, codeunits do not display data and do not define layouts. Unlike tables, they do not define storage. Their sole responsibility is behavior.
A codeunit:
• Executes logic
• Manipulates records
• Coordinates processes
• Exposes reusable procedures
A codeunit does not:
• Store data
• Define UI
• Replace tables or pages
Codeunits are the logic layer of Business Central.
Why Codeunits Exist in Business Central
ERP systems require complex, reusable logic:
• Posting routines
• Calculations
• Validations
• Automation
• Integration handling
Placing this logic in pages or reports leads to duplication and instability. Codeunits exist to:
• Centralize business logic
• Promote reuse
• Improve maintainability
• Support event-driven extensibility
They are essential for clean architecture.
Basic Structure of a Codeunit
A codeunit is defined using the codeunit object and consists primarily of procedures and optional triggers.
Example: Basic Structure
codeunit 50160 "Customer Feedback Management"
{
}
The object itself does nothing until procedures or triggers are defined.
Simple Codeunit Example
The following example shows a codeunit that encapsulates a business operation.
Example: Simple Codeunit
codeunit 50160 "Customer Feedback Management"
{
procedure CreateFeedback(CustomerNo: Code[20]; FeedbackText: Text[250])
var
Feedback: Record "Customer Feedback";
begin
Feedback.Init();
Feedback."Customer No." := CustomerNo;
Feedback."Feedback Text" := FeedbackText;
Feedback.Insert(true);
end;
}
This codeunit:
• Contains reusable logic
• Interacts with a table
• Does not depend on UI
• Can be called from pages, reports, or other codeunits
Calling a Codeunit From a Page
Codeunits are commonly invoked from UI actions.
Example: Calling Codeunit from Page
var
FeedbackMgt: Codeunit "Customer Feedback Management";
begin
FeedbackMgt.CreateFeedback("Customer No.", 'Good service');
end;
This keeps the page clean and delegates logic to the correct layer.
Codeunit Triggers
Codeunits support triggers such as:
• OnRun
Example: Codeunit Trigger
trigger OnRun()
begin
// Entry point logic
end;
The OnRun trigger is typically used when:
• The codeunit is executed directly
• It acts like a processing job
• It replaces a report for pure logic execution
Single-Instance Codeunits
Some codeunits are marked as single-instance, meaning their state is preserved during a session.
SingleInstance = true;These are commonly used for:
• Caching
• Session-level state
• Central coordination
They must be used carefully to avoid side effects.
Access Modifiers and Reusability
Procedures in codeunits can be:
• Public (callable from other objects)
• Local (internal use only)
This supports encapsulation and controlled exposure of logic.
Where Codeunits Fit in the Architecture
Codeunits sit at the center of business processing:
• Pages call codeunits
• Reports call codeunits
• XML Ports call codeunits
• Event subscribers often live in codeunits
They should not depend on UI objects.
Common Beginner Mistakes With Codeunits
Developers often:
• Put UI logic in codeunits
• Write very large, monolithic codeunits
• Ignore procedure visibility
• Duplicate logic instead of reusing procedures
Avoiding these mistakes leads to cleaner solutions.
Summary
Codeunits in AL define how Business Central behaves. They are the core of business logic, responsible for processing data and enforcing business rules in a reusable and maintainable way.
A well-designed codeunit:
• Has a clear responsibility
• Exposes meaningful procedures
• Avoids UI dependencies
• Supports extensibility
Understanding codeunits is essential before learning Events, Subscribers, and Interfaces.
Hot Topics in Business Central