function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew Hoban 6Andrew Hoban 6 

CPU Time-Out on Trigger isBefore

Hi,

I am getting a CPU Time-Out error message with reference to the trigger below.

The only records I want to be triggered are records that match the IF criteria, however all records will be processed within the loop.

When I add 800 test records that dont even meet the critera, im getting the CPU error.

What would be the best practice to only process the records that meet the criteria beofore entering the loop? 
 
trigger accountTrigger on Account (before insert, before update, after insert, after update) {
    
    if(Trigger.isBefore)
    {
        if(Trigger.isInsert) // Run only BEFORE Insert 
        {
            
            
        }
        
        if(Trigger.isUpdate) // Run only BEFORE update
        {
            
            
        }
        
        if (Trigger.isInsert || Trigger.isUpdate) {

            for(Account acc: Trigger.new){
               

                if (acc.EFCAIS__Status__c != NULL && acc.PersonBirthdate != Null){
                    
                    academyCriteriaHandler.calculateAgeInformationCriteria(Trigger.new, Trigger.oldMap, Trigger.newMap);
                } 
            }
            
        }
    }


 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Andrew,

I found few links that could be of help in debugging where the issue occurring from below are the links:

Apex CPU Limit Exceeded.

https://help.salesforce.com/apex/HTViewSolution?id=000232681&language=en_US 
 
https://developer.salesforce.com/page/Apex_Code_Best_Practices
 
http://www.jitendrazaa.com/blog/salesforce/batch-apex-first-error-apex-cpu-time-limit-exceeded/
 
Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Anutej
David Zhu 🔥David Zhu 🔥
With 800 records, it should not have CPU limit problem with the right coding pattern. You may move calculation out of the loop to avoid this issue.
trigger accountTrigger on Account (before insert, before update, after insert, after update) {
    
    if(Trigger.isBefore)
    {
       if (Trigger.isInsert || Trigger.isUpdate) {

            List<Account> accountList = new List<Account>();
            Map<Id,Account> oldMap = new Map<Id,Account>();
            Map<Id,Account> newMap = new Map<Id,Account>();
            for(Account acc: Trigger.new){
               
                if (acc.EFCAIS__Status__c != NULL && acc.PersonBirthdate != Null){
                    accountList.add(acc);
                    newMap.put(acc.Id,acc);
                    if (Trigger.isUpdate) {
                        oldMap.put(acc.Id,trigger.oldMap.get(acc.Id));
                    }
                } 
            }

            if (accountList.size() > 0)
            {
                academyCriteriaHandler.calculateAgeInformationCriteria(accountList, oldMap, newMap);
            }            
            
        }
    }
}

 
Sanjay Bhati 95Sanjay Bhati 95
Hi

You are writing the wrong code in the trigger. Please follow the below code for your functionality.
trigger accountTrigger on Account (before insert, before update, after insert, after update) {
    
    List<Account> accList = new List<Account>();
    if(Trigger.isBefore){
        if (Trigger.isInsert || Trigger.isUpdate) {
            for(Account acc: Trigger.new){
                if (acc.EFCAIS__Status__c != NULL && acc.PersonBirthdate != Null){
                    accList.add(acc);
                } 
            }
            if(accList.size() > 0){
                 academyCriteriaHandler.calculateAgeInformationCriteria(accList, Trigger.oldMap, Trigger.newMap);
            }
        }
    }

Please let me know if this code still giving you the CPU time error.

Please also check that you don't have any other trigger on the same Account Object. Maybe you have any other process builder or workflow that is updating the same account record again and again.