0

I have one Trigger for Tas_Project Object and another Trigger for Proj object.

When I create a new project for Tas_Project object then Trigger for Tas_Project Object will create the same project on Proj object.

When I create a new project for Proj object then Trigger for Proj Object will create the same project on Tas_Project object.

If one trigger is active and another is in inactive then project will create.

If both Triggers are active, then its not working..

Is it possible both triggers are in active state and project should create?

------------------------Trigger 1-------------------

trigger createProjekt on Tas__Project__c (after insert) {
    List <Projekt__c> proInsert = new List<Projekt__c>();
    for (Tas__Project__c tr : Trigger.new){
        Projekt__c p = new Projekt__c();
        p.Name = tr.Name;
        p.Price_Book__c = tr.Price_Book__c;
        proInsert.add(p); 
    }
    try{
        insert proInsert;
    }
    catch(system.Dmlexception e) {
        system.debug (e);
    }
}

------------------------Trigger 2------------------

trigger createTaskRayProj on Projekt__c (after insert) {
    List <Tas__Project__c> proInsert1 = new List<Tas__Project__c>();
    for (Projekt__c p : Trigger.new){
        Tas__Project__c tr = new Tas__Project__c();
        tr.Name = p.Name;
        tr.Price_Book__c = p.Price_Book__c;
        proInsert1.add(tr); 
    }
   try{
        insert proInsert1;
    }
    catch(system.Dmlexception e) {
        system.debug (e);
    }
}
2
  • Without your code and an explanation of what you already tried, I don't think you can get much help. Please provide the code of your conflicting triggers. Jul 19, 2019 at 9:45
  • Dear Florian Heer please check the code added..
    – Vishwas S
    Jul 19, 2019 at 10:14

1 Answer 1

2

Your triggers are recursing into one another. Each trigger inserts the other trigger's object, which then runs the other trigger, which inserts the first trigger's object, which runs the first trigger again, and so on until you hit the stack limit.

There are two possible solutions here.

  • Code your triggers to check whether they need to take action or not before they perform any DML. For example, if one object looked up to the other and you populated the lookup on creation, you could look to see if that field contained a value before you created the other record, blocking recursion. Here, you have no link between the two records save a common name. You could choose to query for records in the other object that have the same name, but that risks false positives. You could also choose to add a lookup field from one object to the other.

  • Use a static variable to guard against the recursion. This is one of the few areas where a static Boolean is (a) right solution. You'd do something like

    public class ProjectTriggerHandler {
        static Boolean otherTriggerIsRunning = false;
    }
    

    Then, each trigger guards its DML with that variable:

    ProjectTriggerHandler.otherTriggerIsRunning = true;
    insert proInsert1;
    ProjectTriggerHandler.otherTriggerIsRunning = false;
    

    and wraps all of its logic in a check for that value:

    if (!ProjectTriggerHandler.otherTriggerIsRunning) {
        // Do work
    }
    

    That will prevent the back-and-forth recursion between the two triggers by preventing each from running due to the other's DML operations.

Other notes:

  • You need to get rid of your exception-swallowing catch blocks. Swallowing exceptions makes debugging much more difficult. They are exceptions for a reason!
  • You should consider adopting a trigger handler framework.
1
  • Thanks David Reed...
    – Vishwas S
    Jul 20, 2019 at 5:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .