You need to sign in to do that
Don't have an account?
SeanCeno
Heap Size Too Large Error
Hey Everyone,
Recently a new error has come up when one of my batch classes runs causing trades from the day before not to post. This code has worked fine since implemented over 6 months ago. I've never dealt with a Heap Size error before so I'm not sure how to fix it. From what I've researched it involves having to many objects, but the code seems pretty efficient. Unless it's the test class, I don't know where the problem would be. Please take a look and let me know if you can help!
Error:
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Apex Class:
public class Account_RollupTrades {
public Static Account_Setting__c setting = Account_Setting__c.getInstance();
public Static boolean inprog = false;
public static void execute (Set<Id> accountIds, List<Account> accountsList) {
Map<Id, Account> accounts = new Map<Id, Account> (AccountsList);
system.debug ('execute');
if(setting.Disable_RollupTrades__c != true) {
//Map<Id, Account> accounts = new Map<Id, Account>();
for(Id accountId:accountIds) {
system.debug(accountid);
accounts.put(accountId,
new Account(
Id=accountId,
YTD_NIOR_I_Sales__c = 0, YTD_NIOR_I_Shares__c = 0, QTD_NIOR_I_Sales__c = 0, QTD_NIOR_I_Shares__c = 0,
MTD_NIOR_I_Sales__c = 0, MTD_NIOR_I_Shares__c = 0, PY_NIOR_I_Sales__c = 0, PY_NIOR_I_Shares__c = 0,
Total_NIOR_I_Sales__c = 0, Total_NIOR_I_Shares__c = 0, YTD_NS_Income_Sales__c = 0, YTD_NS_Income_Shares__c = 0,
QTD_NS_Income_Sales__c = 0, QTD_NS_Income_Shares__c = 0, MTD_NS_Income_Sales__c = 0, MTD_NS_Income_Shares__c = 0,
PY_NS_Income_Sales__c = 0, PY_NS_Income_Shares__c = 0, Total_NS_Income_Sales__c = 0, Total_NS_Income_Shares__c = 0,
Total_NS_HI_Sales__c = 0, Total_NS_HI_Shares__c = 0, YTD_NS_HI_Sales__c = 0, YTD_NS_HI_Shares__c = 0,
QTD_NS_HI_Sales__c = 0, QTD_NS_HI_Shares__c = 0, MTD_NS_HI_Sales__c = 0, MTD_NS_HI_Shares__c = 0,
PY_NS_HI_Sales__c = 0, PY_NS_HI_Shares__c = 0, Total_NS_Income_II_Sales__c = 0, Total_NS_Income_II_Shares__c = 0,
YTD_NS_Income_II_Sales__c = 0, YTD_NS_Income_II_Shares__c = 0, QTD_NS_Income_II_Sales__c = 0, QTD_NS_Income_II_Shares__c = 0,
MTD_NS_Income_II_Sales__c = 0, MTD_NS_Income_II_Shares__c = 0, PY_NS_Income_II_Sales__c = 0, PY_NS_Income_II_Shares__c = 0,
Rollup_Trades__c = DateTime.now()
)
);
}
// Roll up the trades based on the Resolved Firm Trading ID field
Trades__c[] tradesList = [
select Dollar_Amount_of_the_transaction__c, Fund_Number__c, Number_of_Shares_of_the_transaction__c,
Resolved_to_Rep_Trading_ID__c, Resolved_Firm_Trading_ID__c, Resolved_Firm_Trading_IDs__c,
Trade_Date__c
from Trades__c
where Resolved_Firm_Trading_ID__c in :accountIds
and Fund_Number__c in ('3910', '3911', '3912', '3915') // NIOR I; NS Income; NS HI; NS Income II
and Dollar_Amount_of_the_transaction__c != null // prevents null pointers below
and Number_of_Shares_of_the_transaction__c != null // prevents null pointers below
and Trade_Date__c != null // prevents null pointers below
// Negative values are ignored for roll-up purposes
and Dollar_Amount_of_the_transaction__c >= 0
and Number_of_Shares_of_the_transaction__c >= 0
];
Map<String, SObjectField[]>
ytd = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.YTD_NIOR_I_Sales__c , account.YTD_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.YTD_NS_Income_Sales__c , account.YTD_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.YTD_NS_HI_Sales__c , account.YTD_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.YTD_NS_Income_II_Sales__c , account.YTD_NS_Income_II_Shares__c }
},
qtd = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.QTD_NIOR_I_Sales__c , account.QTD_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.QTD_NS_Income_Sales__c , account.QTD_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.QTD_NS_HI_Sales__c , account.QTD_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.QTD_NS_Income_II_Sales__c , account.QTD_NS_Income_II_Shares__c }
},
mtd = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.MTD_NIOR_I_Sales__c , account.MTD_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.MTD_NS_Income_Sales__c , account.MTD_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.MTD_NS_HI_Sales__c , account.MTD_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.MTD_NS_Income_II_Sales__c , account.MTD_NS_Income_II_Shares__c }
},
py = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.PY_NIOR_I_Sales__c , account.PY_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.PY_NS_Income_Sales__c , account.PY_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.PY_NS_HI_Sales__c , account.PY_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.PY_NS_Income_II_Sales__c , account.PY_NS_Income_II_Shares__c }
},
total = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.Total_NIOR_I_Sales__c , account.Total_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.Total_NS_Income_Sales__c , account.Total_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.Total_NS_HI_Sales__c , account.Total_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.Total_NS_Income_II_Sales__c , account.Total_NS_Income_II_Shares__c }
};
for(trades__c trade:tradesList) {
if(date.today().year() == trade.trade_date__c.year()) {
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(ytd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(ytd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(ytd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(ytd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
if( (Decimal.ValueOf(date.today().month()).divide(3, 0) == Decimal.ValueOf(trade.trade_date__c.month()).divide(3, 0)) ) {
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(qtd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(qtd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(qtd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(qtd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
if(date.today().month()==trade.trade_date__c.month()) {
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(mtd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(mtd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(mtd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(mtd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
}
}
} else if(date.today().year()-1==trade.trade_date__c.year()) {
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(py.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(py.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(py.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(py.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
}
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(total.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(total.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(total.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(total.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
}
}
inprog = true;
update accounts.values();
inprog = false;
}
}
Apex Trigger:
trigger Account_RollupTrades on Account (after update) {
if(Account_RollupTrades.inprog == false) {
set<ID> sID = new set<ID> (trigger.newMap.keySet());
Account_RollupTrades.execute(sID, trigger.new);
}
}
Recently a new error has come up when one of my batch classes runs causing trades from the day before not to post. This code has worked fine since implemented over 6 months ago. I've never dealt with a Heap Size error before so I'm not sure how to fix it. From what I've researched it involves having to many objects, but the code seems pretty efficient. Unless it's the test class, I don't know where the problem would be. Please take a look and let me know if you can help!
Error:
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Trades_CascadeAccounts: System.LimitException: Apex heap size too large: 6220442
Apex Class:
public class Account_RollupTrades {
public Static Account_Setting__c setting = Account_Setting__c.getInstance();
public Static boolean inprog = false;
public static void execute (Set<Id> accountIds, List<Account> accountsList) {
Map<Id, Account> accounts = new Map<Id, Account> (AccountsList);
system.debug ('execute');
if(setting.Disable_RollupTrades__c != true) {
//Map<Id, Account> accounts = new Map<Id, Account>();
for(Id accountId:accountIds) {
system.debug(accountid);
accounts.put(accountId,
new Account(
Id=accountId,
YTD_NIOR_I_Sales__c = 0, YTD_NIOR_I_Shares__c = 0, QTD_NIOR_I_Sales__c = 0, QTD_NIOR_I_Shares__c = 0,
MTD_NIOR_I_Sales__c = 0, MTD_NIOR_I_Shares__c = 0, PY_NIOR_I_Sales__c = 0, PY_NIOR_I_Shares__c = 0,
Total_NIOR_I_Sales__c = 0, Total_NIOR_I_Shares__c = 0, YTD_NS_Income_Sales__c = 0, YTD_NS_Income_Shares__c = 0,
QTD_NS_Income_Sales__c = 0, QTD_NS_Income_Shares__c = 0, MTD_NS_Income_Sales__c = 0, MTD_NS_Income_Shares__c = 0,
PY_NS_Income_Sales__c = 0, PY_NS_Income_Shares__c = 0, Total_NS_Income_Sales__c = 0, Total_NS_Income_Shares__c = 0,
Total_NS_HI_Sales__c = 0, Total_NS_HI_Shares__c = 0, YTD_NS_HI_Sales__c = 0, YTD_NS_HI_Shares__c = 0,
QTD_NS_HI_Sales__c = 0, QTD_NS_HI_Shares__c = 0, MTD_NS_HI_Sales__c = 0, MTD_NS_HI_Shares__c = 0,
PY_NS_HI_Sales__c = 0, PY_NS_HI_Shares__c = 0, Total_NS_Income_II_Sales__c = 0, Total_NS_Income_II_Shares__c = 0,
YTD_NS_Income_II_Sales__c = 0, YTD_NS_Income_II_Shares__c = 0, QTD_NS_Income_II_Sales__c = 0, QTD_NS_Income_II_Shares__c = 0,
MTD_NS_Income_II_Sales__c = 0, MTD_NS_Income_II_Shares__c = 0, PY_NS_Income_II_Sales__c = 0, PY_NS_Income_II_Shares__c = 0,
Rollup_Trades__c = DateTime.now()
)
);
}
// Roll up the trades based on the Resolved Firm Trading ID field
Trades__c[] tradesList = [
select Dollar_Amount_of_the_transaction__c, Fund_Number__c, Number_of_Shares_of_the_transaction__c,
Resolved_to_Rep_Trading_ID__c, Resolved_Firm_Trading_ID__c, Resolved_Firm_Trading_IDs__c,
Trade_Date__c
from Trades__c
where Resolved_Firm_Trading_ID__c in :accountIds
and Fund_Number__c in ('3910', '3911', '3912', '3915') // NIOR I; NS Income; NS HI; NS Income II
and Dollar_Amount_of_the_transaction__c != null // prevents null pointers below
and Number_of_Shares_of_the_transaction__c != null // prevents null pointers below
and Trade_Date__c != null // prevents null pointers below
// Negative values are ignored for roll-up purposes
and Dollar_Amount_of_the_transaction__c >= 0
and Number_of_Shares_of_the_transaction__c >= 0
];
Map<String, SObjectField[]>
ytd = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.YTD_NIOR_I_Sales__c , account.YTD_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.YTD_NS_Income_Sales__c , account.YTD_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.YTD_NS_HI_Sales__c , account.YTD_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.YTD_NS_Income_II_Sales__c , account.YTD_NS_Income_II_Shares__c }
},
qtd = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.QTD_NIOR_I_Sales__c , account.QTD_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.QTD_NS_Income_Sales__c , account.QTD_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.QTD_NS_HI_Sales__c , account.QTD_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.QTD_NS_Income_II_Sales__c , account.QTD_NS_Income_II_Shares__c }
},
mtd = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.MTD_NIOR_I_Sales__c , account.MTD_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.MTD_NS_Income_Sales__c , account.MTD_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.MTD_NS_HI_Sales__c , account.MTD_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.MTD_NS_Income_II_Sales__c , account.MTD_NS_Income_II_Shares__c }
},
py = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.PY_NIOR_I_Sales__c , account.PY_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.PY_NS_Income_Sales__c , account.PY_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.PY_NS_HI_Sales__c , account.PY_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.PY_NS_Income_II_Sales__c , account.PY_NS_Income_II_Shares__c }
},
total = new map<string, sobjectfield[]> {
'3910' => new sobjectfield[] { account.Total_NIOR_I_Sales__c , account.Total_NIOR_I_Shares__c},
'3911' => new sobjectfield[] { account.Total_NS_Income_Sales__c , account.Total_NS_Income_Shares__c },
'3912' => new sobjectfield[] { account.Total_NS_HI_Sales__c , account.Total_NS_HI_Shares__c },
'3915' => new sobjectfield[] { account.Total_NS_Income_II_Sales__c , account.Total_NS_Income_II_Shares__c }
};
for(trades__c trade:tradesList) {
if(date.today().year() == trade.trade_date__c.year()) {
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(ytd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(ytd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(ytd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(ytd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
if( (Decimal.ValueOf(date.today().month()).divide(3, 0) == Decimal.ValueOf(trade.trade_date__c.month()).divide(3, 0)) ) {
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(qtd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(qtd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(qtd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(qtd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
if(date.today().month()==trade.trade_date__c.month()) {
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(mtd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(mtd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(mtd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(mtd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
}
}
} else if(date.today().year()-1==trade.trade_date__c.year()) {
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(py.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(py.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(py.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(py.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
}
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(total.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(total.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
accounts.get(trade.Resolved_Firm_Trading_ID__c).put(total.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(total.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
}
}
inprog = true;
update accounts.values();
inprog = false;
}
}
Apex Trigger:
trigger Account_RollupTrades on Account (after update) {
if(Account_RollupTrades.inprog == false) {
set<ID> sID = new set<ID> (trigger.newMap.keySet());
Account_RollupTrades.execute(sID, trigger.new);
}
}
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);
}
}
}
All Answers
public class Trades_CascadeAccounts {
public Trades__c[] tradesOldList { set; get; }
public Trades__c[] tradesNewList { set; get; }
public Map<Id, Trades__c> tradesOldListMap { set; get; }
public Trades_CascadeAccounts(Trades__c[] tradesOldList, Trades__c[] tradesNewList) {
this.tradesNewList = tradesNewList == null ? new Trades__c[0] : tradesNewList.clone();
this.tradesOldList = tradesOldList == null ? new Trades__c[0] : tradesOldList.clone();
this.tradesOldListMap = new Map<Id, Trades__c>(this.tradesOldList);
}
public void execute() {
Set<Id> tradesAccountIds = new Set<Id> {};
for(Trades__c tradesNew : tradesNewList) {
Trades__c tradesOld = tradesOldListMap.containsKey(tradesNew.Id) ? tradesOldListMap.get(tradesNew.Id) : new Trades__c();
tradesAccountIds.addAll(new Set<Id> { tradesOld.Resolved_Firm_Trading_ID__c, tradesNew.Resolved_Firm_Trading_ID__c });
}
tradesAccountIds.remove(null);
Account[] accountList = new Account[0];
for(Id accountId : tradesAccountIds) {
accountList.add(new Account(Id=AccountId,Rollup_Trades__c=null));
}
update accountList;
System.debug('Total Number of script statements allowed in this apex code context: ' + Limits.getLimitScriptStatements());
}
}
I work for a fund raising company also, we ran into this sort of problem about a year ago and moved this sort of aggragetion to batchs rather than having them run in a trigger.
So let's say I wanted to run a batch for a single firm that usually has the most trades every day (LPL). How would one go about including that firm in one batch and not another? Or how do I manage the heap size on my existing batch?
You can also split the batch into 2 seperate runs, change the the batch's query to get only accounts with > xxx number of trades, runs then run the same again with <= xxx in the query.
global class Trades_CascadeAccountsBatchable implements Database.Batchable<sObject>, Database.Stateful {
global Database.QueryLocator start(Database.BatchableContext batchableContext){
return Database.getQueryLocator('select Id from Account where ParentId = null');
}
global void execute(Database.BatchableContext batchableContext, Account[] accountList) {
for(Account account : accountList)
account.Rollup_Trades__c = null;
Database.executeBatch(account.Rollup_Trades__c = null, 50);
update accountList;
}
global void finish(Database.BatchableContext batchableContext) {
}
}
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);
}
}
}
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 Total_Sales__c > 75000000','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) {
for(Account account : accountList)
account.Rollup_Trades__c = null;
update accountList;
}
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 Total_Sales__c <= 75000000',null), 200);
}
}
}
"Expecting a right paranthesis, found 'new'."
ID idBatch = Database.executeBatch(......