Maximum trigger depth exceeded

"Maximum trigger depth exceeded" is an error message in Salesforce that occurs when there's a recursive trigger execution that exceeds the platform's governor limit.

Let's consider a scenario with a Contact object with a custom field called Total_Contacts__c. This field represents the total number of contacts associated with an account. We aim to ensure that this field is always up-to-date whenever a new contact is created, updated, or deleted. 

To achieve this, we implement triggers on the Contact object that update the Total_Contacts__c field on the related Account object. Unfortunately, due to a mistake in the trigger logic, the triggers recursively update contacts and accounts, causing an error.

Here's an example

trigger UpdateTotalContacts on Contact (after insert, after update, after delete) {
    if (Trigger.isAfter) {
        if (Trigger.isInsert || Trigger.isUpdate) {
            List<Account> accountsToUpdate = new List<Account>();
            for (Contact con : Trigger.new) {
                // Update related account's Total_Contacts__c field
                Account acc = [SELECT Id, Total_Contacts__c FROM Account WHERE Id = :con.AccountId];
                acc.Total_Contacts__c = [SELECT COUNT() FROM Contact WHERE AccountId = :con.AccountId];
                accountsToUpdate.add(acc);
            }
            update accountsToUpdate;
        } else if (Trigger.isDelete) {
            List<Account> accountsToUpdate = new List<Account>();
            for (Contact con : Trigger.old) {
                // Update related account's Total_Contacts__c field
                Account acc = [SELECT Id, Total_Contacts__c FROM Account WHERE Id = :con.AccountId];
                acc.Total_Contacts__c = [SELECT COUNT() FROM Contact WHERE AccountId = :con.AccountId];
                accountsToUpdate.add(acc);
            }
            update accountsToUpdate;
        }
    }

}

We have implemented triggers for after insert, after update, and after delete events on the Contact object. These triggers update the Total_Contacts__c field on the related Account object whenever a new contact is added, an existing contact is updated, or a contact is deleted. However, the current implementation of the trigger queries the Account object within the loop for each contact, which results in multiple SOQL queries and DML operations.

This trigger can cause a recursive loop, in which updating the Total_Contacts__c field on the Account triggers the same trigger again, resulting in multiple trigger invocations and eventually exceeding the maximum trigger depth limit.

We must revise the trigger logic to prevent recursive updates and optimize the queries to handle multiple contacts efficiently to resolve the issue. It is also important to conduct thorough unit testing to verify that the triggers function properly and do not surpass the governor's limits.

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach