AL events and integration patterns
How AL events let extensions hook into Business Central — business events, integration events, subscriber patterns, and what to avoid.
Events are the heart of AL extension architecture. They let your code run when Microsoft's code (or another extension's) reaches a defined point, without you modifying anyone else's source. Used well, they keep extensions upgrade-safe; used badly, they create invisible coupling across the codebase.
Two kinds of events. Business events are domain-level publications — "OnAfterPostSalesDocument", "OnAfterInsertCustomer" — declared by Microsoft on its standard objects. Integration events are custom publications declared by an extension when it wants to be extensible itself: an ISV's app might publish OnBeforeCalculateLineDiscount so customers can layer their own discounting rules.
Subscribers. A subscriber is a procedure in a codeunit decorated with [EventSubscriber(...)], naming the publisher object and the event. The compiler binds it at install time. When the publisher raises the event, every subscriber runs, in undefined order, in the same transaction.
OnBefore vs OnAfter. OnBefore events run before the action and can suppress or modify the operation (typically via a var IsHandled parameter). OnAfter events run after the action completes and are the right place for follow-on processing — sending an email, calling an integration, writing to a log.
Manual vs automatic binding. Subscribers can bind automatically (the default) or manually. Manual binding is for cases where the subscriber should run only when explicitly activated — for tests, or for per-tenant feature toggles. Manual subscribers are bound at runtime with BindSubscription.
Conditional subscription. A subscriber attribute can include a property filter so the subscriber only runs when a property on the publisher record matches a value — useful to scope cross-extension behaviour.
Common patterns.
- Add a field to a posted document: subscribe to
OnAfterCopy...between unposted and posted records to carry the value across. - Validate before posting: subscribe to
OnBeforePost...and raiseErrorif business rules don't pass. - Trigger an integration: subscribe to
OnAfterPost...and queue a job to call an external API.
Anti-patterns. Subscribing to dozens of OnBefore events to inject conditional behaviour. Long-running synchronous subscribers (move them to job queue entries). Subscribers that depend on the order other subscribers run in (they shouldn't). And subscribers that mutate the publisher's record after it's been written — almost always wrong.
Related guides
- AL extension architectureHow AL extensions are structured in Business Central — objects, namespaces, app.json, dependencies, and the runtime model.
- AL language and runtime evolutionHow the AL language for Business Central evolved from C/AL — runtime versions, language features added wave by wave, and what AL is becoming.
- Business Central CI/CD with AL-GoHow AL-Go for GitHub turns an AL extension repo into a build-test-deploy pipeline — secrets, environments, and continuous delivery.
- Business Central web servicesThe classic OData and SOAP web services in Business Central — how they differ from the v2.0 API, and when to use them.
- Isolated storage and secrets in ALHow AL extensions store secrets safely — IsolatedStorage scopes, Azure Key Vault integration, and the patterns for managing credentials in Business Central code.