1

I am getting an error message when trying to use an after update, after insert trigger, the code exists within another class

Trigger:

trigger Identity_Set_Contact_Name_Text on Identity__c (after insert,after update) {


IdentityTriggerHandler ith = new IdentityTriggerHandler();
ith.setContactSearchFieldText(trigger.new);
}

Class

public void setContactSearchFieldText(list<Identity__c> records)
{

 list<Identity__c> contactIds22= new list<Identity__c>([select id,name,Search_Contact__c ,Contact__r.name,
 Contact__r.phone, Contact__r.Secondary_Phone__c,Last_4_digits_of_Identity__c, Last_4_digits_of_Phone__c,
 Last_4_digits_of_Secondary_Phone__c
  from Identity__c where id in: records ]);
   list<Identity__c> idttupins= new list<Identity__c>();                                                                   

  for(Identity__c idtt : contactIds22)
  {

  if(idtt.name.length()>=4)
  { 
  idtt.Last_4_digits_of_Identity__c=string.valueOf(idtt.name).right(4);
  }

  if(idtt.Contact__r.phone.length()>=4) {
  idtt.Last_4_digits_of_Phone__c=string.valueOf(idtt.Contact__r.phone).right(4);
  }
  if(idtt.Contact__r.Secondary_Phone__c.length()>=4){
  idtt.Last_4_digits_of_Secondary_Phone__c=string.valueOf(idtt.Contact__r.Secondary_Phone__c).right(4);  
  }    
  idtt.Search_Contact__c=string.valueOf(idtt.Contact__r.name);

  idttupins.add(idtt);=
  }

 update idttupins;

}

When update the indentity Record, I get the following error message (see below)

My second question, is it possible to run this trigger before insert/before update, if yes, how can we write the trigger ?

Update failed. First exception on row 0 with id a023C000001D8SgQAK; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Identity_Set_Contact_Name_Text: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a023C000001D8SgQAK; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Identity_Set_Contact_Name_Text: maximum trigger depth exceeded Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg] Identity trigger event AfterUpdate for [a023C000001D8Sg]: [] Class.IdentityTriggerHandler.setContactSearchFieldText: line 123, column 1 Trigger.Identity_Set_Contact_Name_Text: line 7, column 1: []

  • Could you please remove update idttupins; this line in your class method and test again. – MSCB Dec 3 '16 at 12:03
0

Your code is a typical example of infinite loop in your trigger. In your update trigger code, you updated a list of Identity records - idttupins. This update will fire the trigger again. Then the trigger will update the records again. So it will become a recursive loop here.

There are multiple ways to resolve this. One easy way is to set a static variable.

In your Util class you can have:

public static inIdentityTrigger = false;

And in your trigger code, update it into:

trigger Identity_Set_Contact_Name_Text on Identity__c (after insert,after update) {
  if(Util.inIdentityTrigger) {
    return;
  }

  Util.inIdentityTrigger = true;
  IdentityTriggerHandler ith = new IdentityTriggerHandler();
  ith.setContactSearchFieldText(trigger.new);
}

As Adrian Larson has mentioned that this pattern can be an anti-pattern due to the abuse of Util class. You might want to change it into TriggerUtil or something like that. A even better way can be employing a TriggerHandler framework. But hopefully this give you a direction.

As for the before trigger, usually it could be done. I am not quite sure what your trigger is trying to achieve. So I am not sure. But your issue is not related to after trigger.

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.