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
AlSawtoothAlSawtooth 

Maximum Trigger Depth Exceeded

Hello,

I'm getting an error that I think is recursion-based and I'm not sure how to fix it. Basically, I'm trying to update the DD field on my DDCustom__c object with a value from my Opportunity field UpdateDD__c (which is a lookup).
 
trigger UpdateDD2 on DDCustom__c (after update, after insert) {

  List<DDCustom__c> des = [select Id, DD__r.Id, Opportunity__r.Id from DDCustom__c where Id in :Trigger.newMap.keySet()];
  List<Opportunity> opp = [select Id, UpdateDD__c, UpdateDD__r.Id from Opportunity where Id = : des[0].cv__Opportunity__r.Id and UpdateDD__c != null];
  List<DDCustom__c> des2 = new List<DDCustom__c>();
  
  Set<DDCustom__c> desSet = new Set<DDCustom__c>();
  
  for(DDCustom__c d : des){
  
      if(des2.size()>0){
          desSet.addAll(des2);
          }
  
      if((! desSet.contains(d)) && (opp[0].UpdateDD__c != null) && (d.DD__r.Id != opp[0].UpdateDD__r.Id)){
           d.DD__r.Id = opp[0].UpdateDD__r.Id;
              des2.add(d);
              }
      }
 
   update des2;
   }

 
Crystal Rochlitz 4Crystal Rochlitz 4
You seem to be declaring des2 as a new list, but not adding anything to it. Then on line 11 checking if the list size is greater than 0 which it won't be. Take out the size check before filling the set (or just skip this step totally, unless you want to make sure there is only one unique instance of each DDCustom__c). Then, after you add 'd' to the desSet you check if !desSet.contains (desSet does not contain d) which results in false everytime. Hope that helps get you going in the right direction. This probably has errors but should help some at least: 
 
List<DDCustom__c> des = [select Id, DD__r.Id, Opportunity__r.Id from DDCustom__c where Id in: Trigger.newMap.keySet()];
  List<Opportunity> opp = [select Id, UpdateDD__c, UpdateDD__r.Id from Opportunity where Id =: des[0].cv__Opportunity__r.Id and UpdateDD__c != null];
  List<DDCustom__c> des2 = new List<DDCustom__c>();
  
  Set<DDCustom__c> desSet = new Set<DDCustom__c>();
  
  for(DDCustom__c d : des){
  
      desSet.addAll(des2);
  }
  
  if(opp[0].UpdateDD__c != null && d.DD__r.Id != opp[0].UpdateDD__r.Id){
        d.DD__r.Id = opp[0].UpdateDD__r.Id;
		des2.add(d);
        
    }
 
   update des2;
}



 
AlSawtoothAlSawtooth
Thanks for the reply. I'm updating des2 when I add the newly updated DDCustom to it, once the field has been changed (line 17).

I'm checking to see if the list size is greater than 0 to prevent a recursive loop - when DD is modified, it will be in trigger. new again, so I wanted to make sure that I don't continuously update it. 

Also, taking the field update out of the for loop doesn't help me at all... 

Does that make sense?
Crystal Rochlitz 4Crystal Rochlitz 4
OK that helped clarify things. Since this is a recursion based issue have you tried adding a boolean value and maybe some debugs to see where the code is getting stuck? Its hard to say without having the fields/values but try this: 
trigger UpdateDD2 on DDCustom__c (after update, after insert) {

	List<DDCustom__c> des = [select Id, DD__r.Id, Opportunity__r.Id from DDCustom__c where Id in :Trigger.newMap.keySet()];
	system.debug('Size of des: ' + des.size());
	
	List<Opportunity> opp = [select Id, UpdateDD__c, UpdateDD__r.Id from Opportunity where Id = : des[0].cv__Opportunity__r.Id and UpdateDD__c != null];
	system.debug('Size of opp: ' + opp.size());
	
	List<DDCustom__c> des2 = new List<DDCustom__c>();
  
	Set<DDCustom__c> desSet = new Set<DDCustom__c>();
  
	Boolean hasUpdated;
	for(DDCustom__c d : des){
		if(des2.size()>0){
          desSet.addAll(des2);
          }
  
		if((!desSet.contains(d)) && (opp[0].UpdateDD__c != null) && (d.DD__r.Id != opp[0].UpdateDD__r.Id)){
			d.DD__r.Id = opp[0].UpdateDD__r.Id;
			des2.add(d);
		}
	}
	system.debug('How many updateable records: ' + des2.size());
    if(des2.size() > 0 && hasUpdated == false){
		update des2;
		hasUpdated = true;
	}
}

 
AlSawtoothAlSawtooth
Thanks Crystal. So what happens with the boolean hasUpdated when there are multiple records in trigger.new? Does it just update the first record? I was concerned about this so I used a set to prevent recursion instead (but that's definitely not working either!)

Thanks for your help!