Stop Writing Slow Loops: Master the FindSet Pattern
Continuing our AL performance series (we’ve covered Dictionary caching and SetLoadFields), today I want to spotlight a common pitfall I see in almost every code review: inefficient record iteration.
The Problem
Processing more than 8,000 records. Developers tend to use Find(‘-‘) with repeat-until Next() = 0. Takes a longer processing time. Users complaining, timeout errors in production.
Root Cause
Find(‘-‘) doesn’t lock the result set. Every Next() call re-queries the database. With over 8,000 records, that’s 8,000+ database round trips.
The Fix
Use FindSet(false) for read-only operations. It locks the result set and loads records in bulk, drastically reducing DB hits and improving performance.
When to Use What
* FindSet(false) – Read-only operations, reporting, exports
* FindSet(true) – When you need to modify records in the loop
* Get() – Single record by primary key (fastest)
* FindFirst() – Only need the first matching record
* Find(‘-‘) – Avoid (legacy pattern)
