+ Start a Discussion
Paul.FoxPaul.Fox 

Problem with Triggers and Loop

I have two objects, A and B, which are linked via a lookup field. B is the sub-object in this example.

 

Now even though they're linked with a lookup, I want one key field to be synced between the two objects. I want users to be able to edit from either record.

 

However, here's my problem:

When someone changes the value on A, the trigger on A fires, and updates B. This causes a chain reaction which causes the trigger on B to fire, and update A again. I'm not actually sure how many times it goes through the loop before failing.

 

This actually works for records where there are only a few B records, but my test cases fail because of too many DML rows. I thought this was a problem with just the number of records, so I created some Asynchronous Apex to handle the actual updating. However, now I get the error that a future method cannot call a future method (since one future method is causing the second trigger to fire which also uses a future method).

 

I have tried and failed with the following:

Trigger.isExecuting - this always returns true since they are triggers, so I can't use this

Custom checkbox which says Updated from Sync - This works for the initial update, but I can never set this field back to false, because when I do (via trigger or workflow rule) it fires the other trigger again, and I get the same errors.

 

Any ideas on how to sync one field between two objects?

Best Answer chosen by Admin (Salesforce Developers) 
Jeremy_nJeremy_n

Try setting a static variable in another class. For example,

 

public class util {
    public static Boolean IsSynching = False;

}

 

Then in each of your Triggers, test for util.IsSynching. If it's true, skip the update. If it's false, set it to true and do your updating then.

 

if (!util.IsSynching) {
    util.IsSynching = true;

    //update fields as necessary here

}

 

Good luck,

 

Jeremy

All Answers

PragadheeshwariPragadheeshwari

can you please explain in detail what all are the things you need to change like....

 

 

For ex.

 

In Object B, whenever a change in "F1"(Field in Object B) ,need to chage in Object A field "F2"

 

Like this explain a bit more

Paul.FoxPaul.Fox

There are several fields that I'm updating, but for the sake of this discussion let's just keep it simple.

 

Let's just say there is one field on both objects that I want to always be the same value. It is a picklist. Whenever it is changed on Object A I want it to be updated on Object B with the same value.

Jeremy_nJeremy_n

Try setting a static variable in another class. For example,

 

public class util {
    public static Boolean IsSynching = False;

}

 

Then in each of your Triggers, test for util.IsSynching. If it's true, skip the update. If it's false, set it to true and do your updating then.

 

if (!util.IsSynching) {
    util.IsSynching = true;

    //update fields as necessary here

}

 

Good luck,

 

Jeremy

This was selected as the best answer
Paul.FoxPaul.Fox

Thanks, that worked. I had to set it again in my Future method, since sometimes that was still running when the method called.

 

In my sync class I added this:

public static boolean firstTrigger = true;

 

Then in each trigger I put:

if(sync.firstTrigger == true){
    sync.firstTrigger = false;
    do something
}

 

I also added the false line to my future method, since I guess sometimes the trigger was done and the future method would call another trigger, and the variable was reset. So the future method basically keeps the static variable alive.