Define per-record page outputs on every control-flow path
A global page variable belongs to the page instance, whereas Rec changes as OnAfterGetRecord processes records. Page variables are initialized when the page is loaded; do not assume that each retrieved record clears them.
- microsoftlayer
- uidomain
- 2examples
- 1pull request
Rule on GitHub · microsoft layer · ui · BC all · tier official · derived
Description
A global page variable belongs to the page instance, whereas Rec changes as OnAfterGetRecord processes records. Page variables are initialized when the page is loaded; do not assume that each retrieved record clears them. If an output is assigned only after a successful lookup or in one branch, a later record processed by that instance can retain an earlier record’s value. This affects per-record fields in list and part pages and in API page output; it is not a claim that values persist between independent requests or sessions.
Best Practice
Define every per-record output for the current record on every path that returns normally from OnAfterGetRecord. Reset those outputs before any conditional computation or early exit, or assign them in every applicable branch. A helper that initializes all outputs, an unconditional assignment or a function returning a value for every path is equally valid; an explicit Clear is not mandatory. Preserve intentional page-wide state, caches and accumulators. Do not clear Rec or call ClearAll as a substitute for initializing the outputs.
Trace helper calls and var parameters before reporting an unassigned path. Keep current-selection action state separate from per-record field values; the distinction between OnAfterGetCurrRecord and OnAfterGetRecord is covered by the existing page-lifecycle guidance.
See sample: reset-per-row-page-variables-in-onaftergetrecord.good.al. It includes both explicit clearing and complete conditional assignment.
Anti Pattern
A field is bound to a global page variable, and a normal path through OnAfterGetRecord leaves that variable unchanged even though it describes the current record. Require evidence of both a prior value and a reachable path that can expose it for another record. An if Lookup.Get(...) then Output := ... without a default or complete alternative is a typical signal. The absence of Clear alone is not a defect. CodeCop AA0205 checks unassigned-variable usage; passing that check is not proof that a conditional global output has been defined for the current record.
See sample: reset-per-row-page-variables-in-onaftergetrecord.bad.al.
References
- OnInit initializes page variables when the page is loaded
- OnAfterGetRecord runs for each fetched record and Rec is that record
- Microsoft API page clears AppName before a conditional assignment
- Microsoft API page clears Status and ErrorMessage for each record
- Microsoft ListPart assigns StyleTxt in both branches of its SetStyle helper
- CodeCop AA0205
Examples
- Good: reset-per-row-page-variables-in-onaftergetrecord.good.al
- Bad: reset-per-row-page-variables-in-onaftergetrecord.bad.al
Names
- System.ClearAll() Method (names ClearAll)
Changed by
From microsoft/BCQuality at aaaf6c1, MIT License, Copyright (c) 2026 Microsoft Corporation (license).