0

So I got help on something recently where I was having a recursive issue with queries, and I got that sorted out.

Today I am having issues with trying to run this loop on a single record.

It's supposed to populate a date field if it is empty. When creating a new record or trying to update I get this error:

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger dreamDateUpdate caused an unexpected exception, contact your administrator: dreamDateUpdate: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id a0p370000017LJ9AAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, dreamDateUpdate: maximum trigger depth exceeded Dream_Journal trigger event AfterUpdate for [a0p370000017LJ9] Dream_Journal trigger event AfterUpdate for [a0p370000017LJ9] Dream_Journal trigger event AfterUpdate for [a0p370000017LJ9] Dream_Journal trigger event AfterUpdate for [a0p370000017LJ9] Dream_Journal trigger event AfterUpdate for [a0p370000017LJ9] Dream_Journal trigger event AfterUpdate for [a0p370000017LJ9]: []: Trigger.dreamDateUpdate: line 17, column 1

It shows more recursive errors than that, but I cut out a lot of them posting this because I figured that is enough.

My expectation is that once I populated the record date field it would invalidate it from meeting the criteria then next time the loop iterated. I also tried creating a Set collection, and the run the loop only on Id's in the collection, but it still generated the same result. I've been scratching my head all day trying to figure it out.

All I can figure is I'm not invalidating it properly after it's been looped. Any advice would be greatly appreciated.

Thanks!

Trigger setDate on object__c (after insert, after update){

List<object__c> tuList = new List<object__c>();

for(object__c checkThis : trigger.new)
{
  object__c toUp = new object__c();
  toUp.Id = checkThis.Id;

  if(checkThis.Date == null)
  {
    toUp.Date = Date.Today();
  }
  tuList.add(toUp);
}
update tuList;
}

Resolution notes: Using the marked answer, I was able to figure out what I was doing wrong. When using an after update trigger, if you aren't being careful, it runs recursive because you are saying "Hey! Update this record" IN the trigger. And then the trigger says "Hey! Something got updated, I need to do more stuff." This creates a loop. For this particular case I was able to switch to a before insert, before update trigger, which adds additional criteria to be inserted/updated. Salesforce, in this case, includes that information as part of it's normal update or insert event, without us have to specifically call it.

4

It is better to move your code on before insert and before update as shown below.

Trigger setDate on object__c (before insert, before update){

for(object__c checkThis : trigger.new)
{
  if(checkThis.Date == null)
  {
    toUp.Date = Date.Today();
  }
}
}
  • always your welcome. Himanshu :) – Ratan Paul Jul 7 '16 at 6:30
  • So is the reason I don't actually use update in my code in this situation is that because it's before the update happens, the normal record update will take care of calling the update event on it's own? – Genko Jul 7 '16 at 20:08
  • Yes that is correct. – Himanshu Jul 8 '16 at 5:48
2

The problem here is you are updating same record in after update. Thus the same code get recursively called again and again. Best way would be to perfom all the assignment operations in before update/insert.

Trigger setDate on object__c (before insert, before update){
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
    for(object__c checkThis : trigger.new)
    {
      if(checkThis.Date == null)
      {
        toUp.Date = Date.Today();
      }
    }
}
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
    //Update any other record other than whom the rigger is written
}
}
  • 1
    So then it's, in a very literal way, running the trigger again every time because it's (after update) and I have an line performing an update in my code? That makes sense in hindsight... – Genko Jul 7 '16 at 16:04
  • 1
    Wanted to note: I didn't mark this as the answer because another solution worked better for this specific case. However, your example helped me solve another issue I was having with a similar code piece in another location. Wanted to let you know and say thanks! – Genko Jul 7 '16 at 20:35
  • You are welcome and welcome to SFSE. – Pranay Jaiswal Jul 8 '16 at 7:09
1

This Code may help to you.

trigger setDate on object__c (after insert, after update){

    List<object__c> tuList = new List<object__c>();

    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
        for(object__c checkThis : trigger.new) {
            object__c toUp = new object__c();

            toUp.Name = checkThis.Name;    

            toUp.Custum_Field_Name = checkThis.Custum_Field_Name; // Similar to your other field values like

            if(checkThis.Date == null) {
                toUp.Date = Date.Today();
            }
            tuList.add(toUp);
        }

        if(tuList.size() > 0) {
            update tuList;
        }
    }
}
  • Thank you. I didn't use this, but wanted to say I appreciate the help all the same. I'm going to review this is see make note of it the next time I have a chance to try this kind of thing. – Genko Jul 7 '16 at 20:36
  • Always welcome.. @Genko – MSCB Jul 8 '16 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.