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
Frank CarterFrank Carter 

After insert trigger error: maximum trigger depth exceeded

Hi,
I have a problem with an apex class. I have created an object related to opportunity, “billing detail”.
The result I want to reach is the creation of multiple child object, Billing details, according the number of months that I Have from the difference to “End of billing” and “Created date” divided for a number. The number is 2 if the billing is two-monthly, 1 if is monthly, 3 if is quarterly, 12 for annual. I have an after insert trigger that call an apex class(Helper).
Apex class:
public class Helper {
    public static void createBD(Billing_Detail__c bd){ //method to create billing details
        
        List <Billing_Detail__c> listBD = new List <Billing_Detail__c> ();
        Integer i=0;
        Integer K=0;
        
        if (bd.Billing_Period__c=='two-monthly') {
            k=2;

// convert from dateTime to date
Date myDate = date.newinstance(bd.CreatedDate.year(), bd.CreatedDate.month(), bd.CreatedDate.day());
       
             
			
      if(((bd.Billing_Type__c=='FEE')||(bd.Billing_Type__c=='PPU'))&& bd.Billing_Period__c=='two-monthly'){
          
        i= myDate.monthsBetween(bd.End_of_Billing__c)/2;//number of month divided 2
      }
      
    //creation
        for(Integer n =0; n<i-1; n++){//-1 I need of this otherwise it creates one more instance 
            if(i!=1){
            Billing_Detail__c newBd = new Billing_Detail__c ();
            
            newBd.Amount__c=bd.Amount__c;
            newBd.Billing_Date__c=bd.Billing_Date__c;
            newBd.Billing_Type__c=bd.Billing_Type__c;
            newBd.Billing_Status__c=bd.Billing_Status__c;
            newBd.Billing_Period__c= bd.Billing_Period__c;
            newBd.Product_Line__c=bd.Product_Line__c;
            newBd.Product__c=bd.Product__c;
            newBd.End_of_Billing__c=bd.End_of_Billing__c;     
            newBd.Opportunity_billing__c=bd.Opportunity_billing__c;
            newBd.Monthly_Forecast__c= bd.Monthly_Forecast__c.addMonths(k);
            k+=2;
            listBD.add(newBd);
            }
        }
        insert listBD;
    }
        
       // I reapet for other cases
else if (bd.Billing_Period__c=='quarterly') {
            k=3;
                
        Date myDate = date.newinstance(bd.CreatedDate.year(), bd.CreatedDate.month(), bd.CreatedDate.day());
       

      
    	if (((bd.Billing_Type__c=='FEE')||(bd.Billing_Type__c=='PPU'))&& bd.Billing_Period__c=='quarterly'){
        i= myDate.monthsBetween(bd.End_of_Billing__c)/3;
        }
                
    
        for(Integer n =0; n<i-1; n++){
            if(i!=1){
            Billing_Detail__c newBd = new Billing_Detail__c ();
            
            newBd.Amount__c=bd.Amount__c;
            newBd.Billing_Date__c=bd.Billing_Date__c;
            newBd.Billing_Type__c=bd.Billing_Type__c;
            newBd.Billing_Status__c=bd.Billing_Status__c;
            newBd.Billing_Period__c= bd.Billing_Period__c;
            newBd.Product_Line__c=bd.Product_Line__c;
            newBd.Product__c=bd.Product__c;
            newBd.End_of_Billing__c=bd.End_of_Billing__c;     
            newBd.Opportunity_billing__c=bd.Opportunity_billing__c;
            newBd.Monthly_Forecast__c= bd.Monthly_Forecast__c.addMonths(k);//parametro k per data scalata
            k+=3;
            listBD.add(newBd);
            }
        }
        insert listBD;
    }
else if (bd.Billing_Period__c=='annual') {
            k=12;
                
        Date myDate = date.newinstance(bd.CreatedDate.year(), bd.CreatedDate.month(), bd.CreatedDate.day());
       
             

        if (((bd.Billing_Type__c=='FEE')||(bd.Billing_Type__c=='PPU'))&& bd.Billing_Period__c=='annual'){
        i= myDate.monthsBetween(bd.End_of_Billing__c)/12;
        }
        
    
        for(Integer n =0; n<i-1; n++){
            if(i!=1){
            Billing_Detail__c newBd = new Billing_Detail__c ();
            
            newBd.Amount__c=bd.Amount__c;
            newBd.Billing_Date__c=bd.Billing_Date__c;
            newBd.Billing_Type__c=bd.Billing_Type__c;
            newBd.Billing_Status__c=bd.Billing_Status__c;
            newBd.Billing_Period__c= bd.Billing_Period__c;
            newBd.Product_Line__c=bd.Product_Line__c;
            newBd.Product__c=bd.Product__c;
            newBd.End_of_Billing__c=bd.End_of_Billing__c;     
            newBd.Opportunity_billing__c=bd.Opportunity_billing__c;
            newBd.Monthly_Forecast__c= bd.Monthly_Forecast__c.addMonths(k);
            k+=12;
            listBD.add(newBd);
            }
        }
        insert listBD;
    }
        else if (bd.Billing_Period__c=='monthly'){
            Date myDate = date.newinstance(bd.CreatedDate.year(), bd.CreatedDate.month(), bd.CreatedDate.day());
             if(((bd.Billing_Type__c=='FEE')||(bd.Billing_Type__c=='PPU'))&& bd.Billing_Period__c=='monthly'){
        i= myDate.monthsBetween(bd.End_of_Billing__c);
             System.debug('i='+i);
        }
             
            for(Integer n =0; n<i-1; n++){
            if(i!=1){
            Billing_Detail__c newBd = new Billing_Detail__c ();
            System.debug('n='+n);
            
            newBd.Amount__c=bd.Amount__c;
               System.debug('amount'+newBd.Amount__c); 
            newBd.Billing_Date__c=bd.Billing_Date__c;
            newBd.Billing_Type__c=bd.Billing_Type__c;
            newBd.Billing_Status__c=bd.Billing_Status__c;
            newBd.Billing_Period__c= bd.Billing_Period__c;
            newBd.Product_Line__c=bd.Product_Line__c;
            newBd.Product__c=bd.Product__c;
            newBd.End_of_Billing__c=bd.End_of_Billing__c;     
         	newBd.Opportunity_billing__c=bd.Opportunity_billing__c;
            newBd.Monthly_Forecast__c= bd.Monthly_Forecast__c.addMonths(n+1);
                 System.debug('monthly forecast'+newBd.Monthly_Forecast__c);
            
            listBD.add(newBd);
                
            }
            
        }
        insert listBD;
        }
    
        }

}
the trigger :
trigger bd on Billing_Detail__c (after insert) {

    for (Billing_Detail__c bd : Trigger.new) {
        if(trigger.size==1){
        Helper.createBD(trigger.new[0]);
        }
    }
}
The ERROR:
"Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger bd caused an unexpected exception, contact your administrator: bd: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, bd: maximum trigger depth exceeded Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert Billing_Detail trigger event AfterInsert: []: Class.Helper.createBD: line 137, column 1"

The error appears only for i=2, in other cases it works well; I don't understand why and how I can resolve.
Can you help me?

Thanks


 
Best Answer chosen by Frank Carter
PawanKumarPawanKumar
Hi Frank,

The trigger is getting called again and again due to some update/insert. So to avoid this recursive call of the trigger. Please do the following changes.

1. Create below class.
public class CheckRecursive { 
   public static boolean firstRun = true; 
}

2. Trigger modified as below.
trigger bd on Billing_Detail__c (after insert) {
    if(CheckRecursive.firstRun){
        CheckRecursive.firstRun=false;

        for (Billing_Detail__c bd : Trigger.new) {
            if(trigger.size==1){
            Helper.createBD(trigger.new[0]);
            }
        }
    }
}

Please mark best if it helps you. Thanks in advance.

Regards,
Pawan Kumar