[SalesForce] How to query records

In Account object I have 1000 records.i need to query first 100 records,next i need to query after 101 records.how it is possible.my first query is like that

[select id,name from Account LIMIt 100];

i want query like this[select id,name from Account LIMIT 101];

Best Answer

Use OFFSET

When expecting many records in a query’s results, you can display the results in multiple pages by using the OFFSET clause on a SOQL query. For example, you can use OFFSET to display records 51 to 75 and then jump to displaying records 301 to 350. Using OFFSET is an efficient way to handle large results sets.

Use OFFSET to specify the starting row offset into the result set returned by your query. Because 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. OFFSET is available in API version 24.0 and later.

[select id,name from Account LIMIT 100 OFFSET 100 ];

this will return all the records except starting 100 records

you need to query like [select id,name from Account OFFSET 100 LIMIT 100];

each time you need to increase OFFSET value like to get the first 100 use offset = 0 and limit 100, this will return first 1-100 records.

2nd time set OFFSET 100 and limit 100 this will return 101 to 200 records.

3rd time set OFFSET 200 and limit 100 this will return 201 to 300 records ...

this way you can use till 2000 records but after that you can't because OFFSET have limit of 2K. We can use more than 2k value in OFFSET

Related Topic