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
Keaton KleinKeaton Klein 

CPU Limit Exception

Okay, i have this trigger which seems to work for all of its intended purposes but occasionally we are getting a CPU Limit Exception.  I've noticed that we get the CPU Limi Exception while Marketo is trying to update the Lead Score.  Any thoughts?
trigger LeadTrigger on Lead (after insert, after update, before insert, before update) {
    
     if (!FollowUpTaskHelper.hasAlreadyCreatedFollowUpTasks()) {    
             FollowUpTaskHelper.setAlreadyCreatedFollowUpTasks();
         string stringfield = '';
         Lead LeadLog = new Lead();
    Set<Id> Taskids = new Set<Id>();
    Set<Id> CloseRelatedTaskIds = new Set<Id>();
    Set<Id> CloseRelatedTaskIds2 = new Set<Id>();
    Set<Id> ReassignTaskLeads = new Set<Id>();
    Set<Id> NothingTranspiredTaskIds = new Set<Id>();
    Set<Id> TrustIds = new Set<Id>();
	Map<Id,task> TasksToUpdate = new Map<Id,Task>();
	Map<Id,task> TasksToUpdate2 = new Map<Id,Task>();
    Map<Id,task> TasksToUpdateRA = new Map<Id,Task>();
    Map<Id,lead> LeadsToUpdate = new Map<Id,Lead>();
    Map<Id,lead> LeadsToUpdate2 = new Map<Id,Lead>();
    Task TaskLog = new Task();
    Task TaskLog2 = new Task();
 string thefield123 = '';
            string thefield1234 ='';
    List<Lead> ls = new List<Lead>();
    list<user> ldusers = [select id, name from user where name = 'marketo' OR name = 'Marketo' OR name = 'Brad Davis' OR name = 'brad davis' order by name];      
   
    for(lead a: trigger.new)
    {
        if(a.isconverted == false){
        
                if(ldusers.size() != 0){
        if(a.ownerid == ldusers[1].id){
            a.ownerid = ldusers[0].id;
        }
        if(a.ownerid ==ldusers[0].id){
        ls.add(new Lead(id = a.id));
        }
    }
 
  
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule = true;
    dmo.EmailHeader.triggerUserEmail = true;    
    Database.update(ls, dmo);        
      
        if( a.Close_All_Current_Marketo_Open_Tasks__c == true  )
        {
            a.Close_All_Current_Marketo_Open_Tasks__c = false;
            CloseRelatedTaskIds.add(a.Id);
        }
             CloseRelatedTaskIds2.add(a.Id);    
			leadstoupdate2.put(a.id, a);
        
    }
     }
         
         
    
    for(task t:[SELECT id, whoid, subject, ownerid from task WHERE (whoid in: CloseRelatedTaskIds AND status != 'completed') OR (whoid in: CloseRelatedTaskIds2 AND status != 'completed') limit 100]){
        if(CloseRelatedTaskIds.contains(t.whoId))	{
        if(t.subject.contains('marketo') || t.subject.contains('Marketo') || t.subject.contains('MARKETO')){
        taskstoupdate.put(t.Id, t);
        
            if(taskstoupdate.get(t.Id) != null){
            tasklog = TasksToUpdate.get(t.id);
            tasklog.status = 'completed';
            stringfield = tasklog.subject;
            
            tasklog.subject = tasklog.subject + 'Set by trigger ezwf';
            taskstoupdate.put(tasklog.Id,tasklog);    

           }
   }
    }
         if(CloseRelatedTaskIds2.contains(t.whoId))	{
         			taskstoupdate2.put(t.Id, t);
        
            if(taskstoupdate2.get(t.Id) != null){
            tasklog2 = TasksToUpdate2.get(t.id);
            LeadLog = leadstoupdate2.get(t.WhoId);    
            tasklog2.ownerid = Leadlog.ownerid;
            TasksToUpdate2.put(TaskLog2.Id,TaskLog2);   
           }
         }
        
    }


       
      if(taskstoupdate != null && taskstoupdate.values() != null &&taskstoupdate.values().size() > 0){
        update TasksToUpdate.values();
    } 
               if(taskstoupdate2 != null && taskstoupdate2.values() != null &&taskstoupdate2.values().size() > 0){
        update TasksToUpdate2.values();
    } 
FollowUpTaskHelper.setAlreadyCreatedFollowUpTasksFalse();
     }
    
}

 
SotosSotos
Hi,

There is a dml call on line 42 inside the for loop that might cause the problem. 
Dml doesn't count towards cpu time but excecution of the trigger counts.
Also the dml should be outside the for loop to prevent dml limit as well.

Hope that helps!