+ Start a Discussion
Huy NguyenHuy Nguyen 

System.Limit exception:Apex CPU time limit

I got system.limit exception:Apex CPU Time limit exceeded for my trigger .Please let me know which one need to improve to avoid that error. The error report at line 22,column 1

trigger LineItemTypeTrigger on Line_Item_Type__c (after update) {
    try
    {
        if (Trigger.isUpdate && Trigger.isAfter)
        {
            Map<String, Decimal> oldRecAuditAmount = new Map<String, Decimal>();
            Map<String, Decimal> oldRecAmount = new Map<String, Decimal>();
            Set<String> litTriggerIds = new Set<String>();
            for (Line_Item_Type__c oldRec : Trigger.old) {
                if (oldRec.Calculated_Amount__c != null)
                    oldRecAmount.put(oldRec.Id, oldRec.Calculated_Amount__c );
                if (oldRec.Calculated_Audit_Amount__c != null)
                    oldRecAuditAmount.put(oldRec.Id, oldRec.Calculated_Audit_Amount__c );
            }
            
            for (Line_Item_Type__c newRec : Trigger.new) {
                if (oldRecAmount.containsKey(newRec.Id))
                    if (oldRecAmount.get(newRec.Id) != newRec.Calculated_Amount__c )
                        litTriggerIds.add(newRec.Id);
                if (oldRecAuditAmount.containsKey(newRec.Id))
                    if (oldRecAuditAmount.get(newRec.Id) != newRec.Calculated_Audit_Amount__c )
                        litTriggerIds.add(newRec.Id);
            }
            
            if (litTriggerIds.size() > 0)
            {
                LineItemTypeTriggerHandler handler = new LineItemTypeTriggerHandler();  
                handler.OnAfterUpdate(litTriggerIds);
            }
        }
    }
    catch (Exception e)
    {
        System.debug('Error Occurred:' + '[' + e.getMessage() +' ]');
    }
}

 
Best Answer chosen by Huy Nguyen
Swati GSwati G
Hi,

Try combining the two for loops as below and check if you still gets this issue. If error comes again then please share your handler code.

for(Integer i = 0; i < Trigger.new.size();i++){
   Line_Item_Type__c oldRec = (Line_Item_Type__c)Trigger.old[i];    
   Line_Item_Type__c newRec = (Line_Item_Type__c)Trigger.new[i];
   if(oldRec.Calculated_Amount__c != null && oldRec.Calculated_Amount__c != newRec.Calculated_Amount__c){
     litTriggerIds.add(newRec.Id);
   }
   if(oldRec.Calculated_Audit_Amount__c != null && oldRec.Calculated_Audit_Amount__c != newRec.Calculated_Audit_Amount__c){
     litTriggerIds.add(newRec.Id);
   }    
}

All Answers

Swati GSwati G
Hi,

Try combining the two for loops as below and check if you still gets this issue. If error comes again then please share your handler code.

for(Integer i = 0; i < Trigger.new.size();i++){
   Line_Item_Type__c oldRec = (Line_Item_Type__c)Trigger.old[i];    
   Line_Item_Type__c newRec = (Line_Item_Type__c)Trigger.new[i];
   if(oldRec.Calculated_Amount__c != null && oldRec.Calculated_Amount__c != newRec.Calculated_Amount__c){
     litTriggerIds.add(newRec.Id);
   }
   if(oldRec.Calculated_Audit_Amount__c != null && oldRec.Calculated_Audit_Amount__c != newRec.Calculated_Audit_Amount__c){
     litTriggerIds.add(newRec.Id);
   }    
}
This was selected as the best answer
Huy NguyenHuy Nguyen
Thank Swati