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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AssignTasksBasedOnPhoneTags: maximum trigger depth exceeded Task trigger event

Overview/context:

I have a new phone system that sync calls summary into Salesforce in a form of tasks. Now I'm developing a trigger that fires each time these tasks (Call summary) are logged with a certain critieria, creating a new tasks on Salesforce based on the call result.

Code:
Trigger AssignTasksBasedOnPhoneTags on Task (After insert) {

    // Create a list that includes all the Tasks
    List<Task> TasksList = new List<Task>();
    
    Task NewestSalesRelatedTask = new Task();
    
    NewestSalesRelatedTask = [SELECT Id, whatid, CallDisposition, whoid
                                 FROM Task 
                                 WHERE CallDisposition LIKE '%Sales inquiries%' AND CallDisposition LIKE '%Call requires a follow-up%'
                                 ORDER BY ActivityDate ASC
                                 LIMIT 1];
                                                        
                                                                            
    if(NewestSalesRelatedTask.CallDisposition.contains('Sales inquiries') && NewestSalesRelatedTask.CallDisposition.contains('Call requires a follow-up')) {
    
    TasksList.add(new Task(subject= 'Alissa: A new lead needs follow up',
                 Description= 'Hey Alissa, based on this lead last call, this need lead need to be followed up with. Please refer to leads previous call',
                 ActivityDate= Date.today().addDays(1),
                 WhatID= NewestSalesRelatedTask.whatid));
    } 
    insert TasksList;
}

Reproduction/Error message:
 
Create any new task manually or the telephony system would lead to this error message. Even when conditition is not met: (CallDisposition LIKE '%Sales inquiries%' AND CallDisposition LIKE '%Call requires a follow-up%')

" Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger AssignTasksBasedOnPhoneTags caused an unexpected exception, contact your administrator: AssignTasksBasedOnPhoneTags: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AssignTasksBasedOnPhoneTags: maximum trigger depth exceeded Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert Task trigger event AfterInsert: []: () "
Raj VakatiRaj Vakati
Try this code
Refer this link 

https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

Many developers face this issue because of a recursive trigger. For example, in an 'after update' trigger, a developer could be performing an update operation and this would lead to recursive call, and the error:
"maximum trigger depth exceeded"


Apex CLass

 
public class checkRecursive {
    private static boolean alreadyCreatedTasks = false;
}
Trigger 

 
Trigger AssignTasksBasedOnPhoneTags on Task (After insert) {

    // Create a list that includes all the Tasks
    List<Task> TasksList = new List<Task>();
    
    Task NewestSalesRelatedTask = new Task();
    
    NewestSalesRelatedTask = [SELECT Id, whatid, CallDisposition, whoid
                                 FROM Task 
                                 WHERE CallDisposition LIKE '%Sales inquiries%' AND CallDisposition LIKE '%Call requires a follow-up%'
                                 ORDER BY ActivityDate ASC
                                 LIMIT 1];
                                                        
                                                                            
    if(NewestSalesRelatedTask.CallDisposition.contains('Sales inquiries') && NewestSalesRelatedTask.CallDisposition.contains('Call requires a follow-up')) {
         if (!checkRecursive.hasAlreadyCreatedFollowUpTasks()) {

    TasksList.add(new Task(subject= 'Alissa: A new lead needs follow up',
                 Description= 'Hey Alissa, based on this lead last call, this need lead need to be followed up with. Please refer to leads previous call',
                 ActivityDate= Date.today().addDays(1),
                 WhatID= NewestSalesRelatedTask.whatid));
				  insert TasksList;
	
				             
    } 
   
	
	}
}

 
Mahmoud Coudsi 1Mahmoud Coudsi 1
Hi Raj,

Thanks for the quick turn around! The error message is no longer happening (Thanks for that!). However, the trigger is not working as expected. It's not firing the action that it's built to trigger (Assign task) eventhough I'm creating tasks with the condition that I specified above. Can spot any line that needs to be debugged any my code?
Global class checkRecursive {
    Global static boolean alreadyCreatedTasks = false;
}
Trigger AssignTasksBasedOnPhoneTags on Task (After insert) {

    // Create a list that includes all the Tasks
    List<Task> TasksList = new List<Task>();
    
    Task NewestSalesRelatedTask = new Task();
    
    NewestSalesRelatedTask = [SELECT Id, whatid, CallDisposition, whoid
                                 FROM Task 
                                 WHERE CallDisposition LIKE '%Sales inquiries%' AND CallDisposition LIKE '%Call requires a follow-up%'
                                 ORDER BY ActivityDate ASC
                                 LIMIT 1];
                                                        
                                                                            
    if(NewestSalesRelatedTask.CallDisposition.contains('Sales inquiries') && NewestSalesRelatedTask.CallDisposition.contains('Call requires a follow-up')) {
         if (!checkRecursive.alreadyCreatedTasks == false) {

    TasksList.add(new Task(subject= 'Alissa: A new lead needs follow up',
                 Description= 'Hey Alissa, based on this lead last call, this need lead need to be followed up with. Please refer to leads previous call',
                 ActivityDate= Date.today().addDays(1),
                 WhatID= NewestSalesRelatedTask.whatid));
                 insert TasksList;
                
                             
    } 

    
    }
}

 
Raj VakatiRaj Vakati
Try this
 
Trigger AssignTasksBasedOnPhoneTags on Task (After insert) {
    if (!checkRecursive.alreadyCreatedTasks == false) {
    // Create a list that includes all the Tasks
    List<Task> TasksList = new List<Task>();
    
    Task NewestSalesRelatedTask = new Task();
    
    NewestSalesRelatedTask = [SELECT Id, whatid, CallDisposition, whoid
                                 FROM Task 
                                 WHERE CallDisposition LIKE '%Sales inquiries%' AND CallDisposition LIKE '%Call requires a follow-up%'
                                 ORDER BY ActivityDate ASC
                                 LIMIT 1];
                                                        
                                                                            
    if(NewestSalesRelatedTask.CallDisposition.contains('Sales inquiries') && NewestSalesRelatedTask.CallDisposition.contains('Call requires a follow-up')) {
     

    TasksList.add(new Task(subject= 'Alissa: A new lead needs follow up',
                 Description= 'Hey Alissa, based on this lead last call, this need lead need to be followed up with. Please refer to leads previous call',
                 ActivityDate= Date.today().addDays(1),
                 WhatID= NewestSalesRelatedTask.whatid));
                 insert TasksList;
                
                             
    } 

    
    }
}