Encapsulate business logic in codeunits
The processing extension starts with a service boundary that owns one business operation. A cohesive codeunit makes that operation reusable without tying it to a page, report, or background session.
Define a cohesive responsibility
A codeunit contains AL procedures and triggers but doesn't store records as an object. Give each codeunit a responsibility that you can state as one outcome, such as processing an entry or calculating a routing value. If the description contains unrelated outcomes, split the object so that callers don't acquire dependencies they don't need.
Access modifiers express the intended boundary:
- A
publicprocedure is available to other objects that can reference the codeunit. - An
internalprocedure is available inside the same extension but not to dependent extensions. - A
localprocedure supports implementation details inside the object.
Use the narrowest access that supports the design. This approach keeps implementation helpers from becoming accidental contracts.
codeunit 70502 "Entry Process Service"
{
procedure Process(var Entry: Record "Processing Entry")
begin
ValidateEntry(Entry);
Entry.Status := Entry.Status::Completed;
Entry.Modify(true);
end;
internal procedure CanProcess(Entry: Record "Processing Entry"): Boolean
begin
exit(Entry.Status = Entry.Status::Ready);
end;
local procedure ValidateEntry(Entry: Record "Processing Entry")
begin
Entry.TestField(Status, Entry.Status::Ready);
end;
}
Choose an invocation pattern
A direct procedure call communicates a required dependency and lets errors propagate normally. You can declare a codeunit variable and call its public or internal procedure, or use a static-style call when the language permits it. Pass records by var only when the procedure intentionally changes the caller's variable.
var
ProcessService: Codeunit "Entry Process Service";
ProcessService.Process(Entry);
The TableNo property gives a codeunit an implicit Rec variable of the specified record type. The runtime supplies that record when a caller invokes the codeunit through Codeunit.Run with a record. The codeunit's OnRun trigger is the entry point.
codeunit 70509 "Entry Task Runner"
{
TableNo = "Processing Entry";
trigger OnRun()
var
ProcessService: Codeunit "Entry Process Service";
begin
ProcessService.Process(Rec);
end;
}
Use Codeunit.Run when the codeunit object itself is the operation boundary. Its behavior depends on whether the caller consumes the Boolean return value:
Succeeded := Codeunit.Run(Codeunit::"Entry Task Runner", Entry);is a conditional run. On success, the runtime commits the codeunit's database changes at the end. If the caller already has an active write transaction, the caller must callCommit()before the conditional run.Codeunit.Run(Codeunit::"Entry Task Runner", Entry);raises a runtime error when the codeunit fails because the caller doesn't consume the return value.
A conditional run isn't a try method, and it doesn't roll back an active transaction owned by the caller. Use a try method only when its documented restrictions and error-handling semantics match the operation.
Control codeunit state
Ordinary codeunit variables receive their own object instances. Avoid storing business state in global variables when a procedure parameter or persisted record makes the state explicit.
SingleInstance = true changes that lifecycle. All variables for that codeunit in the same client session and company share one instance, and its global state remains for the lifetime of that company connection. The state isn't shared across sessions or companies, and a new session starts with new state. Use this property for session-scoped coordination or caching, never as shared durable storage.
These boundaries prepare the service for looser extension points. The next unit separates optional reactions from the core processing call by using events.
Learn more in Codeunit object and Codeunit.Run method.