Do not call ChangeCompany inside a per-row loop
ChangeCompany retargets a record variable to another company's data and drops the in-memory caches bound to the previous company. Calling it once per row in a multi-company scan therefore pays a cache reset on every iteration, even when consecutive rows share a company.
- microsoftlayer
- performancedomain
- 2examples
- 2pull requests
Rule on GitHub · microsoft layer · performance · BC all · tier official · derived
Contributions welcome — open a PR to refine or extend this article.
Description
ChangeCompany retargets a record variable to another company’s data and drops the in-memory caches bound to the previous company. Calling it once per row in a multi-company scan therefore pays a cache reset on every iteration, even when consecutive rows share a company. Agents treat ChangeCompany like a filter. It is an isolation switch.
Best Practice
Group work by company. Call ChangeCompany once per distinct company, then FindSet/Get that company’s rows. If the record variable is reused afterward, call ChangeCompany() without a company name to redirect it back to the current company.
See sample: changecompany-in-loop-drops-caches.good.al.
Anti Pattern
repeat Rec.ChangeCompany(Buffer.Company); Rec.Get(Buffer."No."); until Buffer.Next() = 0 when Buffer is not ordered by company, or even when it is — if ChangeCompany still runs every row. The signal is ChangeCompany inside repeat/while keyed by a document line rather than by a company loop.
See sample: changecompany-in-loop-drops-caches.bad.al.
Examples
Changed by
- #174 Make BCQuality documentation easier for partners to get started, 2026-09-09
- #153 Promote knowledge for Microsoft-owned review skills, 2026-09-03
From microsoft/BCQuality at a4593a1, MIT License, Copyright (c) 2026 Microsoft Corporation (license).