+ Start a Discussion
Rija SanaRija Sana 

Running into cpu time limit exceeded -- Help!

I have the following class that is hitting cpu time limit exception. still learning so help very much appreciated!!  When its working I'll put it in a schedulable class. 
 
public class test1 {
 public static void test1() {
      
     Map<id,account> aMap = new Map<id,account>([Select Id,Name, Total_number_of_open_opportunities__c,Total_number_of_open_Upsel_opportunities__c, No_of_days_since_last_AE_activity__c
                        FROM ACCOUNT WHERE Assigned_AE__c != null AND (Assigned_AE__r.profile.Name = 'Sales Executive' OR 
   Assigned_AE__r.profile.Name = 'Premier Account Executive') AND No_of_days_since_AE_assigned__c >=60]);
     List<account> acc =  aMap.values() ;
   
   system.debug(acc.size());
   
   List<Opportunity> opps = new List<Opportunity>();    //list of all open opps
   List<account> acc1 = new List<account>();  
 
   
     
  if(acc.size() > 0)
   {
  // Query to get all open opportunites with no stage change in over 60 days 
   Map<id,opportunity> aMap2 = new Map<id,opportunity>([SELECT Id,accountid FROM Opportunity 
                                     WHERE AccountId IN :acc AND ISCLOSED = FALSE AND days_since_last_stage_change__c > 60 AND (RecordType.Name= 'New Business' OR RecordType.Name= 'Upsell')]);
       
       opps = aMap2.values() ;
   
  
     for (account a : acc )
     {
       
         
       If((a.Total_number_of_open_opportunities__c == 0 && a.Total_number_of_open_Upsel_opportunities__c == 0) || (a.No_of_days_since_last_AE_activity__c > 30 || a.No_of_days_since_last_AE_activity__c == 0) || opps.size()>0)
        { 
           
            a.Assigned_AE__c = null;
            acc1.add(a);
        }
       
     }
     update acc1;
   }
}
}


 
Best Answer chosen by Rija Sana
Rija SanaRija Sana
Thanks both for your help. I was able to by pass the cpu time limit exception by making code asynchronous. 

All Answers

GauravendraGauravendra
Hi Rija,

I don't find anything suspicious in the code you have shared.
Kindly check the account trigger and workflow on account object. The time taken to run the action mentioned on Account update also count against CPU time in this case as you are updating account.
Hope that helps!
Khan AnasKhan Anas (Salesforce Developers) 
Hi Rija,

Greetings to you!

Your code looks good to me. Please refer to the below blog and Salesforce Knowledge Article which might help you further with the above issue.

https://welkinsuite.com/blog/how-to-get-past-apex-cpu-limits/

https://help.salesforce.com/articleView?id=000339361&language=en_US&type=1

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Rija SanaRija Sana
Thanks both for your help. I was able to by pass the cpu time limit exception by making code asynchronous. 
This was selected as the best answer