How can we avoid trigger get executed again and again (recursive trigger)? One way is to have a class with static variable and have boolean value and check it is true or false and changing the boolean according to executing the trigger value. Is there any other way other than this to handle recursive trigger in salesforce. Please guide me the best way to do ?
Another good way to avoid recursion is highlighted in Chapter 6 of Dan Applemans's excellent book, Advanced Apex Programming. This particular example is around recursion when looking for a specific field change which is very common in many triggers.
As mentioned in @BarCotters answer, you generally run your trigger on very specific criteria. A change in a field can trigger certain logic in your trigger. If you look at the Order of Operations for triggers, you can see that any workflow rules and field updates based on those rules are run after the both before and after triggers have run. It runs however prior to the changes actually being committed to the DB, which is where this can cause problems when detecting field changes.
So lets say we are using a very simple example where we have a trigger on the Opportunity object that looks for opportunities that have been Won, and based on that it creates a new custom object record.
This trigger will work fine, and will create a new custom object when you close an Opportunity. It will work fine that is, assuming you have no WFR's that have field updates on the Opportunity Object.
Let's say now I had a workflow rule on the Opportunity that When an Opp is closed, it changes the CloseDate of the Opportunity to Today. (A fairly simple and common WFR that many admins may add).
That simple change now Breaks my trigger. This same trigger will now actually create 2 custom objects when my opportunity is closed. This is due to the fact that the WFR rule is now firing the triggers one final time.
This is how you would think the logic works
This is not how it works though, even though the opp has been updated, it has not yet been committed to the Database, so in the second run of the trigger it still see's the opp and just being closed like below
This is actually how it works
This is where Dan Appleman's solution comes in. He advises to use a mechanism that actually checks for the 'correct old value'. This would allow the second run of the trigger to detect the value that was set in the first run of the trigger.
With this change, now the WFR rule does not break our trigger. Only 1 custom object is created when the opp is closed.
I know this was longwinded, but I hope it helps. I would highly getting this book as this was only a few pages worth of goodness, and this book is jam packed with great knowledge that I think all SFDC developers should have.
Heres the link again.
Your trigger should only be firing on very specific criteria. Make them as specific as possible by comparing old and new field values to make sure your criteria is met.
For example if you wanted to only fire when a contacts BirthDate changed then you could do the following:
Even though the trigger is updating the current Contact, we don't need to worry about recursion because:
If you want to limit each trigger to fire only once then what you described would be the solution. SalesForce document it here but it may be a valid scenario that triggers fire more than once in the same transaction, the issue is if it is an infinite loop. You will hit a Governor Limit if there is more than 16 recursions of the same trigger.