+ Start a Discussion
ColealarsColealars 

Help with fixing error: maximum trigger depth exceeded on trigger

Can someone tell me how to fix the error below on my trigger:

 

Error:Apex trigger ContractChangeQuote caused an unexpected exception, contact your administrator: ContractChangeQuote: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0QL700000000zWnGAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContractChangeQuote: maximum trigger depth exceeded Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote

trigger ContractChangeQuote on Quote (after update) {
If(Trigger.isUpdate){
     //check the opprtunites in the trigger to see if the Contract Term changed, if so add them to a Set
     Set<Id> UpdatedOppIds = new Set<Id>();
        string term;
        for(Integer i=0;i<trigger.new.size();i++){  
            if(trigger.old[i].Contract_Term__c == null){
                term = '';
            } else {
                term = trigger.old[i].Contract_Term__c;
            }    
            //if(trigger.new[i].Contract_Term__c <> trigger.old[i].Contract_Term__c){
            if(trigger.new[i].Contract_Term__c <> term) {
                UpdatedOppIds.add(Trigger.new[i].id);
            }
        }
         
        //Process any opps that have changed
        if(UpdatedOppIds.size() > 0){
            //get all of the quote Line items tied to opportunities in this trigger
            List<Quote> qliList = [select Id, Name, Contract_Term__c, (select Id, ContractTerm__c, QuoteId from QuoteLineItems) from Quote where Id IN :UpdatedOppIds];
             
            //create list to hold quote line items that need updating.
            List<QuoteLineItem> qliUpdateList = new List<QuoteLineItem>();
             
            //loop through opportunites and update all of the quote line items. add line items to update list.
            for(Quote o : qliList){
                for(QuoteLineItem qli : o.QuoteLineItems){
                    qli.ContractTerm__c = o.Contract_Term__c;
                    qliUpdateList.add(qli);
                }
            }
             
            //update the line items
            if(!qliUpdateList.isEmpty()){
                update qliUpdateList;
            }
        }
    }
}

 

trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6] Quote trigger event AfterUpdate for [0Q0700000004jf6]: []: Trigger.ContractChangeQuote: line 36, column 1

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Your trigger is causing recursion on the same quote over and over again. You should break the recursion chain with a flag that stops it from happening. This is done with a static variable in a class, like this:

 

public class Recursion {
  public static boolean quoteRecursion;
  static {
    quoteRecursion = false;
  }
}

Your trigger should then check this flag, abort if true, or set it and continue if false:

 

trigger ... {
  if(Recursion.quoteRecursion)
    return;
  Recursion.quoteRecursion = true;
  ...
}

 

All Answers

sfdcfoxsfdcfox

Your trigger is causing recursion on the same quote over and over again. You should break the recursion chain with a flag that stops it from happening. This is done with a static variable in a class, like this:

 

public class Recursion {
  public static boolean quoteRecursion;
  static {
    quoteRecursion = false;
  }
}

Your trigger should then check this flag, abort if true, or set it and continue if false:

 

trigger ... {
  if(Recursion.quoteRecursion)
    return;
  Recursion.quoteRecursion = true;
  ...
}

 

This was selected as the best answer
ColealarsColealars
Thanks that worked.