function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tony2009tony2009 

Apex Web Service Pagination

Hi Guys,

 

I am asked to create a APEX web service to query the data in salesforce.com. Is it possible to paginate the query records to 10 or 20 per request?

 

I looked thought the Apex document, it seems that there is no such api, am I right?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,


You can use the OFFSET in your query. Use OFFSET to specify the starting row offset into the result set returned by your query. Using OFFSET is helpful for paging into large result sets, in scenarios where you need to quickly jump to a particular subset of the entire results. As the offset calculation is done on the server and only the result subset is returned, using OFFSET is more efficient than retrieving the full result set and then filtering the results locally.


As an example, if a SOQL query normally returned 50 rows, you could use OFFSET 10 in your query to skip the first 10 rows:
SELECT Name FROM Merchandise__c WHERE Price__c > 5.0 ORDER BY Name LIMIT 100 OFFSET 10;

 

The result set for the preceding example would be a subset of the full result set, returning rows 11 through 111 of the full set.

 

OFFSET is currently available as a Developer Preview. For more information on enabling OFFSET for your organization, contact Salesforce.com.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,


You can use the OFFSET in your query. Use OFFSET to specify the starting row offset into the result set returned by your query. Using OFFSET is helpful for paging into large result sets, in scenarios where you need to quickly jump to a particular subset of the entire results. As the offset calculation is done on the server and only the result subset is returned, using OFFSET is more efficient than retrieving the full result set and then filtering the results locally.


As an example, if a SOQL query normally returned 50 rows, you could use OFFSET 10 in your query to skip the first 10 rows:
SELECT Name FROM Merchandise__c WHERE Price__c > 5.0 ORDER BY Name LIMIT 100 OFFSET 10;

 

The result set for the preceding example would be a subset of the full result set, returning rows 11 through 111 of the full set.

 

OFFSET is currently available as a Developer Preview. For more information on enabling OFFSET for your organization, contact Salesforce.com.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
tony2009tony2009

Thanks Navatar_DbSup,

 

Perfect solution, new feature of SOQL :)