Friday 17 October 2014

How to avoid Recursive Trigger in Salesforce

To resolve recursive trigger please follow the below steps,

     1. Create a class with the Boolean variable and set this as true to avoid Recursive trigger.
  1. public class RecursiveHandler
  2. {
  3.      public static Boolean isNotRecursive = true;
  4. }

    2. Check the Boolean flag value before process your condition and set the flag value to false at the beginning of your process. Once if you set this false, the recursion will stop.
  1. trigger DuplicateRecordMaker on Account (after insert, after update)
  2. {
  3.     List<Account> accounts = new List<Account>();
  4.    
  5.     if(RecursiveHandler.isNotRecursive)
  6.     {
  7.         RecursiveHandler.isNotRecursive = false;
  8.        
  9.         for(Account currentAccount : Trigger.New)
  10.         {
  11.             Account acc = new Account();
  12.             acc.Name = currentAccount.Name;
  13.             accounts.add(acc);
  14.         }
  15.        
  16.         insert accounts;
  17.     }
  18. }

Hope the above coding will helpful to solve recursive trigger error.

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...