+ Start a Discussion
Dev2IndiaDev2India 

APEX CPU TIME LIMIT ISSUE : New release limit

Hello Experts,

 

Below is the pretty much good code i am using in orde to update the first activity date field. As per the new salesforce limit I am getting the Apex cpu time limit during mass upload. Some time it works fine and get the error at 1000 upload where in 900 sucess and limit exceed for rest. Sometime it occurs at 100 task uploads only.

 

Please let me know how to overcome this and test the consumption of limits in this code ?? Code was well behaved with preveous releses.

 

===========================================================

trigger SetFirstactivityDate on Task(after update,after insert) {
   
  
  
    Set<Id> RecordIds=new Set<Id>();
    for (Task evntObj: Trigger.new){
       
        RecordIds.add(evntObj.WhoId);
   
    }
    List<Contact> conList=new List<Contact>();
    for(Contact con:[Select id,(SELECT ActivityDate FROM ActivityHistories Where ActivityDate < = Today  Order
    By ActivityDate ASC Limit 1) from Contact where id in:RecordIds])
    {
         if(con.ActivityHistories.size()>0)
         {
         con.First_ACTIVITY_Date__c=con.ActivityHistories[0].ActivityDate;
         conList.add(con);
         con.Apex_context__c=True; \\ in this line bypassing all the validation rule
         }
         else
         {
         con.First_ACTIVITY_Date__c=null;
         conList.add(con);
         con.Apex_context__c=True; \\ in this line bypassing all the validation rule
         }
        
    }
   
    if(conList.size()>0)
    {
    update conList;
    }
    }

===========================================================

 

Appreciate your suggestions ...........

 

Thanks.......

Dev2IndiaDev2India

Expert advice needed in this....

Prashant TiwariPrashant Tiwari

Hi, 

 

If this is not working only because of CPU asynchronous apex limit (which is 10,000 Miliseconds) then try it using batch class.

Define the logic in batch class and call it from this trigger.

 

Hope this will work.

 

Please mark this as solved if it works as it may help others..

 

 

Thanks,

 

Prashant Tiwari