0

I made a scheduled class which gave me the error soql limit exeeded I following got the error too many query rows. Now I have the error:CPU time limit exceeded. How can in bulkify my class correctly

Before:

global class RenewalValueOpp  implements Schedulable{
  global void execute(SchedulableContext SC) {
    List<Opportunity> RenewalValue=[SELECT Id,Opportunity.Name, (SELECT Id FROM Orders__r) 
                                    FROM Opportunity 
                                    WHERE CreatedBy.Name='Man' AND CreatedDate=Yesterday AND Type='Renewal'];//TODAY
    system.debug('RenewalValue van Opportunity'+RenewalValue);
    for(Opportunity r : RenewalValue){
      double TotOrder = 0;

      List<Order__c> OrderAmount =[SELECT id, Order_Amount__c FROM Order__c WHERE Opportunity_Name__r.Id=:r.Id];

      System.debug('OrderAmount '+ OrderAmount);
        for(Order__c o : OrderAmount){ 
          TotOrder += o.Order_Amount__c; 
        }  
       r.Renewal_Value__c=TotOrder;
       System.debug('r.Renewal_Value__c   TotOrder'+ r.Renewal_Value__c);
       Update r;
     }
  }
}

After

global class RenewalValueOpp  implements Schedulable{
    public Opportunity r;
      global void execute(SchedulableContext SC) {

        List<Opportunity> RenewalValue=[SELECT Id,Opportunity.Name, (SELECT Id FROM Orders__r) 
                                        FROM Opportunity 
                                        WHERE CreatedBy.Name='Man' AND CreatedDate=Yesterday AND Type='Renewal'];//TODAY
          List<Order__c> OrderAmount =[SELECT id,Opportunity_Name__r.Id, Order_Amount__c FROM Order__c WHERE CreatedDate=Yesterday];      
          system.debug('RenewalValue van Opportunity'+RenewalValue);
        for(Opportunity r : RenewalValue){
          double TotOrder = 0;
          System.debug('OrderAmount '+ OrderAmount);
            for(Order__c o : OrderAmount){ 
                if(o.Opportunity_Name__r.Id==r.id){
              TotOrder += o.Order_Amount__c; 
                    }
            }  
           r.Renewal_Value__c=TotOrder;
           System.debug('r.Renewal_Value__c   TotOrder'+ r.Renewal_Value__c);

         }
          Update r;
      }
    }
0

As the relationship is master details, we can always create a roll-up summary field to get aggregate of all the values of a certain field of child records. This should be the best solution suggested.

enter image description here

Considering the scheduled update, you can go ahead with the code as below.

Map<String,double> mapOppIdToOrderAmount = new Map<String,double>();
List<Opportunity> lstOpportunity = new List<Opportunity>();

for(Order__c objOrder : [SELECT Opportunity_Name__c, Order_Amount__c 
                                 FROM Order__c WHERE CreatedDate=TODAY]){

    double dblOrderAmount = 0;
    if(mapOppIdToOrderAmount.containsKey(objOrder.Opportunity_Name__c)){

        dblOrderAmount = mapOppIdToOrderAmount.get(objOrder.Opportunity_Name__c) + objOrder.Order_Amount__c;
    }    
    dblOrderAmount += objOrder.Order_Amount__c;


    mapOppIdToOrderAmount.put(objOrder.Opportunity_Name__c,dblOrderAmount);
}

for(Opportunity objOpportunity : [SELECT Id, Renewal_Value__c
                                    FROM   Opportunity 
                                    WHERE  Id =: mapOppIdToOrderAmount.keySet()]){

    objOpportunity.Renewal_Value__c = (objOpportunity.Renewal_Value__c != null) ? objOpportunity.Renewal_Value__c : Double.valueOf(0);
    if(mapOppIdToOrderAmount.containsKey(objOpportunity.Id)){       

        objOpportunity.Renewal_Value__c += mapOppIdToOrderAmount.get(objOpportunity.Id);
        lstOpportunity.add(objOpportunity);
    }
}

if(!lstOpportunity.isEmpty()){

    update lstOpportunity;
}

Follow Apex best practices. few specifics:

  1. avoid using nested loop, instead use collections.

  2. bulkify code to handle multiple records.

The best solution will always be going with clicks over code which results in same desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.