You need to sign in to do that
Don't have an account?
[Lindsey Kiken]
ReasonUnderReview: maximum trigger depth exceeded Error
I am new to apex coding, especially code that involves mult-select picklists, so please bear with me!
I have a multi-select picklist on the Contact object called Reason_Under_Review__c that I am working on coding to automatically populate with values that are depented on specific values within the Contact record to == TRUE. My current code is as follows:
So far, I see two glaring issues with the code above:
Cheers,
Lindsey
I have a multi-select picklist on the Contact object called Reason_Under_Review__c that I am working on coding to automatically populate with values that are depented on specific values within the Contact record to == TRUE. My current code is as follows:
trigger ReasonUnderReview on Contact (after insert, after update) { //(1) define variable for list of all UnderReviewIDs List<Id> UnderReviewIDs = new List<ID>(); for(Contact c: trigger.new){ if(c.Reason_Under_Review__c == null){ UnderReviewIDs.add(c.id); } } //(2) get ids and fields on the Contact record List<Contact> underreviewreasons = new List<Contact>(); for(Contact c: [SELECT Id,FirstName,LastName,Email,Reason_Under_Review__c FROM Contact WHERE Id in: UnderReviewIDs]) { //(3) select Under Review Reasons based on fields on the Contact record if(c.FirstName==null){c.Reason_Under_Review__c += ';Missing First Name ';} if(c.LastName==null){c.Reason_Under_Review__c +=';Missing Last Name ';} if(c.Email==null){c.Reason_Under_Review__c +=';Missing Email Address ';} c.Reason_Under_Review__c = c.Reason_Under_Review__c.replaceAll(';?null;?',''); underreviewreasons.add(c); //(4) update Reason_Under_Review__c field } update underreviewreasons; }
So far, I see two glaring issues with the code above:
- I can only get this code to fire when the Reason_Under_Review__c field == null on Line 6. This means that if the trigger has ever previously fired and populated the field with a value, the trigger will no longer fire any additional updates.
- If I change Line 6 to say RecordTypeID = [18digit record type ID in my test org] or Reason_Under_Review__c != null, I get a large "ReasonUnderReview: maximum trigger depth exceeded" error when trying to save my test Contact record.
- I believe that this issue might be due to recursive errors, but am a bit lost on how to fix the issue. How do I resolve this error in my code?
- This code is very obviously not setup right now to conditionally delete multi-select picklist values, if the criteria does not meet the condition for the value to exist within the field.
- A quick example would be that the Contact record previously did not have a FirstName value, and has since updated the record. Per Line 17, the field would have been stamped with a "Missing First Name" value, but I have nothing in place to conditional remove said value when it no longer rings true.
- How do I conditionally delete values within a multi-select picklist field?
Cheers,
Lindsey
1) The recursion is because you are firing the trigger on after update, and you're calling an update on a list of contacts in the trigger. You can solve this problem by writing a class to check for a recursive call.
2) One way to do this is instead of appending values to the field, just set the field to empty first and populate based on your criteria.
Here's the class code:
And you would have to modify your trigger code like this:
Let me know how that works!
All Answers
1) The recursion is because you are firing the trigger on after update, and you're calling an update on a list of contacts in the trigger. You can solve this problem by writing a class to check for a recursive call.
2) One way to do this is instead of appending values to the field, just set the field to empty first and populate based on your criteria.
Here's the class code:
And you would have to modify your trigger code like this:
Let me know how that works!