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.
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.
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.
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.
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
000387840

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.