2

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];

  • why would you want to make multiple queries? It will count towards your governor limits. Isn't it better to just query them all at once and then split it in apex if you really need it? – Novarg Jun 29 '16 at 7:08
2

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

  • This not a vf page....just i need to query first 100 records..after that 101-200 – Hemalatha paruchuri Jun 29 '16 at 6:47
  • @Hemalathaparuchuri yeah 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. second 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 – Ratan Paul Jun 29 '16 at 6:51
  • @Ratan Why don't you update this in your answer. – Mr.Frodo Jun 29 '16 at 6:54
  • First Limit must come and then Offset @Ratan, Otherwise it won't query the records... – Subhash Jun 29 '16 at 6:55
  • @Ratan select id,name from Account OFFSET 50 LIMIT 100 iquery like this but it shows error(Unknown error parsing query) – Hemalatha paruchuri Jun 29 '16 at 7:02
1

You can use the Queries with Offset and Limit

Syntax to use Limit with Offset to retrieve records is following:

SELECT fieldList FROM objectType

[WHERE conditionExpression]

ORDER BY fieldOrderByList

LIMIT numberOfRowsToReturn

OFFSET numberOfRowsToSkip

If you want to query the First 100 Records give the query like below.

Select ID, Name from Account LIMIT 100 OFFSET 0

If you want to query the next 100 Records give the query like below.

Select ID, Name from Account LIMIT 100 OFFSET 100

Hope this might helps...

  • In second SOQL Offset will be 100 to start from 101. – Mr.Frodo Jun 29 '16 at 6:54
  • 1
    I have added my 2 cents to update the post a littile bit.Hope you don't mind. – Mr.Frodo Jun 29 '16 at 7:04
  • Never mind @Mr.Frodo, And ORDER BY is good for filtering too... – Subhash Jun 29 '16 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.