bcquality/microsoft/knowledge/performance/changecompany-in-loop-drops-caches.md
Jesper Schulz-Wedde 2b5550c346
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
Improve partner onboarding and documentation navigation (#174)
Lead with a complete plugin quick start and add task-oriented usage, troubleshooting, customization, and contribution guides. Preserve the broader plugin framing, correct conflicting contract guidance, support Agents folder reviews, and align repository validation. Convert existing sample references to clickable links without changing knowledge rules.

Co-authored-by: Jesper Schulz-Wedde <jesper.schulzwedde@microsoft.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
2026-09-09 17:31:03 +02:00

1.4 KiB

bc-version domain keywords technologies countries application-area
all
performance
changecompany
loop
cache
multi-company
isolation
al
w1
all

Do not call ChangeCompany inside a per-row loop

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.