bcquality/microsoft/knowledge/events/initialize-ishandled-to-false-before-publishing.md
Jesper Schulz-Wedde faeacb2484 Add 12 general AL event-design articles to events domain
Add 12 atomic knowledge articles under microsoft/knowledge/events covering
general AL event-design best practices: IsHandled initialization and OnAfter
preservation, appending new event parameters, position-based event naming,
reusing/extending events, avoiding per-iteration publishing, Temp-prefixing
temporary record parameters, unabbreviated parameter names, preferring the
this keyword over IncludeSender, avoiding loosely typed parameters, not
mutating existing event contracts, and not bypassing critical operations
with IsHandled. Each article ships a .good.al and .bad.al demonstration
sample (object IDs 50240-50296; not compiled by CI). Extend the
al-events-review leaf Worklist with one targeted check per new rule.

Additive only; no contract or wiring change (events leaf already wired).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-25 11:58:59 +02:00

1.8 KiB

bc-version domain keywords technologies countries application-area
all
events
ishandled
initialization
deterministic
onbefore
reset
integration-event
control-flow
al
w1
all

Initialize IsHandled to false before publishing

Description

A routine that raises an OnBefore… integration event with a var IsHandled: Boolean parameter passes that variable in by reference, so its incoming value decides whether the default logic is skipped. A freshly declared Boolean starts as false, but the same variable is frequently reused to raise several events in one routine, and after the first raise it may already be true. Assigning IsHandled := false; on the line immediately before every raise makes the control flow deterministic and self-documenting, and prevents a stale true from silently suppressing logic the author never meant to make skippable. Generated code often reuses one IsHandled across several raises without resetting it.

Best Practice

Set IsHandled := false; immediately before each OnBeforeX(…, IsHandled) raise, then guard the default logic with if IsHandled then exit; or if not IsHandled then …. Do this even when the variable was just declared: the explicit reset documents intent and stays correct if a second event raise is added to the routine later.

See sample: initialize-ishandled-to-false-before-publishing.good.al.

Anti Pattern

Raising OnBeforeX(…, IsHandled) with a variable whose value carries over from an earlier raise, so a subscriber that handled the first event unintentionally suppresses the second routine's default logic. Detection: an IsHandled variable passed to more than one event in a routine without an intervening IsHandled := false;, or any OnBefore… raise not preceded by an explicit reset.

See sample: initialize-ishandled-to-false-before-publishing.bad.al.