+ Start a Discussion
s5s5 

Error in Sheduled Batch apex programme

Hi Team,

 

I wrote one batch apex it was working fine in developer sandbox but if i move that code to Test sandbox it is showing error saying "Apex heap size too large".Please help me in this  and below is the code i wrote.

 

 

WesNolte__cWesNolte__c

Hi

 

That error means that you're using too much "memory" on the server, probably because of the amount of data you have in live. Can you write your code to not be "stateful"?

 

Wes

Rahul SharmaRahul Sharma

Hello,

 

This code can be optimized a lot, 1 major is optimazition would be:
* Use map to avoid double for loop.
    - the inner List is iterated over again and again for same records.

Ankit AroraAnkit Arora

Wesnolte is correct, also you can make collections/variables/instance null after they are used and are no longer in use.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

Does this error comes on Test Method Execution when you move to Test SandBox?

Rahul SharmaRahul Sharma

Hi, I think error id due to the highlighted code:

global class batchUpdateCPpoints implements Database.Batchable<sObject>,Database.Stateful{
    public String query='select id,name, current_year_cp_points__c from Account and recordType.DeveloperName = "Retailer" ';
    public Set<Account> accSet=new Set<Account>();
    public List<Account> updateAcc=new List<Account>();
    
    global database.querylocator start(Database.BatchableContext BC){
     return Database.getQueryLocator(query);
     }
      
  
  global void execute(Database.BatchableContext BC, List<sObject> scope){      
    List<Registration__c> allRegList =new List<Registration__c>();
  RecordType recordtype=[select name,id from RecordType where Name='Product  Record'];
  allRegList=[select id,Name,where_did_you_purchase__c,sum_of_enduse_points__c from Registration__c where recordtypeid=:recordtype.id and Createddate=TODAY]; 
   for(sObject s:scope){
       Account a=(Account)s;
       accSet.add(a);
   }   
   
   for(Account a:accSet){
       Double sum=0.0;
       System.debug('THE ACCOUNT ID INSIDE FOR LOOP******'+a);
       
       for(Registration__c r:allRegList){                     if(r.Where_did_you_Purchase__c==a.id){
                      System.debug('THE SUM OF END USE POINTS IS****** '+r.sum_of_enduse_points__c);
                if(r.sum_of_enduse_points__c!=null){           
                sum=sum+r.sum_of_enduse_points__c;
                }      
                System.debug('THE SUM INSIDE FOR LOOP IS ******'+sum);
            }
       }
      System.debug('THE ACCOUNT ID IS'+a.id);
      System.debug('SUM OF THE ACCOUNT IS '+sum);   
      if(a.current_year_cp_points__c==null) 

     a.current_year_cp_points__c =sum; 

    else  
        a.current_year_cp_points__c= a.current_year_cp_points__c + sum;
      System.debug('THE CURRENT YEAR CP POINTS ******** '+a.current_year_cp_points__c);
      updateAcc.add(a);    
   }
      
}

global void finish(Database.BatchableContext BC){
    update updateAcc;
}

}

Inner loop/list is Repeated over all the records which are not even related to outer loop.

 

 for(Account a:accSet){
Double sum=0.0;
System.debug('THE ACCOUNT ID INSIDE FOR LOOP******'+a);

for(Registration__c r:allRegList){



Which is wrong, Using a Map there could solve the problem.

Also only fetch the registrants, whose Account are present in Scope.