0

I have the following schedulerd class wich given me an error: Too many query rows: 50001.

Class:

global class RenewalValueOpp  implements Schedulable{
    global void execute(SchedulableContext SC) {
        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]){
                                     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() AND CreatedDate=TODAY]){
                                              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;
        }
    }
}

I assume this would be solved by using a bach apex

I followed the trailhead but get the error: Missing return statement required return type: Database.QueryLocator. How do I make this work

global class RenewalValueOpp implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext bc) {
        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 Journal_Code__c != 'KLP' AND
                                 End_Date__c != NULL]){
                                     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() AND CreatedDate=TODAY AND Type = 'Renewal' AND StageName = 'DMU Check']){
                                              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;
        } 
    }

    global void execute(Database.BatchableContext bc, List<Opportunity> 200){

    }    

    global void finish(Database.BatchableContext bc){

    }    

}

2 Answers 2

1

Your class is not designed properly. You are doing the main processes in the start method, while you should only Query the records in the start method.

Why? The batch works to process large data in batches. Start method is executed synchronously, i.e. as soon as you run/execute the batch class, start method gets processed, but the execute method runs in batches, or asynchronously (of size you specify).

e.g. Database.executeBatch(new testBatch([parameter]), 200); Read more on the above here.

The error you are having clearly telling you that you are not returning anything, as you should be returning of type Database.QueryLocator. Just create a SOQL of your records and return it in end of start method and then you should process those records in the execute method of your batch class.

0

You want to move the actual work into the execute method. Use the start method to Query the records that you want to work with and return the Database.QueryLocator object. The start method finds the records u want to work with and passes batches of the records to the execute method. you are trying to do everything in the start method when u should only be trying to find the records that you want to work with.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .