bcquality/microsoft/knowledge/error-handling/collect-validation-errors-with-errorbehavior.md
Copilot 8901b83e07 Add error-handling knowledge domain and review leaf skill
Seed the first new AL knowledge domain toward issue #34: a fully wired
error-handling domain that the review pipeline actually consumes.

- 3 knowledge articles (+ good/bad AL samples):
  - prefer-errorinfo-for-actionable-errors (ErrorInfo Fix-it/Show-it actions)
  - collect-validation-errors-with-errorbehavior (ErrorBehavior::Collect)
  - errortype-internal-vs-client-for-diagnostics (ErrorType Internal vs Client)
- New leaf skill al-error-handling-review.md, modeled on al-performance-review
- Wire the leaf into al-code-review (sub-skills, Source, description)
- README: six -> seven leaf skills

Validated: frontmatter validator clean; knowledge-index check passes
(deterministic, full coverage, selection inputs intact).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-23 10:35:24 +02:00

2.3 KiB

bc-version domain keywords technologies countries application-area
all
error-handling
collectible-errors
errorbehavior
collect
getcollectederrors
hascollectederrors
validation
batch
al
w1
all

Collect validation errors with ErrorBehavior::Collect and handle the collected list

Description

By default a procedure stops on the first Error, so a user fixing ten bad rows must rerun the operation ten times. The collectible-errors feature postpones error handling to the end of the call: a procedure attributed [ErrorBehavior(ErrorBehavior::Collect)] keeps running as errors occur and gathers them, so all failures can be presented together. The collected errors are read with HasCollectedErrors() and GetCollectedErrors() (which returns a List of [ErrorInfo]); ClearCollectedErrors() empties the buffer. This is a platform mechanism most LLMs are unaware of — they reach for a manually concatenated Text buffer or a temporary error table instead.

Best Practice

Mark the orchestrating procedure [ErrorBehavior(ErrorBehavior::Collect)] and run each item's validation so one failure doesn't abandon the rest — typically by calling the per-item routine through Codeunit.Run. When the run finishes, inspect HasCollectedErrors() and surface GetCollectedErrors() to the user as a single list. Always handle the collected errors yourself: the platform's own guidance is that any errors still in the collected list when the procedure ends are concatenated into one dialog, which is hard for users to read.

See sample: collect-validation-errors-with-errorbehavior.good.al.

Anti Pattern

Two shapes signal trouble. The first is hand-rolled accumulation — appending messages to a Text variable and showing them at the end — which reimplements the platform feature, loses each error's ErrorInfo structure, and skips telemetry classification. The second is applying [ErrorBehavior(ErrorBehavior::Collect)] but never calling HasCollectedErrors/GetCollectedErrors, so every collected error spills into the platform's concatenated end-of-procedure dialog. Detection: a Collect attribute with no matching GetCollectedErrors call, or a per-row loop that builds an error string by concatenation.

See sample: collect-validation-errors-with-errorbehavior.bad.al.