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
OlbrestOlbrest 

How can I make SOQL querry which selecting records from xx to yy ?

Hello.

I have the salesforce object with ~ 10000 records.

 

I need to select specific range of records.

 

For example records from 5000 to 5500.

 

In MySql I use next query: [ select id from object limit 5000, 500 ]

 

But I can't found the way how I can make it in Apex SOQL.

Thank you.

Message Edited by Olbrest on 03-21-2009 06:38 AM
Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

If you don't have any specific way of identifying the range on the record itself, some special exteral id field for example, you could use a for loop to do this. 

 

 

List<mycustObj__c> rangerecs = new List<mycustObj__c>(); Integer rangecounter =0; for(mycustObj__c x : [select id from mycustObj limit 5500]){ if(rangecounter>=5000){ rangerecs.add(x); } rangecounter++; } system.debug('my list has this many records: '+rangerecs.size());

 

 This will work if you need less than 1000 in your range (since a list can only contain 1000 elements).

You could also use a variation of this counter and for loop to do things without a list as well. 

Be careful however, you will need to somehow limit your records being returned or you will hit a governor limit.

 

 

All Answers

JimRaeJimRae

If you don't have any specific way of identifying the range on the record itself, some special exteral id field for example, you could use a for loop to do this. 

 

 

List<mycustObj__c> rangerecs = new List<mycustObj__c>(); Integer rangecounter =0; for(mycustObj__c x : [select id from mycustObj limit 5500]){ if(rangecounter>=5000){ rangerecs.add(x); } rangecounter++; } system.debug('my list has this many records: '+rangerecs.size());

 

 This will work if you need less than 1000 in your range (since a list can only contain 1000 elements).

You could also use a variation of this counter and for loop to do things without a list as well. 

Be careful however, you will need to somehow limit your records being returned or you will hit a governor limit.

 

 

This was selected as the best answer
OlbrestOlbrest
Issue is solved.
Thank You.
OlbrestOlbrest

Oh... I have over than 10000 records in object.

 

How can I select records from 10001 and following ?

 

( I'm using live account )

Message Edited by Olbrest on 03-23-2009 03:34 AM
JimRaeJimRae

I think you are going to rethink your design then. Any SOQL query that returns more than 10000 records will cause an error, due to hitting the governor limit (too many query rows).

 

You will probably need to consider doing something like adding an additional field on the object record that you can use to number them and get the approporiate range of values back.

 

 

Good Luck!