Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions evaluation/review-fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion microsoft/skills/review/al-events-review.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down