Home SALESFORCEAPEX How to fix the heap Size issue in Batch Classes

something like this, assuming updating an account is what triggers your code above.

global class Trades_CascadeAccountsBatchable implements Database.Batchable<sObject>, Database.Stateful {

/*run this batch the first time with this
Database.executeBatch(new Trades_CascadeAccountsBatchable(‘SELECT id FROM Account WHERE Number_Of_Trades > XXXX’,’Trades_CascadeAccountsBatchable run 2′), 1);
*/

global string query;
global string nextBatch;

global Trades_CascadeAccountsBatchable(string q, string nb){

//what is the criteria for running this on an account?
if(q != null)query = q;
else query = ‘select Id from Account where ParentId = null’;

//for the second run
nextBatch = nb;

}

global Database.QueryLocator start(Database.BatchableContext batchableContext){
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext batchableContext, Account[] accountList) {

account.Rollup_Trades__c = null;

update accountList;
//does this update make the rest of your code run?
}

global void finish(Database.BatchableContext batchableContext) {

//if nextBatch is ‘Trades_CascadeAccountsBatchable run 2’ then run it
if(nextBatch == ‘Trades_CascadeAccountsBatchable run 2’){
//put in here the query to pull the rest of the accounts, with lower number of trades
Database.executeBatch(new Trades_CascadeAccountsBatchable(‘SELECT id FROM Account WHERE Number_Of_Trades <= XXXX’,null), 200);
}

}
}

You may also like

Leave a Comment