Loading

Workaround for the 2000-Row OFFSET Limit on Salesforce SOQL Queries

게시 일자: Jun 19, 2026
상세 설명

What Is the SOQL OFFSET 2000-Row Limit?

When using SOQL pagination with the OFFSET keyword, you may encounter this error: "The maximum offset is 2,000 rows. Requesting an offset greater than 2,000 will result in a NUMBER_OUTSIDE_VALID_RANGE error."
This limit is hard-coded in Salesforce and cannot be increased. However, several workarounds exist to paginate through large data sets without using OFFSET.

솔루션

Option 1: Use QueryMore for Full Dataset Retrieval

QueryMore is the recommended approach when retrieving an entire data set at once. QueryMore uses a server-side cursor, allowing you to retrieve all records without hitting the OFFSET limit.

Option 2: Paginate Using Filter-Based Queries

For paginated UIs (such as a website or portal), sort records by a high-cardinality field — a field with many unique values such as CreatedDate or Id — and use filters to navigate between pages. This eliminates the need for OFFSET entirely.


Page 1: Retrieve the first 2,000 records ordered by CreatedDate:
SELECT Id, Name, CreatedDate FROM Account ORDER BY CreatedDate LIMIT 2000
Take the CreatedDate of the last returned record and use it as the filter anchor for the next page.

Page 2 (forward): Use the anchor value as a filter:
SELECT Id, Name, CreatedDate FROM Account WHERE CreatedDate > 'Last-Returned-Created-Date' ORDER BY CreatedDate LIMIT 2000

Going back one page: Reverse the order and results:
SELECT Id, Name, CreatedDate FROM Account WHERE CreatedDate < 'First-Returned-Created-Date' ORDER BY CreatedDate DESC LIMIT 2000
Note: You cannot jump directly to an arbitrary offset (such as the 50,000th record). You must step through pages sequentially.

Option 3: Use Client-Side Caching with Periodic Refresh

For large, frequently accessed data sets, consider client-side caching with a periodic refresh using the SOAP API getUpdated() and getDeleted() replication methods. This reduces repeated large queries.

Option 4: Custom Apex Web Service for Large Offsets

A custom Apex web service can step through records in batches of up to 50,000 rows per call, using the filter-based pagination approach described above. This brings total efficiency to approximately 1 API call per 50,000 rows plus one final query for the page result. Additional resources: OFFSET in SOQL

Knowledge 기사 번호

000387840

 
로드 중
Salesforce Help | Article