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
streetstreet 

How to retrieve more than 1000 records in single SOQL query?

I have an custom object   which has more than 1000 records in it. Able to retrieve only 1000 records above that it is throwing an error... How to retrieve more than 1000 records on a single soql query?

RajiRaji



 

//we can use query in for loop , to overcome governing limit.

//example 

for(Account objAccount : [select id,name from Account]){
}

 

Regards,

VNath.

 

tedwardtedward

There is a 50,000 limit on the total number of records retrieved by all SOQL queries in a transaction.

There is also a limit of 100 on the total number of SOQL queries issued.

 

These limits can be avoided, but it depends on what you are trying to do.

 

If you can post some code examples it might help.

streetstreet
public class printrecord
 {

 public PLOT__c[] o ;
  public PLOT__c[] o1 ;

public printrecord() 
{
for(PLOT__c[] acts : [select id,name from PLOT__c ])
{
    for (PLOT__c a : acts)
    {
      o.add(a);
  
    }
    
}

}
public PLOT__c[] geto() 
{
return o;
}
}

 Some thing like this....

streetstreet
public class printrecord
 {

 public Plot__c[] o ;

  
 
public printrecord() 
{

  [o= [select id,name from PLOT__c]; 
 // PLOT__c contains more than 1000 records records. so, here i need to retrieve all...how can i? 
}
public Plot__C[] geto() 
{
return o;
}
}

 

RajiRaji

Hi Street ,

 

it should work.

 

Regards,

VNath.

 

streetstreet
Hello Raji,
 
Its not working...Throwing Error.
 
An unexpected error has occurred. Your development organization has been notified.
Attempt to de-reference a null object 

 

tedwardtedward

You are trying to add to a list that hasn't yet been created.

 

Try

 

public PLOT__c[] o = new List<Plot__c>();

 

Then you can add to the list with :

 

o.add(a);

 

 

streetstreet
public class printrecord
 {
public PLOT__c[] o = new List<Plot__c>();

 

public printrecord() 
{
for(PLOT__c[] acts : [select id,name from PLOT__c ])
{
    for (PLOT__c a : acts)
    {
      o.add(a);
  
    }
    
}

}
public PLOT__c[] geto() 
{
return o;
}
}

 

Collection size 1,452 exceeds maximum size of 1,000.

 

This is the error im getting :( 

 

sfdcfoxsfdcfox

If you query more than 1000 records, and try to add them all to a single list, you'll get that error. PLOT__c[] o can't exceed 1000 items. I think they removed that in later versions of the API, so you might check your version settings (edit the class and check the versions tab).

 

Also, save yourself some script statements and use:

 

for(PLOT__c[] acts:[select id,name from PLOT__c])
  o.addAll(acts);

Of course, that's actually pretty pointless, since you could just as easily do this:

 

o = [select id,name from PLOT__C];

Then again, you should probably consider your application design; there's virtually no reason at all to load more than a couple of hundred records at once. Make sure you're filtering your queries.