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
krish4ukrish4u 

Recursive Trigger

Hi Folks,

There is a after update trigger on account object. In trigger, there is a date field we are checking the date is changed or not if it is changed we are doing some process.

The trigger is executed 5 times in the transaction.

First time debugs:
Old value --- 11/25/2015
New value --- 11/26/2015  dates are different then class is executed.

Second time:
Old value --- 11/26/2015
New value --- 11/26/2015

Third time:
Old value --- 11/25/2015
New value --- 11/26/2015 -- dates are changed then class is executed. but in the second transaction the new date is override the old date but the third time getting different values same as first transaction values. Is it recursive or some thing wrong in the code? There is no field update and not assiging in the code for the field but the update is happened from the Job.

Fourth time:
Old value --- 11/26/2015
New value --- 11/26/2015

Fiftth time:
Old value --- 11/26/2015
New value --- 11/26/2015

Please help on this.

Thanks,
Krish
JeffreyStevensJeffreyStevens
I would guess that you're doing an update dml in the trigger - right?

Can you post your trigger code?
krish4ukrish4u
No we are not doing any update in the trigger we are just passing the values to other system, thats it. but other tirggers may contain the dml statements but not on the field.

Thanks,
Krish
Amit Chaudhary 8Amit Chaudhary 8
Please check below post . I hope that will help you
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

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.

Apex Class with Static Variable
public class ContactTriggerHandler
{
     public static Boolean isFirstTime = true;
}
Trigger Code
trigger ContactTriggers on Contact (after update)
{
    Set<String> accIdSet = new Set<String>(); 
    if(ContactTriggerHandler.isFirstTime)
    {
        ContactTriggerHandler.isFirstTime = false;
         for(Contact conObj : Trigger.New)
  {
            if(conObj.name != 'Test') 
     {
                accIdSet.add(conObj.accountId);
            }
         }
   // any code here
    }
}




 
MagulanDuraipandianMagulanDuraipandian
Check this example to solve it - http://www.infallibletechie.com/2012/08/recursive-triggers-in-salesforce.html
ManojjenaManojjena
Hi Krish,

Belwo is the article which is really good to handle  recursive trigger.

https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)
Thanks 
Manoj
krish4ukrish4u
Hi All,

Thanks for your suggestions. I know how to stop the execution second time, but if we see thtat in the third execution of the trigger we are getting the first execution values(actual values). Is it standard behaviour of the salesforce for recursive trigger? 

Thanks,
Krish