You need to sign in to do that
Don't have an account?

Please HELP - Apex CPU time limit exceeded
Hello All,
I have trigger on the the Account object that updates a custom field (MyOpsPath) on the Opportunity object when Account.Name,Site, or SI_Site_Reference__c changes. My trigger fails after the 4th update due to the CPU time limit error.
Is anyone willing to take a look, other suggestions, or help fix my code?
With thanks in advance.
Robert
trigger UpdateMyOpsPathFromAccount on Account (after update) { System.Debug('*** UpdateMyOpsPathFromAccount *** '); //String ClosedLit='Closed%'; *** 27/Feb/2020 String Slsh = '\\'; String PthLit1 = 'ni-cr-svc-fp1'; String PthLit2 = 'QUOTES AND CONTRACTS - LIBRARY'; Set<Id> accountIds = new Set<Id>(); for(Account acc : trigger.new) { if(acc.SI_Site_Reference__c!=null){ //System.Debug('This Accounts SI_Site Reference__c='+acc.SI_Site_Reference__c); if((acc.Name != trigger.oldMap.get(acc.Id).Name) || (acc.Site != trigger.oldMap.get(acc.Id).Site) || (acc.SI_Site_Reference__c != trigger.oldMap.get(acc.Id).SI_Site_Reference__c)){ accountIds.add(acc.Id); String FrstLtr=acc.Name.substring(0,1); String Ctgry='2 Accounts (A to H)'; If(FrstLtr.containsAny('ABCDEFGH')) {Ctgry='2 Accounts (A to H)';} If(FrstLtr.containsAny('IJKLMNOP')) {Ctgry='3 Accounts (I to P)';} If(FrstLtr.containsAny('QRSTUVWXYZ')) {Ctgry='4 Accounts (Q to Z)';} System.Debug('acc.Name='+acc.Name); System.Debug('FrstLtr='+FrstLtr); System.Debug('Ctgry='+Ctgry); System.debug('Account with Change='+acc.Id); System.Debug('Old Account Name='+trigger.oldMap.get(acc.Id).Name); System.Debug('New Account Name='+acc.Name); System.Debug('Old Account Site='+trigger.oldMap.get(acc.Id).Site); System.Debug('New Account Site='+acc.Site); System.Debug('Old Account Site Reference='+trigger.oldMap.get(acc.Id).SI_Site_Reference__c); System.Debug('New Account Site Reference='+acc.SI_Site_Reference__c); if (!accountIds.isEmpty()){ // Get Opportunity Information List<Opportunity> LstOpps=[SELECT Id, MyOpsPath__c, AccountId, Almac_Division__c, StageName, Reference_Number__c FROM Opportunity //WHERE AccountId in: accountIds AND Almac_Division__c='SI' AND (NOT StageName LIKE:ClosedLit)]; *** 27/Feb/2020 WHERE AccountId in: accountIds AND Almac_Division__c='SI' ]; System.Debug('LstOpps.size='+LstOpps.size()); System.Debug('Id='+LstOpps[0].Id); System.Debug('AccountId='+LstOpps[0].AccountId); System.Debug('Alamc_Division__c='+LstOpps[0].Almac_Division__c); System.Debug('StageName='+LstOpps[0].StageName); If(!LstOpps.isEmpty()){ for(Opportunity o: LstOpps) { System.Debug('BEFORE-MyOpsPath='+LstOpps[0].MyOpsPath__c); LstOpps[0].MyOpsPath__c=(Slsh+Slsh+PthLit1+Slsh+'sqman'+Slsh+PthLit2+Slsh+Ctgry+Slsh+acc.Name+' ('+acc.Site+') '+acc.SI_Site_Reference__c+Slsh+'Quotes'+Slsh+LstOpps[0].Reference_Number__c+Slsh); System.Debug('AFTER-MyOpsPath__c='+LstOpps[0].MyOpsPath__c); Update LstOpps; System.Debug('*** Opportunity Updated ***'); } } } } } } }
To avoid CPU limit you need to optimize your code.
1. Check the trigger execution and with the help of developer console you can figure out where CPU time is spent and then target the area which most CPU costly.
2. Try and optimize those costly code blocks
3. Note for optimization of trigger you can use anomalous block to fire trigger
Please review below article for details on 'Apex CPU time limit exceeded'
https://help.salesforce.com/articleView?id=000339361&language=en_US&type=1&mode=1
If you need more information please log a case with salesforce support.
Hope above information was helpful.
Please mark as Best Answer so that it can help others in the future.
Thanks,
Vinay Kumar