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
Amit Jadhav 13Amit Jadhav 13 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateClientTenant: maximum trigger depth exceeded

Apex Trigger:
trigger CreateClientTenant on Lease_Summary__c (after update) {
    integer i = 1;
    List<Tenant__c> lstTenant = new List<Tenant__c>();
    List<Client__c> lstClient = new List<Client__c>();  
    for(Lease_Summary__c objLease : trigger.new){
        system.debug('objLease.Rent_start_date__c'+objLease.Rent_start_date__c);
        If(objLease.Rent_start_date__c == DATE.TODAY()){
            Tenant__c objtenant = new Tenant__c();
            objtenant.Rent_Start_Date__c = objLease.Rent_Start_Date__c;
            objtenant.Lease_Summary__c = objLease.id;
            lstTenant.add(objtenant);
            
        }
        system.debug('lstTenant'+lstTenant);
        insert lstTenant;
     }
}

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Amit,

Greetings to you!

"maximum trigger depth exceeded" exception in Salesforce occurs mainly due to recursion. Kindly implement a static variable in a class to avoid recursion.

Create a static variable in a class as true. Make it false before or after a trigger has been executed. Variable will become false when trigger runs for the first time (before workflow update). If the variable is true only then trigger will fire and if the trigger will try to execute the second time then it will not fire.

Handler Class:
public Class CheckRecursive {
    
    private static boolean run = true;
    
    public static boolean runOnce() {
        if(run) {
            run=false;
            return true;
        }
        else {
            return run;
        }
    }
}

Trigger:
trigger CreateClientTenant on Lease_Summary__c (after insert) {
    
    if(CheckRecursive.runOnce()) {
        // Logic 
        integer i = 1;
        List<Tenant__c> lstTenant = new List<Tenant__c>();
        List<Client__c> lstClient = new List<Client__c>();  
        for(Lease_Summary__c objLease : trigger.new){
            system.debug('objLease.Rent_start_date__c'+objLease.Rent_start_date__c);
            If(objLease.Rent_start_date__c == DATE.TODAY()){
                Tenant__c objtenant = new Tenant__c();
                objtenant.Rent_Start_Date__c = objLease.Rent_Start_Date__c;
                objtenant.Lease_Summary__c = objLease.id;
                lstTenant.add(objtenant);
            
           }
           System.debug('lstTenant'+lstTenant);
           if(lstTenant.size()>0){
              insert lstTenant;
           }
        }
    }
}

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
Amit Jadhav 13Amit Jadhav 13
Hey Khan Anas,

Your Code Works fine But When in deleted child record then new child record automatically created please help me.


Thanks and Regards,
Amit Jadhav
Deepali KulshresthaDeepali Kulshrestha
Hi Amit,

I have gone through your problem. This problem occurs due to when the tenant is inserted the Lease_Summary__c object is update and your trigger is working on after update condition that's why it runs again and again. For that you have to make on the boolean
flag to check the trigger does not run more than once at a time.

Please try the code given below:-
trigger CreateClientTenant on Lease_Summary__c (after update) {
    Boolean flag = true;
    if(flag == true)
    {
        integer i = 1;
        List<Tenant__c> lstTenant = new List<Tenant__c>();
        List<Client__c> lstClient = new List<Client__c>();  
        for(Lease_Summary__c objLease : trigger.new){
            system.debug('objLease.Rent_start_date__c'+objLease.Rent_start_date__c);
            If(objLease.Rent_start_date__c == DATE.TODAY()){
                Tenant__c objtenant = new Tenant__c();
                objtenant.Rent_Start_Date__c = objLease.Rent_Start_Date__c;
                objtenant.Lease_Summary__c = objLease.id;
                lstTenant.add(objtenant);
            
            }
            system.debug('lstTenant'+lstTenant);
            insert lstTenant;
        }
        flag = false;
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com