Friday 17 October 2014

How to avoid maximum trigger depth exceeded exception in Salesforce

As Per Salesforce Governor Limits, Total stack depth for any Apex invocation that recursively fires triggers due to insert, update, or delete statements size is 16.  If you go beyond this limit you will get “Maximum Trigger Depth Exceeded error”.

The below code is one of the example of getting this error,

  1. trigger DuplicateRecordMaker on Account (after insert, after update)
  2. {
  3.     List<Account> accounts = new List<Account>();
  4.     for(Account currentAccount : Trigger.New)
  5.     {
  6.         Account acc = new Account();
  7.         acc.Name = currentAccount.Name;
  8.         accounts.add(acc);
  9.     }
  10.     insert accounts;
  11. }
Reason for getting such error,

            My Trigger concept is whenever account record is created, the same record should be created as duplicate record. After insert a record the above trigger will fire then duplicate record will process after that trigger will call recursively, this cause a Maximum Trigger Depth Exceeded error.

Check out the below link to resolve this error,

 How to avoid Recursive Trigger in Salesforce




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...