Decouple extension behavior with events
The service codeunit provides a direct dependency for required processing. Events add a different relationship: the publisher announces that something occurred without depending on the objects that react.
Distinguish triggers and event types
A trigger is a runtime entry point on an object, field, action, or control. Database triggers such as OnInsert and OnModify, page triggers, report triggers, and the codeunit OnRun trigger run in response to their defined runtime conditions. Many platform and application triggers also expose trigger events that subscribers can handle.
Published AL events express intent beyond the raw trigger:
- A business event represents a meaningful business occurrence, such as an order release. Treat its signature as a stable contract for integrations.
- An integration event supports extension behavior inside the application and can expose implementation-oriented context.
- An internal event is available only within the extension that declares it.
- A trigger event is published by the runtime around table and page trigger execution.
Choose a business event for a durable business contract and an integration event for an application extension point. Don't publish both when one clear contract serves the need.
Match publisher and subscriber contracts
An event publisher is a procedure marked with an event attribute. Calling that procedure raises the event. The subscriber names the object type, publisher object, event name, and optional element name.
codeunit 70502 "Entry Process Service"
{
procedure Process(var Entry: Record "Processing Entry")
begin
Entry.Status := Entry.Status::Completed;
Entry.Modify(true);
OnAfterEntryProcessed(Entry);
end;
[IntegrationEvent(false, false)]
local procedure OnAfterEntryProcessed(var Entry: Record "Processing Entry")
begin
end;
}
codeunit 70503 "Entry Event Subscriber"
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Entry Process Service", 'OnAfterEntryProcessed', '', false, false)]
local procedure RecordProcessingNote(var Entry: Record "Processing Entry")
begin
Entry."Event Note" := 'Subscriber observed processing';
Entry.Modify(true);
end;
}
The subscriber parameters must match the publisher signature. Keep the contract small. Every added parameter becomes part of the coupling between publisher and subscribers.
Reason about order and transactions
Never depend on subscriber order. Business Central doesn't guarantee the order in which subscribers to the same event run. If one operation must happen before another, coordinate those operations explicitly through a procedure instead of separate subscribers.
For a normal event, publisher and subscribers run in the same transaction. An unhandled subscriber error stops later processing and rolls back the transaction. A subscriber can also change var parameters, so use those parameters only when mutation is an intentional part of the event contract.
An event marked Isolated = true runs each subscriber in its own transaction. The publisher must commit an existing write transaction before raising an isolated event. If an isolated subscriber fails, only changes to normal tables in that subscriber transaction rollback. Changes to variables, external HTTP effects, and other nontransactional effects don't roll back. Isolation is disabled while install or upgrade code runs, so don't use isolated events as a failure boundary for lifecycle operations.
Isolation allows other isolated subscribers to continue after one fails, but it isn't a substitute for idempotent subscriber design. A subscriber can run more than once because of retries or repeated business operations.
Choose an extension relationship
Use a direct call when the caller requires one known operation and needs its result. Use an event when zero or more independent subscribers react and the publisher doesn't select them. Use an interface when the caller selects one implementation of a contract. The next unit applies that third relationship to the processing strategy.
Learn more in Events in AL, Event types, and Subscribe to events.