Set Query filters before Open
Query.SetFilter and Query.SetRange automatically close an open query dataset. A call placed after Open() therefore does not refine the rows already being read; it ends that dataset.
- microsoftlayer
- querydomain
- 2examples
- 2pull requests
Rule on GitHub · microsoft layer · query · BC all · tier official · derived
Description
Query.SetFilter and Query.SetRange automatically close an open query dataset. A call placed after Open() therefore does not refine the rows already being read; it ends that dataset. The next Read() has no open dataset unless the code explicitly calls Open() again, so a plausible filter change can turn a working loop into an empty or failing read sequence without a compiler diagnostic.
Best Practice
Apply every filter before Open(), then read the dataset to completion and call Close(). When a later branch needs different filters, close or clear the query, set the new filters, and open a new dataset deliberately.
See sample: set-query-filters-before-open.good.al.
Anti Pattern
Query.Open() followed by SetFilter or SetRange and then Read() under the assumption that the filter updates the open cursor. Refiltering after Open() is valid only when the code intentionally opens a fresh dataset afterward.
See sample: set-query-filters-before-open.bad.al.
Examples
Changed by
- #174 Make BCQuality documentation easier for partners to get started, 2026-09-09
- #108 Complete AL review knowledge readiness, 2026-07-15
From microsoft/BCQuality at a4593a1, MIT License, Copyright (c) 2026 Microsoft Corporation (license).