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
force noviceforce novice 

Recursive Trigger

what is a recursive trigger?Please explain me with an example...
Best Answer chosen by force novice
SFDC_DevloperSFDC_Devloper
Hi,

A recursive trigger is one that will end up calling itself again. For example, if you have a trigger on accounts that updates contacts, and a trigger on contacts that updates accounts, it is possible that the account update could update contacts that would in turn update the account... this will cause errors. The specific solution to this depends on the situation, but usually it will suffice to have a "trigger guard" that will keep the trigger from running a second time. It can look like this:

<pre>
public class TriggerGuard {
  public static boolean accountguard, contactguard;
  static {
accountguard = contactguard = false;
  }
}
</pre>

-------------------------------------------------------------------------------
<pre>
trigger upAcc on account (after update) {
  if(!TriggerGuard.ContactGuard) {
    TriggerGuard.AccountGuard = true;
    // update the contacts.
  }
}
</pre>

--------------------------------------------------------------------------------------
<pre>
trigger upCon on Contact (after update) {
  if(!TriggerGuard.AccountGuard) {
    TriggerGuard.ContactGuard = true;
    // Do something to update the accounts.
  }
}
</pre>

In this manner, each trigger advises the other if it is already in progress, thus preventing a recursive call that will ultimately fail.

Thanks,
Allways Cool


All Answers

bob_buzzardbob_buzzard
Check out this blog post (not mine) : http://www.infallibletechie.com/2012/08/recursive-triggers-in-salesforce.html
SFDC_DevloperSFDC_Devloper
Hi,

A recursive trigger is one that will end up calling itself again. For example, if you have a trigger on accounts that updates contacts, and a trigger on contacts that updates accounts, it is possible that the account update could update contacts that would in turn update the account... this will cause errors. The specific solution to this depends on the situation, but usually it will suffice to have a "trigger guard" that will keep the trigger from running a second time. It can look like this:

<pre>
public class TriggerGuard {
  public static boolean accountguard, contactguard;
  static {
accountguard = contactguard = false;
  }
}
</pre>

-------------------------------------------------------------------------------
<pre>
trigger upAcc on account (after update) {
  if(!TriggerGuard.ContactGuard) {
    TriggerGuard.AccountGuard = true;
    // update the contacts.
  }
}
</pre>

--------------------------------------------------------------------------------------
<pre>
trigger upCon on Contact (after update) {
  if(!TriggerGuard.AccountGuard) {
    TriggerGuard.ContactGuard = true;
    // Do something to update the accounts.
  }
}
</pre>

In this manner, each trigger advises the other if it is already in progress, thus preventing a recursive call that will ultimately fail.

Thanks,
Allways Cool


This was selected as the best answer