You need to sign in to do that
Don't have an account?
Ti Saunders 8
Apex CPU Time Limit Exceeded - unexpected exception
Our org has a trigger that will update contact fields to corresponding account fields when changed. We are getting a lot of CPU time limit exceeded emails for some reason.
Here is what we have for the trigger:
Here is what we have for the trigger:
AccountAfterUpdate on Account (after update) { list<Account> accounts = new list<Account>(); // Loop through Accounts to gather modified fields for (Account acct : Trigger.New) { Account oldAcct = Trigger.oldMap.get(acct.Id); /* i_Account_Type i_PSA/CRM i_RMM i_ET_Customer i_ET_Exp_Date i_NA_Customer i_NA_Exp_Date i_NA_LastRun_Date i_NA_Purch_Date i_NA_Report_Count i_PW_Customer i_PW_Exp_Date i_SA_Customer i_SA_Exp_Date i_SA_LastRun_Date i_SA_Purch_Date i_SA_Report_Count i_TD_Customer i_TD_Exp_Date i_BDR_RunCount_1y__c i_BDR_RunCount_30d__c i_BDR_RunCount_90d__c i_DetectorSDS_NumDeployed__c i_DetectorSDS_Purch_Date__c i_EA_RunCount_1y__c i_EA_RunCount_30d__c i_EA_RunCount_90d__c i_HIPAA_RunCount_1y__c i_HIPAA_RunCount_30d__c i_HIPAA_RunCount_90d__c i_NA_RunCount_1y__c i_NA_RunCount_30d__c i_NA_RunCount_90d__c i_PCI_RunCount_1y__c i_PCI_RunCount_30d__c i_PCI_RunCount_90d__c i_Reporter_Units__c i_SA_RunCount_1y__c i_SA_RunCount_30d__c i_SA_RunCount_90d__c i_SQL_RunCount_1y__c i_SQL_RunCount_30d__c i_SQL_RunCount_90d__c i_Type_of_IT__c i_ET_Price__c i_TD_Price__c i_SA_Price__c i_PW_Price__c i_NA_Price__c i_EA_Customer__c i_EA_Exp_Date__c i_EA_LastRun_Date__c i_EA_Purch_Date__c i_EA_Report_Count__c i_EA_Price__c i_ND_Module_Count__c i_CC_Exp_Date__c i_ET_Purch_Date__c i_Affiliations__c i_SQL_Customer__c i_SQL_Exp_Date__c i_SQL_LastRun_Date__c i_SQL_Price__c i_SQL_Purch_Date__c i_SQL_Report_Count__c i__CNCT_Customer__c i__CNCT_Purch_Date__c i__CNCT_Exp_Date__c i__CNCT_Price__c i_HIPAA_Customer__c i_HIPAA_Exp_Date__c i_HIPAA_Price__c i_HIPAA_Purch_Date__c i_INSPECTOR_Customer__c i_INSPECTOR_Units__c Affiliation_ASCII__c Affiliation_CharTec__c Affiliation_CompTIA__c Affiliation_HTG__c Affiliation_RR__c Business_Type__c, FMIT_ID__c, i_HIPAA_Report_Count__c, i_HIPAA_LastRun_Date__c, i_NDS_HC_Customer__c, i_NDS_HC_Exp_Date__c, i_NDS_HC_Purch_Date__c, i_NDS_HC_Price__c, i_NDS_HC_Report_Count__c, i_NDS_HC_LastRun_Date__c i_PCI_Customer__c, i_PCI_Exp_Date__c, i_PCI_LastRun_Date__c, i_PCI_Price__c, i_PCI_Purch_Date__c, i_PCI_Report_Count__c, i_MSA_Customer__c, i_MSA_Exp_Date__c, i_MSA_LastRun_Date__c, i_MSA_Price__c, i_MSA_Purch_Date__c, i_MSA_Report_Count__c i_DETECTOR_Customer__c, i_DETECTOR_Units__c, i_REPORTER_Customer__c, i_REPORTER_Units__c, Sales_Rep__c, i_OEM_Continuum__c i_BDR_Customer_c__c, i_BDR_Exp_Date_c__c, i_BDR_LastRun_Date_c__c, i_BDR_Price_c__c, i_BDR_Purch_Date_c__c, i_BDR_Report_Count_c__c i_DetectorSDS_Customer__c, i_DetectorSDS_Exp_Date__c, i_DetectorSDS_LastActivation_Date__c, i_DetectorSDS_NumDeployed__c, i_DetectorSDS_Price__c, i_DetectorSDS_Purch_Date__c */ if (acct.i_Account_Type__c != oldAcct.i_Account_Type__c || acct.i_PSA_CRM__c != oldAcct.i_PSA_CRM__c || acct.i_RMM__c != oldAcct.i_RMM__c || acct.i_ET_Customer__c != oldAcct.i_ET_Customer__c || acct.i_ET_Exp_Date__c != oldAcct.i_ET_Exp_Date__c || acct.i_NA_Customer__c != oldAcct.i_NA_Customer__c || acct.i_NA_Exp_Date__c != oldAcct.i_NA_Exp_Date__c || acct.i_NA_LastRun_Date__c != oldAcct.i_NA_LastRun_Date__c || acct.i_NA_Purch_Date__c != oldAcct.i_NA_Purch_Date__c || acct.i_NA_Report_Count__c != oldAcct.i_NA_Report_Count__c || acct.i_PW_Customer__c != oldAcct.i_PW_Customer__c || acct.i_PW_Exp_Date__c != oldAcct.i_PW_Exp_Date__c || acct.i_SA_Customer__c != oldAcct.i_SA_Customer__c || acct.i_SA_Exp_Date__c != oldAcct.i_SA_Exp_Date__c || acct.i_SA_LastRun_Date__c != oldAcct.i_SA_LastRun_Date__c || acct.i_SA_Purch_Date__c != oldAcct.i_SA_Purch_Date__c || acct.i_SA_Report_Count__c != oldAcct.i_SA_Report_Count__c || acct.i_Type_of_IT__c != oldAcct.i_Type_of_IT__c || acct.i_ET_Price__c != oldAcct.i_ET_Price__c || acct.i_SA_Price__c != oldAcct.i_SA_Price__c || acct.i_PW_Price__c != oldAcct.i_PW_Price__c || acct.i_NA_Price__c != oldAcct.i_NA_Price__c || acct.i_EA_Customer__c != oldAcct.i_EA_Customer__c || acct.i_EA_Exp_Date__c != oldAcct.i_EA_Exp_Date__c || acct.i_EA_LastRun_Date__c != oldAcct.i_EA_LastRun_Date__c || acct.i_EA_Purch_Date__c != oldAcct.i_EA_Purch_Date__c || acct.i_EA_Report_Count__c != oldAcct.i_EA_Report_Count__c || acct.i_EA_Price__c != oldAcct.i_EA_Price__c || acct.i_ND_Module_Count__c != oldAcct.i_ND_Module_Count__c || acct.i_CC_Exp_Date__c != oldAcct.i_CC_Exp_Date__c || acct.i_ET_Purch_Date__c != oldAcct.i_ET_Purch_Date__c || acct.i_Affiliations__c != oldAcct.i_Affiliations__c || acct.i_Refresh__c != oldAcct.i_Refresh__c || acct.i_SQL_Customer__c != oldAcct.i_SQL_Customer__c || acct.i_SQL_Exp_Date__c != oldAcct.i_SQL_Exp_Date__c || acct.i_SQL_LastRun_Date__c != oldAcct.i_SQL_LastRun_Date__c || acct.i_SQL_Price__c != oldAcct.i_SQL_Price__c || acct.i_SQL_Purch_Date__c != oldAcct.i_SQL_Purch_Date__c || acct.i_SQL_Report_Count__c != oldAcct.i_SQL_Report_Count__c || acct.i_CNCT_Customer__c != oldAcct.i_CNCT_Customer__c || acct.i_CNCT_Purch_Date__c != oldAcct.i_CNCT_Purch_Date__c || acct.i_CNCT_Exp_Date__c != oldAcct.i_CNCT_Exp_Date__c || acct.i_CNCT_Price__c != oldAcct.i_CNCT_Price__c || acct.i_HIPAA_Customer__c != oldAcct.i_HIPAA_Customer__c || acct.i_HIPAA_Exp_Date__c != oldAcct.i_HIPAA_Exp_Date__c || acct.i_HIPAA_Price__c != oldAcct.i_HIPAA_Price__c || acct.i_HIPAA_Purch_Date__c != oldAcct.i_HIPAA_Purch_Date__c || acct.i_INSPECTOR_Customer__c != oldAcct.i_INSPECTOR_Customer__c || acct.i_INSPECTOR_Units__c != oldAcct.i_INSPECTOR_Units__c || acct.Affiliation_ASCII__c != oldAcct.Affiliation_ASCII__c || acct.Affiliation_CharTec__c != oldAcct.Affiliation_CharTec__c || acct.Affiliation_CompTIA__c != oldAcct.Affiliation_CompTIA__c || acct.Affiliation_HTG__c != oldAcct.Affiliation_HTG__c || acct.Affiliation_RR__c != oldAcct.Affiliation_RR__c || acct.Business_Type__c != oldAcct.Business_Type__c || acct.FMIT_ID__c != oldAcct.FMIT_ID__c || acct.i_HIPAA_Report_Count__c != oldAcct.i_HIPAA_Report_Count__c || acct.i_HIPAA_LastRun_Date__c != oldAcct.i_HIPAA_LastRun_Date__c || acct.i_PCI_Customer__c != oldAcct.i_PCI_Customer__c || acct.i_PCI_Exp_Date__c != oldAcct.i_PCI_Exp_Date__c || acct.i_PCI_LastRun_Date__c != oldAcct.i_PCI_LastRun_Date__c || acct.i_PCI_Price__c != oldAcct.i_PCI_Price__c || acct.i_PCI_Purch_Date__c != oldAcct.i_PCI_Purch_Date__c || acct.i_PCI_Report_Count__c != oldAcct.i_PCI_Report_Count__c || acct.i_DETECTOR_Customer__c != oldAcct.i_DETECTOR_Customer__c || acct.i_DETECTOR_Units__c != oldAcct.i_DETECTOR_Units__c || acct.i_REPORTER_Customer__c != oldAcct.i_REPORTER_Customer__c || acct.i_REPORTER_Units__c != oldAcct.i_REPORTER_Units__c || acct.Sales_Rep__c != oldAcct.Sales_Rep__c || acct.i_OEM_Continuum__c != oldAcct.i_OEM_Continuum__c || acct.i_BDR_Customer_c__c != oldAcct.i_BDR_Customer_c__c || acct.i_BDR_Exp_Date_c__c != oldAcct.i_BDR_Exp_Date_c__c || acct.i_BDR_LastRun_Date_c__c != oldAcct.i_BDR_LastRun_Date_c__c || acct.i_BDR_Price_c__c != oldAcct.i_BDR_Price_c__c || acct.i_BDR_Purch_Date_c__c != oldAcct.i_BDR_Purch_Date_c__c || acct.i_BDR_Report_Count_c__c != oldAcct.i_BDR_Report_Count_c__c || acct.i_DetectorSDS_Customer__c != oldAcct.i_DetectorSDS_Customer__c || acct.i_DetectorSDS_Exp_Date__c != oldAcct.i_DetectorSDS_Exp_Date__c || acct.i_DetectorSDS_LastActivation_Date__c != oldAcct.i_DetectorSDS_LastActivation_Date__c || acct.i_DetectorSDS_NumDeployed__c != oldAcct.i_DetectorSDS_NumDeployed__c || acct.i_DetectorSDS_Price__c != oldAcct.i_DetectorSDS_Price__c || acct.i_DetectorSDS_Purch_Date__c != oldAcct.i_DetectorSDS_Purch_Date__c || acct.i_CyberHawk_ABDS_Sites__c != oldacct.i_CyberHawk_ABDS_Sites__c || acct.i_CyberHawk_BDS_Sites__c != oldacct.i_CyberHawk_BDS_Sites__c || acct.i_BDR_RunCount_1y__c != oldacct.i_BDR_RunCount_1y__c || acct.i_BDR_RunCount_30d__c != oldacct.i_BDR_RunCount_30d__c || acct.i_BDR_RunCount_90d__c != oldacct.i_BDR_RunCount_90d__c || acct.i_EA_RunCount_1y__c != oldacct.i_EA_RunCount_1y__c || acct.i_EA_RunCount_30d__c != oldacct.i_EA_RunCount_30d__c || acct.i_EA_RunCount_90d__c != oldacct.i_EA_RunCount_90d__c || acct.i_HIPAA_RunCount_1y__c != oldacct.i_HIPAA_RunCount_1y__c || acct.i_HIPAA_RunCount_30d__c != oldacct.i_HIPAA_RunCount_30d__c || acct.i_HIPAA_RunCount_90d__c != oldacct.i_HIPAA_RunCount_90d__c || acct.i_NA_RunCount_1y__c != oldacct.i_NA_RunCount_1y__c || acct.i_NA_RunCount_30d__c != oldacct.i_NA_RunCount_30d__c || acct.i_NA_RunCount_90d__c != oldacct.i_NA_RunCount_90d__c || acct.i_PCI_RunCount_1y__c != oldacct.i_PCI_RunCount_1y__c || acct.i_PCI_RunCount_30d__c != oldacct.i_PCI_RunCount_30d__c || acct.i_PCI_RunCount_90d__c != oldacct.i_PCI_RunCount_90d__c || acct.i_SA_RunCount_1y__c != oldacct.i_SA_RunCount_1y__c || acct.i_SA_RunCount_30d__c != oldacct.i_SA_RunCount_30d__c || acct.i_SA_RunCount_90d__c != oldacct.i_SA_RunCount_90d__c || acct.i_SQL_RunCount_1y__c != oldacct.i_SQL_RunCount_1y__c || acct.i_SQL_RunCount_30d__c != oldacct.i_SQL_RunCount_30d__c || acct.i_SQL_RunCount_90d__c != oldacct.i_SQL_RunCount_90d__c || acct.i_Reporter_Purch_Date__c != oldacct.i_Reporter_Purch_Date__c || acct.i_Reporter_Exp_Date__c != oldacct.i_Reporter_Exp_Date__c || acct.i_AGP_GDPR_Units__c != oldacct.i_AGP_GDPR_Units__c || acct.i_AGP_Program__c != oldacct.i_AGP_Program__c || acct.i_AGP_Purch_Date__c != oldacct.i_AGP_Purch_Date__c || acct.i_AG_GDPR_Exp_Date__c != oldacct.i_AG_GDPR_Exp_Date__c || acct.i_AG_GDPR_Price__c != oldacct.i_AG_GDPR_Price__c || acct.i_AG_HIPAA_Customer__c != oldacct.i_AG_HIPAA_Customer__c || acct.i_AG_HIPAA_Exp_Date__c != oldacct.i_AG_HIPAA_Exp_Date__c || acct.i_AG_HIPAA_Price__c != oldacct.i_AG_HIPAA_Price__c || acct.i_AG_HIPAA_Purch_Date__c != oldacct.i_AG_HIPAA_Purch_Date__c || acct.i_AG_HIPAA_Units__c != oldacct.i_AG_HIPAA_Units__c || acct.ob_Cyber_Hawk__c != oldacct.ob_Cyber_Hawk__c || acct.ob_Cyber_Hawk_Date__c != oldacct.ob_Cyber_Hawk_Date__c || acct.ob_Exchange__c != oldacct.ob_Exchange__c || acct.ob_Exchange_Date__c != oldacct.ob_Exchange_Date__c || acct.ob_GDPR__c != oldacct.ob_GDPR__c || acct.ob_GDPR_Date__c != oldacct.ob_GDPR_Date__c || acct.ob_HIPAA__c != oldacct.ob_HIPAA__c || acct.ob_HIPAA_Date__c != oldacct.ob_HIPAA_Date__c || acct.ob_Inspector__c != oldacct.ob_Inspector__c || acct.ob_Inspector_Date__c != oldacct.ob_Inspector_Date__c || acct.ob_NAM_SAM__c != oldacct.ob_NAM_SAM__c || acct.ob_NAM_SAM_Date__c != oldacct.ob_NAM_SAM_Date__c || acct.ob_PCI__c != oldacct.ob_PCI__c || acct.ob_PCI_Date__c != oldacct.ob_PCI_Date__c || acct.ob_Reporter__c != oldacct.ob_Reporter__c || acct.ob_Reporter_Date__c != oldacct.ob_Reporter_Date__c || acct.ob_SQL__c != oldacct.ob_SQL__c || acct.ob_SQL_Date__c != oldacct.ob_SQL_Date__c || acct.Lead_Status__c != oldacct.Lead_Status__c || acct.Lead_Status_Reason__c != oldacct.Lead_Status_Reason__c || acct.Already_Being_Worked__c != oldacct.Already_Being_Worked__c || acct.i_DWID_Customer__c != oldacct.i_DWID_Customer__c || acct.i_DWID_Exp_Date__c != oldacct.i_DWID_Exp_Date__c || acct.i_DWID_Level__c != oldacct.i_DWID_Level__c || acct.i_DWID_Price__c != oldacct.i_DWID_Price__c || acct.i_DWID_Purch_Date__c != oldacct.i_DWID_Purch_Date__c || acct.i_AG_CI_Customer__c != oldacct.i_AG_CI_Customer__c || acct.i_AG_CI_Exp_Date__c != oldacct.i_AG_CI_Exp_Date__c || acct.i_AG_CI_Price__c != oldacct.i_AG_CI_Price__c || acct.i_AG_CI_Purch_Date__c != oldacct.i_AG_CI_Purch_Date__c || acct.i_AG_CI_Units__c != oldacct.i_AG_CI_Units__c ) { accounts.add(acct); } } ProactiveWatchHelper.UpdateContactDetails(accounts);
A thumb rule to remeber is that long running operations should never be done synchronously. An account can have any number of contacts. For an account with few contacts, say around 50, this code may work without any issues. But for an account, where the number of contacts are high, this approach will definitely fail. The reason is, the higher the number of contacts, the higher will be the number of iterations and hence CPU time limit exception will come.
Fortunately, you have a simple fix for this problem. You need to move the logic present in ProactiveWatchHelper.UpdateContactDetails into a batch class. Let me know if you need more help on this. Thanks.
Thanks for the insight. I'm not quite sure how to move the logic into a batch class. Here is what we have in ProactiveWatchHelper.UpdateContactDetails