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

Why is Apex CPU Time Limit exceeded for this trigger?
I have a rather simple trigger. It updates a custom Account field when the opportunity is updated. I don't know why I get an error about a CPU time limit?
"Apex trigger: UpdateLateStageOppField caused an unexpected exception. SystemLimitException: Apex CPU time limit exceeded. "
When you answer this please, could you actually type in some coding changes? I hardly use Apex and am really not familiar with it. General advice won't be sufficient. Thanks for your help!
"Apex trigger: UpdateLateStageOppField caused an unexpected exception. SystemLimitException: Apex CPU time limit exceeded. "
When you answer this please, could you actually type in some coding changes? I hardly use Apex and am really not familiar with it. General advice won't be sufficient. Thanks for your help!
trigger UpdateLateStageOppField on Opportunity (after delete, after insert, after update) { try{ //the trigger will update the account's SAM Marketing Customer field //and also the Late_Stage_Opps__c, too if (! trigger.isDelete) { List<Id> oppIds = new List<Id>() ; List<Id> AccountIds = new List<Id>() ; List<Account> AcctToUpdate = new List<Account>() ; for (opportunity op : trigger.New) { oppIds.add(op.Id); AccountIds.add(op.AccountId); } Map<Id,Account> acctMap = new Map<Id,Account>([select id, SAM_Marketing_Customer__c, name, Closed_Won_Opps__c, Type from Account where id in :AccountIDs]); Map<Id,Opportunity> oppMap2 = new Map<Id,Opportunity>([Select id, StageName, Opportunity__c from Opportunity where Accountid in :AccountIDs]) ; for (opportunity op : trigger.New){ //Find the account for this opportunity which is being update Account oAccount = acctMap.get(op.AccountId); Opportunity StageOpportunities = oppMap2.get(op.Id); string stagelist; stagelist = ''; if (oppMap2.isEmpty()){ //No opportunities for that account stagelist = ''; } else { stagelist = ''; System.debug('In Else StageList '); for (Opportunity stageMap: oppMap2.values()) { system.debug ('Oppor. ' + StageMap.Opportunity__c); if (stageMap.StageName=='Closed Won' || stageMap.StageName=='Negotiation/Review' || stageMap.StageName=='Perception Analysis' || stageMap.StageName=='Value Proposition'){ stagelist += stageMap.Opportunity__c + ','; } } } oAccount.Late_Stage_Opps__c = stagelist; AcctToUpdate.add(oAccount); } update AcctToUpdate; } if (trigger.isDelete) { List<Id> oppIds = new List<Id>() ; List<Id> AccountIds = new List<Id>() ; List<Account> AcctToUpdate = new List<Account>() ; for (opportunity op : trigger.Old) { oppIds.add(op.Id); AccountIds.add(op.AccountId); } Map<Id,Account> acctMap = new Map<Id,Account>([select id, SAM_Marketing_Customer__c, name, Closed_Won_Opps__c, Type from Account where id in :AccountIDs]); Map<Id,Opportunity> oppMap2 = new Map<Id,Opportunity>([Select id, StageName, Opportunity__c from Opportunity where Accountid in :AccountIDs]) ; for (opportunity op : trigger.Old){ //Find the account for this opportunity which is being update Account oAccount = acctMap.get(op.AccountId); //Opportunity SamOpportunity = oppMap.get(op.Id); Opportunity StageOpportunities = oppMap2.get(op.Id); string stagelist; if (oppMap2.isEmpty()){ //No opportunities for that account stagelist = ''; } else { stagelist = ''; System.debug('in stage list'); for (Opportunity stageMap: oppMap2.values()) { System.debug('in values list'); if (stageMap.StageName=='Closed Won' || stageMap.StageName=='Negotiation/Review' || stageMap.StageName=='Perception Analysis' || stageMap.StageName=='Value Proposition'){ stagelist += stageMap.Opportunity__c + ','; } } } oAccount.Late_Stage_Opps__c = stagelist; AcctToUpdate.add(oAccount); } update AcctToUpdate; } } catch (Exception e ){ System.debug('Create customer field trigger exception ' + e.getMessage()); } }
All Answers
We need to determine which part of the code might be causing this and in what circumstances this error occurs (like during a bulk upload or single record update) and should be able to replicate it. Once we know how to easily replicate it, we can use the following line after every couple of lines and find out in the debug logs which part of the code is faulty and make that code asynchronous (by using @future annotation).
system.debug(limits.getcputime());
The trigger works with just a few records. I have updated up to 100 records at once. Here is a list of tests that I have done in updating a group of opportunities.
200 records did not work - 18.74 secs.
150 records did not work - 17 seconds
125 records did not work 16 secs.
100 records worked - 11 secs.
AS you see, it is quite slow, but doesn't seem to do all that much.I am simply updating a custom field. I will add the system.debug line that you have suggested.
Is there a way to do this with a formula field? Would it be possible to write a formula that creates a comma-delimited list of the opportunities in an account? that's all I need.
The field label is Late Stage Opps and the field name is Late_Stage_Opps. The API Name is Late_Stage_Opps__c.
When an opportunity is updated then this new Accounts custom field is updated with a comma delimited list of opportunities which have only the following stages:
closed won, negotiation/review, perception analysis or value proposition.
For instance if there were three opportunities in a certain Account:MobiLab closed/Won
Accelero qualification
IatriScan Negotiation/Review
Then the new custom field would contain "MobiLab,IatriScan"
If Accelero later had a stage of "closed won" then the field would be updated with a delimited list of three names.
Is this clear enough? Thanks!
You can leave your feedback as a comment here and my manager will be able to see it :)
Thanks,
Shashank