+ Start a Discussion

System.LimitException: Too many query rows: 50001

i'm getting this error: Too many query rows: 50001

public without sharing class OpportunityPipelineChangeProcessing {
public static Date TodaysDate = system.today();
    public static void OCP(){
         // Pull in all Open Opportunity records
        Map<Id,Opportunity> mapOpportunity= new Map<Id, Opportunity>
        ([Select Id, CurrencyIsoCode, IsClosed, StageName, CloseDate, Amount, OwnerId, OpportunityPipelineChangeId__c, OpportunityPipelineChanged__c  from Opportunity where IsClosed = False or OpportunityPipelineChanged__c = True]);
        // Pull in all prior Pipeline Change records
        Map<Id, Id> mapPC = new Map<Id, Id>();
        Set<Id>     setPC = new Set<Id>();
        for (Opportunity z: mapOpportunity.values()){
            if(z.OpportunityPipelineChangeId__c != Null){
             mapPC.put(z.OpportunityPipelineChangeId__c, z.id);
        Map<Id,OpportunityPipeline_Change__c> mapOpportunityPipeline_Change = new Map<Id, OpportunityPipeline_Change__c>
        ([Select Id, Opportunity__c, Amount_After__c, Amount_Prior__c, Close_Date_After__c, Close_Date_Prior__c, Stage_After__c, Stage_Prior__c, New_Opportunity__c from OpportunityPipeline_Change__c where Id = :setPC]);       

        // Create new OpportunityPipeline Change records
        List<OpportunityPipeline_Change__c> listOpportunityPipelineChangeAdds = new List<OpportunityPipeline_Change__c>();
        for (Opportunity z: mapOpportunity.values()){
            boolean NewOpportunity = True;
            decimal PriorAmountAfter = 0.00;
            string  PriorStageAfter = Null;
            date    PriorCloseDateAfter = Null;
            OpportunityPipeline_Change__c PC = Null;
            if(z.OpportunityPipelineChangeId__c != Null){             
                PC = mapOpportunityPipeline_Change.get(z.OpportunityPipelineChangeId__c);
                if(PC != null){
                    NewOpportunity      = False;
                    PriorAmountAfter    = PC.Amount_After__c;
                    PriorStageAfter     = PC.Stage_AFter__c;
                    PriorCloseDateAfter = PC.Close_Date_After__c;
                } else {
                    PriorAmountAfter    = z.Amount;
                    PriorStageAfter     = z.StageName;
                    PriorCloseDateAfter = z.CloseDate;
            } else {
                    PriorAmountAfter    = z.Amount;
                    PriorStageAfter     = z.StageName;
                    PriorCloseDateAfter = z.CloseDate;         
            listOpportunityPipelineChangeAdds.add(new OpportunityPipeline_Change__c(
                Amount_After__c                 = z.Amount,
                Amount_Prior__c                 = PriorAmountAfter,
                Close_Date_After__c             = z.CloseDate,
                Close_Date_Prior__c             = PriorCloseDateAfter,
                CurrencyIsoCode                 = z.CurrencyIsoCode,
                Effective_Date_Time__c          = TodaysDate,
                EffectiveDateMilliseconds__c    = System.currentTimeMillis(),
                New_Opportunity__c              = NewOpportunity,
                Opportunity__c                  = z.Id,
                Opportunity_Amount__c           = z.Amount,
                Stage_After__c                  = z.StageName,
                Stage_Prior__c                  = PriorStageAfter));
        // Update Opportunities and put in new OpportunityPipelineChange Id and reset OpportunityPipelineChanged__c value to False
        List<Opportunity> listOpportunityUpdates = new List<Opportunity>();
        for (OpportunityPipeline_Change__c z: listOpportunityPipelineChangeAdds){
            listOpportunityUpdates.add(new Opportunity(
                id                             = z.opportunity__c,
                OpportunityPipelineChangeId__c = z.id,
                OpportunityPipelineChanged__c  = false));


Hi Kumar,

Can you provide more context of what you are trying to do here? The error you are getting is because one of your SOQL is returning more than 50K records, which is the limit in synchronous processing.

You can maybe try using Batch Apex or use more selective SOQL to return less records or use LIMIT in your query.

if the number of records are more than 50000 you will get this error. You need to do record updates through batch job. more @ http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

Just an example taken from above doc.

global class batchClass implements Database.batchable{
   global Iterable start(Database.BatchableContext info){
       return new CustomAccountIterable();
   global void execute(Database.BatchableContext info, List<Account> scope){
       List<Account> accsToUpdate = new List<Account>();
       for(Account a : scope){
           a.Name = 'true';
           a.NumberOfEmployees = 70;
       update accsToUpdate;
   global void finish(Database.BatchableContext info){    

You are hitting governers limit. A SOQL can retunr only upto 50K records. Make sure you either filter your Query or put a limit in your soql query.