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
SakthidasanSakthidasan 

What situation Recursive Trigger Occurs

I create atfter update trigger on Account object.it takes all the contacts from account object and update the contact mailing Address,I create another after update trigger on Contact object.it monitor the last contact update.I got recursive .my question is ,what kind of situation recursive trigger occurs,why we use static lock.static lock use for both trigger ?or any one ?
here my code:

trigger AccountRecursiveTrigger on Account (after insert,after update) {
    if(checkRecursive.isFutureUpdate) {
        checkRecursive.isFutureUpdate=false;
    List<Contact> updateContact=new List<Contact>();
    List<Account> accounts=new List<Account>();
    accounts=[select id,Name,BillingStreet,BillingCity,BillingCountry,
              (select id,FirstName,LastName,MailingStreet,MailingCity,MailingCountry from Contacts) from Account where id IN:trigger.new];
    system.debug('cc::'+accounts);
    for(Account acc:accounts){
        for(Contact con:acc.Contacts){
            
             con.MailingStreet=acc.BillingStreet;
             con.MailingCity=acc.BillingCity;
            con.MailingCountry=acc.BillingCountry;
            updateContact.add(con);
            
        }
        
    }
             
    update updateContact;
     
         
    }
}

trigger ContactRecursiveTrigger on Contact (after insert,after update) {
     System.debug('afterif::'+checkRecursive.isFutureUpdate);
    //if(checkRecursive.isFutureUpdate=true) {
        System.debug('afterif::'+checkRecursive.isFutureUpdate);
    Map<id,Account> updateAccount=new Map<id,Account>();
    for(Contact con:trigger.new){
        if(con.AccountId !=null){
            
            Account acc=new Account(id=con.AccountId,Contact_Last_Updates__c=system.now());
            updateAccount.put(con.AccountId, acc);
        }
        
    }
    System.debug('valuess:'+updateAccount.values());
       // checkRecursive.isFutureUpdate=false;
    update updateAccount.values();
         System.debug('change::'+checkRecursive.isFutureUpdate);
        
//}
}
 
Best Answer chosen by Sakthidasan
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html


Problem :-  
1) Many Developers face recursive trigger , or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.

2) You want to write a trigger that creates a new record ; however, that record may then cause another trigger to fire, which in turn causes another to fire, and so on. 
 
Solution :-

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.

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html


Problem :-  
1) Many Developers face recursive trigger , or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.

2) You want to write a trigger that creates a new record ; however, that record may then cause another trigger to fire, which in turn causes another to fire, and so on. 
 
Solution :-

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.

 
This was selected as the best answer
{tushar-sharma}{tushar-sharma}
Hi,
We have multiple ways to avoid recursive triggers. They all depend on your use case, While one solution may work but will not work for others. Check the below links, It has multiple ways to avoid recursive trigger: https://newstechnologystuff.com/2020/05/28/avoid-recursive-trigger-in-salesforce/

Many Developers face recursive trigger or recursive update trigger. For example in 'after update' trigger, the Developer is performing update operation, and this leads to the recursive call. Or if we have WF field update sometimes they also contribute to recursive triggers.