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
sumit dsumit d 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger_FBGFamily: maximum trigger depth

Hi All,
       i am getting this error:-
 System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger_FBGFamily: maximum trigger depth exceeded FBG_Family        i have a requirement in which I have a custom object called  Family which is a junction object between two accounts.
   lets assume we have two accounts, Account A, Account B. Lets create an  Family record between these two. 
  Every time a family record is inserted,
  we have to create another family record and have to swap the  Sister_Company__c and Sister_Company2__c Ids in the new record.
  i created a trigger and trigger helper  given below:-
  trigger Trigger_FBGFamily on FBG_Family__c (before insert, before update, 
                                            before delete, after insert, 
                                            after update, after delete) {
                                                
    FBGFamilyTriggerHelper.newFBGFamily = trigger.new;
    FBGFamilyTriggerHelper.oldFBGFamily = trigger.old;
    FBGFamilyTriggerHelper.newMapFBGFamily = trigger.newMap;
    FBGFamilyTriggerHelper.oldMapFBGFamily = trigger.oldMap;   
                                                
       if(!FBGFamilyTriggerHelper.runTrigger) {
        return;
    }
                                              
                                                if( Trigger.isAfter ){
                                                    if(Trigger.isInsert ){
                                                        FBGFamilyTriggerHelper.CreatingReverseFamilyRecords(trigger.new);   
                                                    }
                                                     
                                                }
                                            }
                    Helper class is given below:-
                    public class FBGFamilyTriggerHelper {
    public static List<FBG_Family__c> newFBGFamily = new List<FBG_Family__c>();
    public static List<FBG_Family__c> oldFBGFamily = new List<FBG_Family__c>();
    public static Map<Id, FBG_Family__c> newMapFBGFamily = new Map<Id, FBG_Family__c>();
    public static Map<Id, FBG_Family__c> oldMapFBGFamily = new Map<Id, FBG_Family__c>();
    
        public static boolean runTrigger = TRUE;

    Public static void CreatingReverseFamilyRecords(List<FBG_Family__c> FBGFamilyIds){
        List<FBG_Family__c> ListFamilyToInsert = New List<FBG_Family__c>();
         For(FBG_Family__c Family : FBGFamilyIds)
        {
            FBG_Family__c ReverseFamily = New FBG_Family__c();
            ReverseFamily.Sister_Company__c = Family.Sister_Company2__c;
            ReverseFamily.Sister_Company2__c = Family.Sister_Company__c;
            ListFamilyToInsert.Add(ReverseFamily);
        }
        if(ListFamilyToInsert.Size() > 0){
          Insert ListFamilyToInsert;
         }
        System.debug('ReverseFamily'+ListFamilyToInsert);
    }
}
How to solve this error?
Any suggestions?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sumit,

Greetings to you!

Maximum trigger depth exceeded exception in Salesforce occurs mainly due to recursion.

When you are creating an Apex code that recursively fires triggers due to insert/update/delete statement for more than 16 times. You will get the Maximum Trigger Depth Exceeded error.

Kindly implement a static variable in a class to avoid recursion.

Please refer to the below links which might help you further with the above issue.

http://www.infallibletechie.com/2014/05/how-to-avoid-recursive-trigger-in.html

http://sfdcsrini.blogspot.com/2014/06/what-is-maximum-trigger-depth-exceeded.html

http://nirmalchristopher.blogspot.com/2014/03/how-to-solve-cannotinsertupdateactivate.html

https://blog.internetcreations.com/2012/07/the-spouse-situation-and-avoiding-recursive-triggers-in-salesforce/#.XQI1NW8zbfY

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
sumit dsumit d
Hi Khan,
Thanks for replying i implemented the variable in helper class but still same error.
can you please tell me that what i have missed?