1

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

1
  • What error did you get? These two triggers are going to never end, every time one object is created it invokes the other trigger, and vice versa. Jan 14, 2014 at 3:11

3 Answers 3

3

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

1
  • You are right it s very practical solution.
    – user5204
    Jan 16, 2014 at 22:13
3

Your triggers are going to create an infinite loop of record creation.

  1. Create an Opportunity
  2. Trigger: Create an Order
  3. Trigger: Create an Opportunity
  4. Trigger: Create an Order
  5. ...

You want to think about how you want to architect your solution so it isn't quite as open ended. Is there a status or flag that you want to set on the new records to prevent this infinite creation from happening on the subsequent records (that will let you set some sort of criteria in your trigger)?

7
  • I understand I wanted to those triggers bulk, didnt think about status or flag, any coding solution that you can suggest me?
    – user5204
    Jan 14, 2014 at 3:30
  • Is there any way to differentiate an automatically created Opportunity/Order from a manually created one in your system? Jan 14, 2014 at 3:38
  • I was thinking about this but I cant find much difference, for example Opportunity object that creating automatically takes its name from Order object that related to (Opp.Name=OrderObj.Name+'-Opportunity';), something like this?
    – user5204
    Jan 14, 2014 at 4:00
  • You could check the suffix of the Name for -Order or -Opportunity using if (!OppObj.Name.endsWith('-Opportunity')) Jan 14, 2014 at 4:13
  • How can I use it in my trigger, is there only this code?
    – user5204
    Jan 14, 2014 at 4:28
0
  make a class
 declare variable\
 public stic  boolean tigerControl=true;

in you trigger
trigger CreateaOpportunity on Order__c(after insert){ 

if(className.tigerControl)
{

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)
        className.tigerControl=false
        insert(OppList);
    }
}
}

You must log in to answer this question.