You need to sign in to do that
Don't have an account?
SeanCeno
Changing class to Dynamic Apex
Hey I'm trying to correct some old code that is hitting governor limits for when we try and post our trades to SF. The last class we changed to Dynamic Apex and it seems to work fine, so I was hoping someone could help me do it again as I'm not experienced enough yet.
Error:
Trades_CascadeAccounts: System.LimitException: Too many code statements: 200001
Main Class:
public class Account_RollupTrades { public Account[] accountOldList { set; get; } public Account[] accountNewList { set; get; } public Account_Setting__c setting { set; get; } public Account_RollupTrades(Account[] accountOldList, Account[] accountNewList) { this.accountOldList = accountOldList == null ? new Account[] {} : accountOldList.clone(); this.accountNewList = accountNewList == null ? new Account[] {} : accountNewList.clone(); this.setting = Account_Setting__c.getInstance(); this.setting = setting == null ? new Account_Setting__c() : setting; } public void execute() { if (setting.Disable_RollupTrades__c == true) return; Account[] accountUpdateableList = new Account[] {}; for(Account accountNew : accountNewList) { if (accountNew == null || account.Id == null) continue; if (accountNew.Rollup_Trades__c != null) continue; accountUpdateableList.add(accountNew); } execute(accountUpdateableList); } public void execute(Account[] accountList) { if (accountList == null || accountList.size() == 0) return; // Reset accounts Map<Id, Account> accountListMap = new Map<Id, Account>(accountList); for(Account account : accountList) { account.Total_NIOR_I_Sales__c = 0; account.Total_NIOR_I_Shares__c = 0; account.YTD_NIOR_I_Sales__c = 0; account.YTD_NIOR_I_Shares__c = 0; account.QTD_NIOR_I_Sales__c = 0; account.QTD_NIOR_I_Shares__c = 0; account.MTD_NIOR_I_Sales__c = 0; account.MTD_NIOR_I_Shares__c = 0; account.PY_NIOR_I_Sales__c = 0; account.PY_NIOR_I_Shares__c = 0; account.Total_NS_REIT_Sales__c = 0; account.Total_NS_REIT_Shares__c = 0; account.YTD_NS_REIT_Sales__c = 0; account.YTD_NS_REIT_Shares__c = 0; account.QTD_NS_REIT_Sales__c = 0; account.QTD_NS_REIT_Shares__c = 0; account.MTD_NS_REIT_Sales__c = 0; account.MTD_NS_REIT_Shares__c = 0; account.PY_NS_REIT_Sales__c = 0; account.PY_NS_REIT_Shares__c = 0; account.Total_NS_HIT_Sales__c = 0; account.Total_NS_HIT_Shares__c = 0; account.YTD_NS_HIT_Sales__c = 0; account.YTD_NS_HIT_Shares__c = 0; account.QTD_NS_HIT_Sales__c = 0; account.QTD_NS_HIT_Shares__c = 0; account.MTD_NS_HIT_Sales__c = 0; account.MTD_NS_HIT_Shares__c = 0; account.PY_NS_HIT_Sales__c = 0; account.PY_NS_HIT_Shares__c = 0; account.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_Firm_Trading_ID__c , Trade_Date__c from Trades__c where Resolved_Firm_Trading_ID__c in :accountList and Fund_Number__c in ('3910', '3911', '3912') // NIOR I; NS REIT; NS HIT 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 ]; // Nothing to do? if (tradesList.size() == 0) return; // Update accounts for(Trades__c trades : tradesList) { Account account = accountListMap.get(trades.Resolved_Firm_Trading_ID__c); if (account == null || trades == null || trades.Trade_Date__c == null) continue; else if ('3910'.equals(trades.Fund_Number__c)) processFund_3910(account, trades); else if ('3911'.equals(trades.Fund_Number__c)) processFund_3911(account, trades); else if ('3912'.equals(trades.Fund_Number__c)) processFund_3912(account, trades); } } void processFund_3910(Account account, Trades__c trades) { system.assert(null != account); system.assert(null != trades); system.assert(null != trades.Trade_Date__c); Boolean isYTD = (date.today().year() == trades.Trade_Date__c.year()); Boolean isMTD = (date.today().month() == trades.Trade_Date__c.month()) && isYTD; Boolean isQTD = (((date.today().month() - 1) / 3) + 1) == (((trades.Trade_Date__c.month() - 1) / 3) + 1) && isYTD; Boolean isPY = ((date.today().year() - 1) == trades.Trade_Date__c.year()); Boolean isTotal = true; // YTD if (isYTD) { account.YTD_NIOR_I_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.YTD_NIOR_I_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // QTD if (isQTD) { account.QTD_NIOR_I_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.QTD_NIOR_I_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // MTD if (isMTD) { account.MTD_NIOR_I_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.MTD_NIOR_I_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // if (isPY) { account.PY_NIOR_I_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.PY_NIOR_I_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // if (isTotal) { account.Total_NIOR_I_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.Total_NIOR_I_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } } void processFund_3911(Account account, Trades__c trades) { system.assert(null != account); system.assert(null != trades); system.assert(null != trades.Trade_Date__c); Boolean isYTD = (date.today().year() == trades.Trade_Date__c.year()); Boolean isMTD = (date.today().month() == trades.Trade_Date__c.month()) && isYTD; Boolean isQTD = (((date.today().month() - 1) / 3) + 1) == (((trades.Trade_Date__c.month() - 1) / 3) + 1) && isYTD; Boolean isPY = ((date.today().year() - 1) == trades.Trade_Date__c.year()); Boolean isTotal = true; // YTD if (isYTD) { account.YTD_NS_REIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.YTD_NS_REIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // QTD if (isQTD) { account.QTD_NS_REIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.QTD_NS_REIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // MTD if (isMTD) { account.MTD_NS_REIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.MTD_NS_REIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // if (isPY) { account.PY_NS_REIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.PY_NS_REIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // if (isTotal) { account.Total_NS_REIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.Total_NS_REIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } } void processFund_3912(Account account, Trades__c trades) { system.assert(null != account); system.assert(null != trades); system.assert(null != trades.Trade_Date__c); Boolean isYTD = (date.today().year() == trades.Trade_Date__c.year()); Boolean isMTD = (date.today().month() == trades.Trade_Date__c.month()) && isYTD; Boolean isQTD = (((date.today().month() - 1) / 3) + 1) == (((trades.Trade_Date__c.month() - 1) / 3) + 1) && isYTD; Boolean isPY = ((date.today().year() - 1) == trades.Trade_Date__c.year()); Boolean isTotal = true; // YTD if (isYTD) { account.YTD_NS_HIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.YTD_NS_HIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // QTD if (isQTD) { account.QTD_NS_HIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.QTD_NS_HIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // MTD if (isMTD) { account.MTD_NS_HIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.MTD_NS_HIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // if (isPY) { account.PY_NS_HIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.PY_NS_HIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } // if (isTotal) { account.Total_NS_HIT_Sales__c += trades.Dollar_Amount_of_the_transaction__c; account.Total_NS_HIT_Shares__c += trades.Number_of_Shares_of_the_transaction__c; } } }
Besides batch apex, one approach is to deal with this part of your code:
and convert to something like this:
and then operate on acctWorkList afterwards. (You'll need to declare this as a list variable - exercise for the reader; you will also need to create your map based on workAcctList). If this class is used in a trigger and is implicitly updating the Account in Trigger.new, then you'll need to use explicit DML statements as workAcctList is not Trigger.new
But, you save about 25 or so script statements per Account. However, I suspect the problem is the combination of trade volume and accountList size. If the latter can be reduced per invocation (see batch apex suggestion), then you have a scalable solution
All Answers
Look at this link with tips arount script statements..
http://www.tgerm.com/2010/02/apex-script-statements-reduction-tips.html
Besides batch apex, one approach is to deal with this part of your code:
and convert to something like this:
and then operate on acctWorkList afterwards. (You'll need to declare this as a list variable - exercise for the reader; you will also need to create your map based on workAcctList). If this class is used in a trigger and is implicitly updating the Account in Trigger.new, then you'll need to use explicit DML statements as workAcctList is not Trigger.new
But, you save about 25 or so script statements per Account. However, I suspect the problem is the combination of trade volume and accountList size. If the latter can be reduced per invocation (see batch apex suggestion), then you have a scalable solution