Problem: I am using SOQL query to retrieve more than 50000 records inside start method and passing those records to query locator. I am getting this error :Too many query rows: 50001 in batch class.
Batch
global Database.QueryLocator start(Database.BatchableContext BC) {
String oppQuery = 'SELECT Id FROM Opportunity WHERE StageName NOT in (\'Duplicate\',\'Closed Lost\',\'Cancelled\',\'Prospect\',\'Active - Low Probability\',\'Active - Med. Probability\') ';
List<Opportunity> Opp = new List<Opportunity>();
System.debug('oppQuery '+oppQuery);
Opp = database.query(oppQuery);
List<Id> oppIds = new List<Id>();
for(Opportunity o : Opp)
{
oppIds.add(o.ID);
}
String query = 'SELECT id FROM OpportunityLineItem WHERE (NOT(Product2.Name like \'%ECO%\')) AND Primary__c = true AND OpportunityId IN: oppIds';
System.debug('query query '+query );
return Database.getQueryLocator(query);
}
Error Line:
String oppQuery = 'SELECT Id FROM Opportunity WHERE StageName NOT in (\'Duplicate\',\'Closed Lost\',\'Cancelled\',\'Prospect\',\'Active - Low Probability\',\'Active - Med. Probability\') ';
How can I fix this error?