[SalesForce] Trigger on A inserts B, Trigger on B inserts A – how to prevent errors

I have 2 triggers, one is on custom Order, other is on Opportunity. When I make both of them is active and try to run these objects, system gives error, how can I solve this problem? My triggers are:

trigger CreateaOpportunity on Order__c(after insert){ 
    if(trigger.IsInsert){
        List<Opportunity> OppList = new List<Opportunity>();
        for(Order__c OrderObj : Trigger.new){
            Opportunity Opp = new Opportunity();
            Opp.Name=OrderObj.Name+'-Opportunity';
            Opp.CloseDate=OrderObj.Close_Date__c;
            Opp.StageName=OrderObj.StageName__c;
            Opp.Orders__c = OrderObj.Id; 
            OppList.add(Opp);
        }
        if(OppList.size()>0)
        insert(OppList);
    }
}



trigger CreateOrder on Opportunity(after insert){
    if(trigger.IsInsert){
        List<Order__c> OrdList = new List<Order__c>();
        for(Opportunity OppObj : Trigger.new){
            Order__c Order = new Order__c(
            Name=OppObj.Name+'-Order',
            Close_Date__c=OppObj.CloseDate,
            StageName__c=OppObj.StageName,
            Opportunities__c=OppObj.Id
            );
            OrdList.add(Order);
        }
        if(OrdList.size()>0)
        insert OrdList;
    }
}

I get an error like this:

Error: Invalid Data. Review all error messages below to correct your
data. Apex trigger CreateaOpportunity caused an unexpected exception,
contact your administrator: CreateaOpportunity: execution of
AfterInsert caused by: System.DmlException: Insert failed. First
exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
CreateOrder: execution of AfterInsert caused by: System.DmlException:
Insert failed. First exception on row 0; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateaOpportunity: execution of
AfterInsert caused by: System.DmlException: Insert failed. First
exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
CreateOrder: execution of AfterInsert caused by: System.DmlException:
Insert failed. First exception on row 0; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateaOpportunity: execution of
AfterInsert caused by: System.DmlException: Insert failed. First
exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
CreateOrder: execution of AfterInsert caused by: System.DmlException:
Insert failed. First exception on row 0; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateaOpportunity: execution of
AfterInsert caused by: System.DmlException: Insert failed. First
exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
CreateOrder: execution of AfterInsert caused by: System.DmlException:
Insert failed. First exception on row 0; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateaOpportunity: execution of
AfterInsert caused by: System.DmlException: Insert failed. First
exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
CreateOrder: execution of AfterInsert caused by: System.DmlException:
Insert failed. First exception on row 0; first error: STRING_TOO_LONG,
Order Name: data value too large:
order7-Opportunity-Order-Opportunity-Order-Opportunity-Order-Opportunity-Order-Opportunity-Order
(max length=80): [Name] Trigger.CreateOrder: line 14, column 1: []
Trigger.CreateaOpportunity: line 13, column 1: [] Trigger.CreateOrder:
line 14, column 1: [] Trigger.CreateaOpportunity: line 13, column 1:
[] Trigger.CreateOrder: line 14, column 1: []
Trigger.CreateaOpportunity: line 13, column 1: [] Trigger.CreateOrder:
line 14, column 1: [] Trigger.CreateaOpportunity: line 13, column 1:
[] Trigger.CreateOrder: line 14, column 1: []:
Trigger.CreateaOpportunity: line 13, column 1

Best Answer

You could resolve this by using a static Boolean variable in a class. Quoting from the success community

In order to avoid the situation of recursive call, make sure your trigger is getting executed only one time. To do so, you can create a class with a static boolean variable with default value true.

In the trigger, before executing your code keep a check that the variable is true or not.

Once you check make the variable false.

A detailed explanation with sample code is available on the link

Related Topic