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
Raj R.Raj R. 

Why am I getting 'Too Many Query Row' error even when I used Limit on my SOQL query?

Hi,

I have a set of unique email address that are stored in a SET. Based on that SET of emails, I want to query all the LEads in the system with that email address. Here is an example of my code. 

Note: I have more than 50,000 leads in the system so I used query in a for loop
Set<String> email = new Set<String>();

//some logic is performed
//at the end of the logic, email has about 10,000 records

For(Lead curLead : [Select Id, Name, Email From Lead Where Email IN: email]){

//some logic performed

}//end for loop

Why would I be getting a 'Too Many query row' error if the query is in the for loop paramater list? What am I doing wrong to be hitting the 50,000 query row governor limit?

If 'email' is bigger than the total number of leads in the system, would that cause an issue? What alternative methods can I use?
Best Answer chosen by Raj R.
Cloud_forceCloud_force
you are hitting governers limit, total records that can be returned by soql should be less than 50k.

thanks,
learn apex basics at forcexplore | http://www.forcexplore.com/2014/07/get-current-record-parameters-in.html

All Answers

Cloud_forceCloud_force
you are hitting governers limit, total records that can be returned by soql should be less than 50k.

thanks,
learn apex basics at forcexplore | http://www.forcexplore.com/2014/07/get-current-record-parameters-in.html
This was selected as the best answer
Ajit KumarAjit Kumar

Hi,

You are getting this error because its violating the Governor limit.

So inspite of processing the records, one by one, you can go for batches. 

Earlier it was recommended to use 200 in a go, but now salesforce has incressed the limit to 1000.

U can go for this.

let me know, if this solution worked for ur scenario.

:)

Raj R.Raj R.
Hi Ajit,

How can I process them in batches in a schedulable apex class? 
Raj R.Raj R.
Hi sfdc_wave, 

The filter I am using is 'where email IN: email' where email is a SET 10,000 strings. The query should return at max 10,000 records or less as far as my understanding of SOQl queries. I have about 2000 leads in the system, so could it also cause an error where the filter (SET of emails in this case) is greater than the number of Leads in the system?