function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bhushan Singh 13Bhushan Singh 13 

I have two object and each object contains trigger for recursive execution , how to prevent recursive without using Boolean variable

Hi,
I have two object and each object contains trigger for recursive execution 
for e.g. Account has field amount and Contact has also field amount, Both has trigger, 
Trigger on Account if amount field will be update that time contact amount field should be update and
Trigger on Contact If amount field of Contact updating that time amount field of Account should be update. SO It is a Recursive trigger, How to prevent this recursive without usin any static boolean variable.
Please help me .
Best Answer chosen by Bhushan Singh 13
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) http://salesforce.stackexchange.com/questions/46790/how-to-avoid-recursive-trigger-other-than-the-classic-class-w-static-variable


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:
trigger ContactBeforeTrigger on Contact (before insert, before update, before delete) {
    if (Trigger.isUpdate) {
        for (Contact contact : Trigger.new) {
            if (contact.Birthdate != Trigger.oldMap.get(contact.Id).Birthdate) {
                contact.SomeField__c = 'DOB Changed';
            }
        }
    }
}
Even though the trigger is updating the current Contact, we don't need to worry about recursion because:
  • The trigger will only fire on updates due to the Trigger.isUpdate check
  • The trigger will only fire when the Birthdate is changed.
NOTE:- other then static method use new Map and Old Map

Let us know if this will help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) http://salesforce.stackexchange.com/questions/46790/how-to-avoid-recursive-trigger-other-than-the-classic-class-w-static-variable


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:
trigger ContactBeforeTrigger on Contact (before insert, before update, before delete) {
    if (Trigger.isUpdate) {
        for (Contact contact : Trigger.new) {
            if (contact.Birthdate != Trigger.oldMap.get(contact.Id).Birthdate) {
                contact.SomeField__c = 'DOB Changed';
            }
        }
    }
}
Even though the trigger is updating the current Contact, we don't need to worry about recursion because:
  • The trigger will only fire on updates due to the Trigger.isUpdate check
  • The trigger will only fire when the Birthdate is changed.
NOTE:- other then static method use new Map and Old Map

Let us know if this will help you

 
This was selected as the best answer
Bhushan Singh 13Bhushan Singh 13
Hi Amit,
It is working for me .
Thanks 
Bhushan