You need to sign in to do that
Don't have an account?
Desirae Slaugh 9
New to Org & Development, I have an existing Trigger blowing out CPU Limits and I need help understanding what it's doing
Hello everyone, I am new to coding and I recently inherited an org with an AccountTrigger that is blowing out my CPU Limits. I could use some assistance understanding exactly what it is doing, as the existing purpose comments don't really provide too many insights.
Trigger Name : AccountTrigger
Purpose : Contains all actions performed by Account Trigger.
trigger AccountTrigger on Account (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
accountTriggerHandler handler = new accountTriggerHandler();
/* Before Insert */
if(Trigger.isInsert && Trigger.isBefore) {
//handler.fillOppAmount(Trigger.new, null);
}
/* After Insert */
else if(Trigger.isInsert && Trigger.isAfter) {
//handler.createOpportunity(Trigger.new, null);
}
/* Before Update */
else if(Trigger.isUpdate && Trigger.isBefore) {
//handler.fillOppAmount(Trigger.new, Trigger.oldMap);
}
/* After Update */
else if(Trigger.isUpdate && Trigger.isAfter) {
//handler.createOpportunity(Trigger.new, Trigger.oldMap);
//handler.updateOpportunity(Trigger.new, Trigger.oldMap);
}
}
Trigger Name : AccountTrigger
Purpose : Contains all actions performed by Account Trigger.
trigger AccountTrigger on Account (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
accountTriggerHandler handler = new accountTriggerHandler();
/* Before Insert */
if(Trigger.isInsert && Trigger.isBefore) {
//handler.fillOppAmount(Trigger.new, null);
}
/* After Insert */
else if(Trigger.isInsert && Trigger.isAfter) {
//handler.createOpportunity(Trigger.new, null);
}
/* Before Update */
else if(Trigger.isUpdate && Trigger.isBefore) {
//handler.fillOppAmount(Trigger.new, Trigger.oldMap);
}
/* After Update */
else if(Trigger.isUpdate && Trigger.isAfter) {
//handler.createOpportunity(Trigger.new, Trigger.oldMap);
//handler.updateOpportunity(Trigger.new, Trigger.oldMap);
}
}
Hi Desirae,
you would have too verify your accountTriggerHandler class, if contains a list try to implement them as map if possible.
https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1
Hope this is helpful!
Cheers,
Akshay
Can you please provide the code in accountTriggerHandler class. This will be helpful in finding the issue. Cheers!
/*************************************************************************************
Class Name : accountTriggerHandler
Purpose : Contains account trigger methods.
***************************************************************************************/
public with sharing class accountTriggerHandler {
public static set<Id> fillOppAmountHasRun = new set<Id>();
public static set<Id>createOpportunityHasRun = new set<Id>();
public static set<Id>updateOpportunityHasRun = new set<Id>();
/******************************************************
* Constructor Method
*******************************************************/
public accountTriggerHandler() {}
/******************************************************
* Method Name : fillOppAmount
* Description : Populate the Opportunity_Amount__c field if the Account_Plan__c field is not blank
* Return Type : void
* Parameters : list<Account> newAccounts
*******************************************************/
/*
public void fillOppAmount(list<Account> newAccounts, map<Id, Account> oldAccounts) {
map<string, string> accountPlanToPrice = new map<string, string>();
for (Account a : newAccounts) {
// For inserted account
if (oldAccounts == null && a.Account_Plan__c != null) {
accountPlanToPrice.put(a.Account_Plan__c, null);
}
// For updated account
else if (oldAccounts != null && a.Account_Plan__c != null && a.Account_Plan__c != oldAccounts.get(a.id).Account_Plan__c && !fillOppAmountHasRun.contains(a.Id)) {
accountPlanToPrice.put(a.Account_Plan__c, null);
fillOppAmountHasRun.add(a.id);
}
// For updated account: Set opp amount to null if Account Plan is changed to blank
else if (oldAccounts != null && a.Account_Plan__c == null && a.Account_Plan__c != oldAccounts.get(a.id).Account_Plan__c && a.Opportunity_Amount__c != null) {
a.Opportunity_Amount__c = null;
}
}
if (!accountPlanToPrice.isEmpty()) {
list<Account_Plan_Price_List__mdt> accountPlanPriceList = [SELECT MasterLabel, Price__c FROM Account_Plan_Price_List__mdt WHERE MasterLabel IN :accountPlanToPrice.keyset()];
for (Account_Plan_Price_List__mdt appl : accountPlanPriceList) {
if (accountPlanToPrice.get(appl.MasterLabel) == null) {
accountPlanToPrice.put(appl.MasterLabel, appl.Price__c);
}
}
for (Account a : newAccounts) {
if (a.Account_Plan__c != null && accountPlanToPrice.get(a.Account_Plan__c) != null) {
a.Opportunity_Amount__c = Decimal.valueOf(accountPlanToPrice.get(a.Account_Plan__c));
}
}
}
}
*/
/******************************************************
* Method Name : createOpportunity
* Description : Create an opportunity record for AC account
* Return Type : void
* Parameters : list<Account> newAccounts, map<Id, Account> oldAccounts
*******************************************************/
/*
public void createOpportunity(list<Account> newAccounts, map<Id, Account> oldAccounts) {
list<Opportunity> oppToInsert = new list<Opportunity>();
for (Account a : newAccounts) {
// for updated account
if (oldAccounts != null ) {
// for AC Account that has its Opportunity Stage set to Closed Won for the first time
if (a.AC__c && a.Opportunity_Stage__c == '7. Closed Won' && (oldAccounts.get(a.id).Opportunity_Stage__c == null || !oldAccounts.get(a.id).Opportunity_Stage__c.equals('7. Closed Won')) && a.Account_Plan__c != null && a.Invoice_Date__c != null && !createOpportunityHasRun.contains(a.Id)) {
Opportunity opp = new Opportunity();
opp.Name = a.Name;
opp.AccountId = a.Id;
opp.Account_Plan__c = a.Account_Plan__c;
opp.StageName = a.Opportunity_Stage__c;
opp.CloseDate = a.Invoice_Date__c;
opp.LeadSource = 'CallRail App';
oppToInsert.add(opp);
createOpportunityHasRun.add(a.Id);
}
}
// for inserted account
else {
if (a.AC__c && a.Opportunity_Stage__c == '7. Closed Won' && a.Account_Plan__c != null && a.Invoice_Date__c != null && !createOpportunityHasRun.contains(a.Id)) {
Opportunity opp = new Opportunity();
opp.Name = a.Name;
opp.AccountId = a.Id;
opp.Account_Plan__c = a.Account_Plan__c;
opp.StageName = a.Opportunity_Stage__c;
opp.CloseDate = a.Invoice_Date__c;
opp.LeadSource = 'CallRail App';
oppToInsert.add(opp);
createOpportunityHasRun.add(a.Id);
}
}
}
if (!oppToInsert.isEmpty()) {
insert oppToInsert;
}
}
*/
/******************************************************
* Method Name : updateOpportunity
* Description : Update an opportunity record for AC account
* Return Type : void
* Parameters : list<Account> newAccounts, map<Id, Account> oldAccounts
*******************************************************/
/*
public void updateOpportunity(list<Account> newAccounts, map<Id, Account> oldAccounts) {
map<Id, Account> accountIdToAccount = new map<Id, Account>();
list<Opportunity> oppToInsert = new list<Opportunity>();
list<Opportunity> oppToUpdate = new list<Opportunity>();
for (Account a : newAccounts) {
// for accounts that already have an active opportunity associated with them and the Account Plan has changed.
if (oldAccounts.get(a.id).Account_Plan__c != null && a.Account_Plan__c != oldAccounts.get(a.id).Account_Plan__c && a.Opportunity_Stage__c == '7. Closed Won' && oldAccounts.get(a.id).Opportunity_Stage__c != null && oldAccounts.get(a.id).Opportunity_Stage__c.equals('7. Closed Won') && !updateOpportunityHasRun.contains(a.Id)) {
accountIdToAccount.put(a.Id, a);
updateOpportunityHasRun.add(a.Id);
}
else if (a.Churned_Date__c != null && oldAccounts.get(a.id).Churned_Date__c == null && !updateOpportunityHasRun.contains(a.Id)) {
accountIdToAccount.put(a.Id, a);
updateOpportunityHasRun.add(a.Id);
}
}
if (!accountIdToAccount.isEmpty()) {
for (Opportunity opp : [select Id, Name, AccountId, Account_Plan__c, Active__c, StageName, CloseDate, Last_Invoice_Date__c, LeadSource from Opportunity where AccountId IN :accountIdToAccount.keySet() and Active__c = true]) {
if (accountIdToAccount.get(opp.AccountId) != null) {
Account a = accountIdToAccount.get(opp.AccountId);
// Churn Date is not blank. Set exisitng active opp to inactive.
if (a.Churned_Date__c != null && opp.Active__c) {
opp.Active__c = false;
oppToUpdate.add(opp);
}
// Plan changed.
else if (a.Opportunity_Stage__c == '7. Closed Won' && opp.Account_Plan__c != a.Account_Plan__c) {
// Create a new opportunity record for the new plan
Opportunity newOpp = new Opportunity();
newOpp = opp.clone(false, true, false, false);
newOpp.Account_Plan__c = a.Account_Plan__c;
newOpp.CloseDate = System.today();
oppToInsert.add(newOpp);
// Set existing Opportunity to inactive
opp.Active__c = false;
oppToUpdate.add(opp);
}
}
}
}
if (!oppToUpdate.isEmpty()) {
update oppToUpdate;
}
if (!oppToInsert.isEmpty()) {
insert oppToInsert;
}
}
*/
}