From 5cc149811db896915869efe2cca47206031ffd7b Mon Sep 17 00:00:00 2001 From: Fer Date: Sun, 29 Nov 2026 10:57:36 +0100 Subject: [PATCH] Document external business event transaction behavior Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- evaluation/review-fixtures.json | 10 ++++- ...nts-require-commit-for-notification.bad.al | 45 +++++++++++++++++++ ...ts-require-commit-for-notification.good.al | 40 +++++++++++++++++ ...-events-require-commit-for-notification.md | 36 +++++++++++++++ microsoft/skills/review/al-events-review.md | 2 +- 5 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 microsoft/knowledge/events/external-business-events-require-commit-for-notification.bad.al create mode 100644 microsoft/knowledge/events/external-business-events-require-commit-for-notification.good.al create mode 100644 microsoft/knowledge/events/external-business-events-require-commit-for-notification.md diff --git a/evaluation/review-fixtures.json b/evaluation/review-fixtures.json index cfcf6be8..9f2f570e 100644 --- a/evaluation/review-fixtures.json +++ b/evaluation/review-fixtures.json @@ -79,8 +79,14 @@ "database-trigger-setup-flags-may-only-be-set-to-true", "isolated-events-are-isolated-only-outside-a-write-transaction", "isolated-event-rollback-does-not-undo-var-parameters", - "avoid-raising-events-inside-try-functions" - ] + "avoid-raising-events-inside-try-functions", + "external-business-events-require-commit-for-notification" + ], + "articleOverrides": { + "external-business-events-require-commit-for-notification": { + "context": "Target Business Central 22, worldwide. The external system is subscribed to the delivery events." + } + } }, "finance": { "articles": [ diff --git a/microsoft/knowledge/events/external-business-events-require-commit-for-notification.bad.al b/microsoft/knowledge/events/external-business-events-require-commit-for-notification.bad.al new file mode 100644 index 00000000..39240582 --- /dev/null +++ b/microsoft/knowledge/events/external-business-events-require-commit-for-notification.bad.al @@ -0,0 +1,45 @@ +enumextension 50180 "Delivery Event Category" extends EventCategory +{ + value(50180; "Delivery Demo") + { + Caption = 'Delivery Demo'; + } +} + +codeunit 50180 "Delivery Processing" +{ + procedure CompleteDelivery(var Delivery: Record "Delivery Demo") + begin + Delivery.Processed := true; + Delivery.Modify(true); + if Delivery."Failure Reason" <> '' then begin + DeliveryFailed(Delivery."No.", Delivery."Failure Reason"); + Error(FailureReportedErr, Delivery."No."); + end; + end; + + [ExternalBusinessEvent('deliveryFailed', 'Delivery failed', 'A delivery failed.', EventCategory::"Delivery Demo")] + procedure DeliveryFailed(DeliveryNo: Code[20]; Reason: Text[100]) + begin + end; + + var + FailureReportedErr: Label 'Delivery %1 failed and the external system has been notified.', Comment = '%1 = delivery number'; +} + +table 50180 "Delivery Demo" +{ + DataClassification = CustomerContent; + + fields + { + field(1; "No."; Code[20]) { } + field(2; Processed; Boolean) { } + field(3; "Failure Reason"; Text[100]) { } + } + + keys + { + key(PK; "No.") { Clustered = true; } + } +} diff --git a/microsoft/knowledge/events/external-business-events-require-commit-for-notification.good.al b/microsoft/knowledge/events/external-business-events-require-commit-for-notification.good.al new file mode 100644 index 00000000..a01c6b47 --- /dev/null +++ b/microsoft/knowledge/events/external-business-events-require-commit-for-notification.good.al @@ -0,0 +1,40 @@ +enumextension 50180 "Delivery Event Category" extends EventCategory +{ + value(50180; "Delivery Demo") + { + Caption = 'Delivery Demo'; + } +} + +codeunit 50180 "Delivery Processing" +{ + procedure CompleteDelivery(var Delivery: Record "Delivery Demo") + begin + Delivery.TestField("Failure Reason", ''); + Delivery.Processed := true; + Delivery.Modify(true); + DeliveryCompleted(Delivery."No."); + end; + + [ExternalBusinessEvent('deliveryCompleted', 'Delivery completed', 'A delivery was completed.', EventCategory::"Delivery Demo")] + procedure DeliveryCompleted(DeliveryNo: Code[20]) + begin + end; +} + +table 50180 "Delivery Demo" +{ + DataClassification = CustomerContent; + + fields + { + field(1; "No."; Code[20]) { } + field(2; Processed; Boolean) { } + field(3; "Failure Reason"; Text[100]) { } + } + + keys + { + key(PK; "No.") { Clustered = true; } + } +} diff --git a/microsoft/knowledge/events/external-business-events-require-commit-for-notification.md b/microsoft/knowledge/events/external-business-events-require-commit-for-notification.md new file mode 100644 index 00000000..e0d984e7 --- /dev/null +++ b/microsoft/knowledge/events/external-business-events-require-commit-for-notification.md @@ -0,0 +1,36 @@ +--- +bc-version: [22..] +domain: events +keywords: [externalbusinessevent, business-event, transaction, commit, rollback, notification] +technologies: [al] +countries: [w1] +application-area: [all] +--- + +# External business event notifications require a committed transaction + +## Description + +Notifications for an `ExternalBusinessEvent` are sent only after the current transaction commits. If that transaction rolls back, the notification is not sent, even if the event was raised before the error. This differs from reasoning about AL `BusinessEvent` or `IntegrationEvent` subscribers. + +The attribute is available from runtime 11.0 (BC 22); Microsoft's business events overview describes the feature as preview. The samples use BC 22 syntax; on BC 23 and later, import `System.Integration` for `EventCategory`. + +## Best Practice + +Use external business events to notify consumers of business outcomes whose transaction can commit. A raise within that transaction is valid: it does not itself commit, and notification waits for the transaction to commit. Do not flag such a raise merely because it precedes the commit. + +If a failed operation must be reported externally, design failure reporting so it does not depend on a notification raised in the transaction being rolled back. Do not add an unconditional `Commit()` solely to force a notification; preserve the operation's required atomicity. + +See sample: [`external-business-events-require-commit-for-notification.good.al`](external-business-events-require-commit-for-notification.good.al). + +## Anti Pattern + +Raise an external business event as a failure notification and then roll back the same transaction, while relying on the notification to reach the external consumer. The rollback discards the notification, so the consumer is not notified of that failure. + +See sample: [`external-business-events-require-commit-for-notification.bad.al`](external-business-events-require-commit-for-notification.bad.al). + +## References + +- [Business events overview: receive notifications](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/business-events-overview#receive-notifications-of-business-events) +- [ExternalBusinessEvent attribute](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/attributes/devenv-externalbusinessevent-attribute) +- [AL runtime versions](https://learn.microsoft.com/dynamics365/business-central/dev-itpro/developer/devenv-choosing-runtime) diff --git a/microsoft/skills/review/al-events-review.md b/microsoft/skills/review/al-events-review.md index 972e322f..a6f03b37 100644 --- a/microsoft/skills/review/al-events-review.md +++ b/microsoft/skills/review/al-events-review.md @@ -39,7 +39,7 @@ Narrow the relevant files to the subset that applies to the changes under review - The changed AL object names and types — especially codeunits that publish events or host event subscribers, posting/release/validation routines that should expose extension points, and test codeunits that bind subscribers. - The changed procedures and triggers, weighted toward event publisher methods, methods carrying the `[EventSubscriber(...)]` attribute, routines that raise `OnBefore`/`OnAfter` events, and any procedure that calls `BindSubscription`/`UnbindSubscription`. -- Tokens extracted from the diff that relate to events and the publish/subscribe model (`IntegrationEvent`, `BusinessEvent`, `InternalEvent`, `EventSubscriber`, `IsHandled`, `BindSubscription`, `UnbindSubscription`, `EventSubscriberInstance`, `OnBefore`, `OnAfter`, `Manual`, `IncludeSender`, `GlobalVarAccess`, `Isolated`, `local`, `internal`, `Sender`, `this`, `RecordRef`, `xRec`, `temporary`, `Temp`, `repeat`, `ChangeCompany`, `StartSession`, `RunTrigger`, `GetDatabaseTableTriggerSetup`, `OnAfterGetDatabaseTableTriggerSetup`, `Commit`, `GlobalTriggerManagement`, `Global Triggers`, `OnDatabaseInsert`, `OnDatabaseModify`, `OnDatabaseDelete`, `OnDatabaseRename`). +- Tokens extracted from the diff that relate to events and the publish/subscribe model (`IntegrationEvent`, `BusinessEvent`, `ExternalBusinessEvent`, `InternalEvent`, `EventSubscriber`, `IsHandled`, `BindSubscription`, `UnbindSubscription`, `EventSubscriberInstance`, `OnBefore`, `OnAfter`, `Manual`, `IncludeSender`, `GlobalVarAccess`, `Isolated`, `local`, `internal`, `Sender`, `this`, `RecordRef`, `xRec`, `temporary`, `Temp`, `repeat`, `ChangeCompany`, `StartSession`, `RunTrigger`, `GetDatabaseTableTriggerSetup`, `OnAfterGetDatabaseTableTriggerSetup`, `Commit`, `GlobalTriggerManagement`, `Global Triggers`, `OnDatabaseInsert`, `OnDatabaseModify`, `OnDatabaseDelete`, `OnDatabaseRename`). A file enters the candidate worklist when its `keywords` intersect the extracted tokens or its topic (derived from the index entry's `path`, `title`, and `description`) matches a changed object type. Read an article's full file — its `## Best Practice` / `## Anti Pattern` bodies — only after it makes the worklist; candidate selection uses the index alone.