Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am getting this error: 'Error:System.LimitException: Apex CPU time limit exceeded' for this simple trigger. The trigger is working just fine. Can anyone tell me if I am doing something wrong in the script below. I will appreciate any help!

 trigger UpdateNRProducts on Account (after update) {

   Set<NRProducts__c> parents = new Set<NRProducts__c>();


   for (NRProducts__c  a : [SELECT Id FROM NRProducts__c WHERE Account__c IN :Trigger.new]){

    parents.add(new NRProducts__c(Id = a.id )); 

    }


    if(!parents.isEmpty()) update new List<NRProducts__c>(parents);

  }

or This trigger which is in the NRProducts Object

   trigger buyerlookupTriggerNRProducts on NRProducts__c (before insert,before update) {

  Map<id,Account> map1= new Map<id,Account>();
  for(NRProducts__c rod : Trigger.new){
    map1.put(rod.Account__c,null);
   }
  map1 = new Map<ID,Account>([select id, Owner.id, Specialist__c, Brand_Manager__c  from Account where Id IN : map1.keySet()]);

  for(NRProducts__c rod : Trigger.new)
  if(map1.get(rod.Account__c) != null){


            rod.Buyer_Lookup__c = map1.get(rod.Account__c).Owner.id;
            rod.Specialist_Lookup__c = map1.get(rod.Account__c).Specialist__c;
            rod.Brand_Manager__c = map1.get(rod.Account__c).Brand_Manager__c;


  }
}
share|improve this question
    
and did you by any chance look at the debug log to gain any insight? Is it happening for a specific Account, a batch of Accounts? How many NRProducts__c are returned and what, if anything happens when an NRProduct is updated? –  crop1645 Aug 15 at 22:35
    
crop1645, usually and Account has around 200 NRProducts, so I don't think that's an issue. There is an API running on NRProducts__c Object all day long. Do you see anything wrong with code? if not, do you think the API is interfering? –  Carlos Aug 15 at 22:44

1 Answer 1

Your problem isn't here, but somewhere else. You'll need to set profiling to the highest level ("FINEST") and see which functions consume the most time in the heap/debug logs.

"API's running all day long" wouldn't cause this error either, as CPU limits are per-transaction. There's just some function that's eating up enormous amounts of CPU time. Check for any triggers on NRProducts__c, see if there's any DML operations that would cascade further updates, etc.

By the way, you can write your entire trigger like this:

trigger updateNRProducts on Account (after update) {
    update [SELECT Id FROM NRProducts__c WHERE Account__c IN :Trigger.new];
}

This removes the need for the loop (which uses CPU time), although that loop shouldn't be anything more than a few milliseconds. Assuming you have similarly structured code elsewhere, it could just be too much inefficient code eating away at the total CPU time available.

Personally, I'd guess that the NRProducts__c triggers are using nested loops instead of maps, or somewhere else down the line has that problem.

Edit

Loops are very slow. Extremely.

Consider the following code:

Long time1 = DateTime.now().getTime();
Account[] records = new Account[0];
Account[] temp = [SELECT Id FROM Account LIMIT 1000];
Long time2 = DateTime.now().getTime();
System.debug(LoggingLevel.ERROR, DateTime.now().getTime());
for(Account record:temp)
    records.add(new Account(Id=record.Id));
Long time3 = DateTime.now().getTime();
System.debug(LoggingLevel.ERROR, 'Query Time: '+(time2-time1)+'ms');
System.debug(LoggingLevel.ERROR, 'Loop Time: '+(time3-time2)+'ms');

1,000 records in my org outputs the following:

17:04:09:691 USER_DEBUG [9]|ERROR|Query Time: 20ms
17:04:09:691 USER_DEBUG [10]|ERROR|Loop Time: 5638ms

I'm not sure if it's memory allocation or a slow iterator, but there's definitely some odd going on here. Save your CPU time and just update the list directly. Avoid memory-hogging loops.

share|improve this answer
    
I will try that right now.. –  Carlos Aug 15 at 23:07
    
sfdcfox, i am new in SF, could you please show me an example of nested loops. I don't know how to differentiate one. I truly appreciate your help. –  Carlos Aug 15 at 23:09
1  
A nested loop is a loop inside a loop, like for(...) { for(...) { ... } }. This has a very poor performance (e.g. 200 records by 200 records is 40,000 executions of code). –  sfdcfox Aug 15 at 23:10
    
i have updated my question with the trigger that is in the NRProducts__c obecjt, do you see anything wrong there? does it need to be before update and after update or just one? –  Carlos Aug 15 at 23:15
    
That trigger is also perfectly optimized. I have to experiment with loops, because there seems to be a new performance bottleneck. I'll report my findings soon as I get back to a desktop. –  sfdcfox Aug 15 at 23:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.