+ Start a Discussion
Pete Watson 5Pete Watson 5 

CPU Time Limit Issue On Trigger

Hi All, 

Any help on the below would be much appreciated.

have the below trigger and its working perfectly in the sandbox but after deploying to prod I am getting a CPU time limit exeeded failure... 

trigger ProfileTrigger on User (before insert, before update) {
    List<Id> userIds = new List<Id>();
    List<Case> cases = new List<Case>();
    List<Case> casesToUpdate = new List<Case>();
    for(User u: Trigger.new) {
        if(u.out_of_office__c==True) {
            userIds.add(u.Id);
        }
        for (case c : (cases = [SELECT id, Status FROM Case where ownerId IN:userIds]))
            IF(c.Status == 'Assigned' || c.Status == 'Working' ||c.Status == 'Escalated'){
            c.Reassign_to_Queue__c=true;
            casesToUpdate.add(c);
        }
        update casesToUpdate;
    }

Many thanks in advance! 
Pete
Best Answer chosen by Pete Watson 5
AnudeepAnudeep (Salesforce Developers) 
Hi Pete, 

I recommend adding Limits.getCpuTime() to check where there is the maximum consumption is
 
Integer cpuStart = Limits.getCpuTime();
System.debug('Before For loop = ' +cpuStart);

    for(User u: Trigger.new) {
        if(u.out_of_office__c==True) {
            userIds.add(u.Id);
 System.debug('CPU time consumed in loop ' + (Limits.getCpuTime() - cpuStart));
        }

// repeat this for the other for loop

// Check CPU time before DML

 cpuStart = Limits.getCpuTime();

 System.debug('Before DML = ' +cpuStart);

 update casesToUpdate;

I suggest comparing the debug logs between your sandbox and production when running this code

Also, review this help article

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Pete, 

I recommend adding Limits.getCpuTime() to check where there is the maximum consumption is
 
Integer cpuStart = Limits.getCpuTime();
System.debug('Before For loop = ' +cpuStart);

    for(User u: Trigger.new) {
        if(u.out_of_office__c==True) {
            userIds.add(u.Id);
 System.debug('CPU time consumed in loop ' + (Limits.getCpuTime() - cpuStart));
        }

// repeat this for the other for loop

// Check CPU time before DML

 cpuStart = Limits.getCpuTime();

 System.debug('Before DML = ' +cpuStart);

 update casesToUpdate;

I suggest comparing the debug logs between your sandbox and production when running this code

Also, review this help article

Anudeep
This was selected as the best answer
Pete Watson 5Pete Watson 5
Thank you very much Anudeep. I dont know what made the difference to be honest but I added the above in and re-deployed... working like a charm now! 
Abdul KhatriAbdul Khatri
Hi Pete,

I see a fundamental errors and not following salesforce best practices which may lead in issues in future. Here is the code for you, please make sure you following the rules mentioned here

https://developer.salesforce.com/index.php?title=Apex_Code_Best_Practices&oldid=26951 (https://developer.salesforce.com/index.php?title=Apex_Code_Best_Practices&oldid=26951)
 
trigger ProfileTrigger on User (before insert, before update) 
{
    List<Id> userIds = new List<Id>();
    List<Case> cases = new List<Case>();
    List<Case> casesToUpdate = new List<Case>();
    
    for(User u: Trigger.new) {
        if(u.out_of_office__c==True) {
            userIds.add(u.Id);
        }       
    }

	if(userIds.isEmpty()) return;
    
    for (case c : [SELECT id, Status 
                   	FROM Case where ownerId IN:userIds 
                   	AND (Status = 'Assigned' OR Status = 'Working' OR Status = 'Escalated')])
    {
          c.Reassign_to_Queue__c=true;
          casesToUpdate.add(c);
	}   
    
    if(!casesToUpdate.isEmpty())  update casesToUpdate;
}