bcquality/microsoft/knowledge/error-handling/fielderror-default-message-logic.md
Jesper Schulz-Wedde 186d8a1314
Some checks failed
Validate knowledge index / validate-index (push) Has been cancelled
Validate AL review fixtures / validate-review-fixtures (push) Has been cancelled
Validate frontmatter and structure / validate (push) Has been cancelled
Complete AL review knowledge readiness (#108)
* Complete AL review knowledge readiness

Fill telemetry and Query coverage, strengthen thin review domains, correct audited content defects, and add deterministic cheap-model evaluation and reference-integrity safeguards.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 9825b012-e653-496a-9310-c1f4b6f8ac27

* Generalize review fixture discovery

Derive smoke cases from the leaf, domain, and paired-sample conventions so new leaves require no scoring-contract changes. Keep only exceptional selection/context overrides and fail when retrieval metadata cannot rank the selected article.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 9825b012-e653-496a-9310-c1f4b6f8ac27

* Preserve published field IDs in sample

Keep the existing Email and Contact Email field IDs unchanged, clarify that the sample represents an independent baseline, and use a local breaking-change rule for the generic smoke evaluation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 9825b012-e653-496a-9310-c1f4b6f8ac27

* Clarify published field identity rules

State explicitly that a published field keeps its ID, name, and type while a replacement is added as a separate field under an unused ID.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 9825b012-e653-496a-9310-c1f4b6f8ac27

* Align field obsoletion sample baselines

Use Email field ID 3 as the shared baseline so the bad example demonstrates a same-ID rename while the good example retains the original field and adds a separate replacement.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 9825b012-e653-496a-9310-c1f4b6f8ac27

---------

Co-authored-by: Jesper Schulz-Wedde <jesper.schulzwedde@microsoft.com>
2026-07-15 10:55:25 +02:00

2.3 KiB

bc-version domain keywords technologies countries application-area
all
error-handling
fielderror
testfield
error-message
field-caption
lowercase-convention
record-context
validation
al
w1
all

Rely On FieldError's Auto-Generated Context And Pass Only A Lowercase Predicate

Description

Rec.FieldError(FieldNo) does not just print the text you give it. Business Central automatically prepends the field caption, appends the current field value (when non-blank), and suffixes the table name and primary-key values for record identification. The optional second argument is only the middle predicate of that sentence — e.g. "must be unique", not a whole self-contained message. Misunderstanding this leads to messages that duplicate the caption and value or read as broken grammar, because the framework's surrounding text is built to join a lowercase fragment.

Best Practice

For a plain required-field check, prefer TestField, which tests the condition and raises the error in one call. When the condition is non-trivial and has already been evaluated, call FieldError(FieldNo) with no message to get the localized default (must have a value, is not valid, etc.), or pass a short lowercase predicate such as FieldError(FieldNo, 'must be a positive number'). Start the custom text with a lowercase letter so it reads as one sentence with the auto-inserted caption, and use a field-number reference (or the field token) rather than a hard-coded field name so captions and translations stay correct. Let the framework supply the caption, value, table, and key context for you.

See sample: fielderror-default-message-logic.good.al.

Anti Pattern

Re-testing a condition you already evaluated, or passing a fully formed sentence like 'The Amount field must be positive.' to FieldError. The result reads as Amount The Amount field must be positive. in Gen. Journal Line ... — capital letter mid-sentence, caption and value repeated, and a stray trailing clause. Reviewer signals: a FieldError argument that names the field, restates the current value, starts with a capital letter, or ends with a period. Each is a sign the author treated FieldError like Error instead of as a predicate slotted into framework-generated context.

See sample: fielderror-default-message-logic.bad.al.