Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I know SOQL can only retrieve data.

Now my question is, WITHOUT WRITING APEX, given that a query have 4 results. I know I can filter the results to 1 by limiting row and pretty sure this could be the 1st record.

Can I have a syntax that will filter down the results for the query to show the 2nd record or 3rd?

Appreciate your response on this.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You can use the offset clause for this - this will skip the specified number of rows in the result. If you also supply a limit 1, this will be applied after the offset and return you the single row you desire. Looking at the debug logs, offset applies a default ordering by id, so you may need to supply your own ordering.

The following code first pulls back 10 accounts ordered by id, then the third and fourth in the list:

List<Account> accs=[select id, name from account order by id limit 10];
System.debug('Accs = ' + accs);
accs=[select id, name from account limit 1 offset 2];
System.debug('Accs = ' + accs);
accs=[select id, name from account limit 1 offset 3];
System.debug('Accs = ' + accs);

Anonymous execution was successful.

08:32:39.024 (24324207)|USER_DEBUG|[2]|DEBUG|Accs = (Account:{Name=GenePoint, Id=0018000000Q9v1QAAR}, Account:{Name=United Oil & Gas, UK, Id=0018000000Q9v1RAAR}, Account:{Name=United Oil & Gas, Singapore, Id=0018000000Q9v1SAAR}, Account:{Name=Edge Communications, Id=0018000000Q9v1TAAR}, Account:{Name=Burlington Textiles Corp of America, Id=0018000000Q9v1UAAR}, Account:{Name=Pyramid Construction Inc., Id=0018000000Q9v1VAAR}, Account:{Name=Dickenson plc, Id=0018000000Q9v1WAAR}, Account:{Name=Grand Hotels & Resorts Ltd, Id=0018000000Q9v1XAAR}, Account:{Name=Express Logistics and Transport, Id=0018000000Q9v1YAAR}, Account:{Name=University of Arizona, Id=0018000000Q9v1ZAAR})

08:32:39.027 (27277422)|USER_DEBUG|[4]|DEBUG|Accs = (Account:{Name=United Oil & Gas, Singapore, Id=0018000000Q9v1SAAR})

08:32:39.029 (29455227)|USER_DEBUG|[6]|DEBUG|Accs = (Account:{Name=Edge Communications, Id=0018000000Q9v1TAAR})

share|improve this answer
    
Thanks a Lot Bob ! You the Man ! ^_^ If its not much, what is the order of query by default? –  JHONG Aug 4 at 7:53
    
Its undefined - salesforce.stackexchange.com/questions/7667/… –  Bob Buzzard Aug 4 at 9:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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