0

The trigger is giving me an error

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY . Maximum trigger depth exceeded.

Now this trigger is working f9 in after insert event but i want it after update. I can't understand where its going wrong.

trigger updatingsuffix on Account (after update) {
    List<Account> acclst=new List<Account>();
    Set<Id> accset=new Set<Id>();
    if(trigger.IsInsert){
        for(Account a:trigger.new){
            accset.add(a.Id);
        }

        for(Account a:[select Id,Name,Gender__c from Account where Id=:accset]){
            if(a.Gender__c=='Male'){
                a.Name='Mr. '+a.Name;
                acclst.add(a);
            }
            else if(a.Gender__c=='Female'){
                acclst.add(a);
            }
        }
        if(acclst!=null){
            update acclst;
        }
    }
}
4
  • thank for the response ,but the issue is same. – vineet May 17 '17 at 12:08
  • first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updatingsuffix: maximum trigger depth exceeded is the exact error – vineet May 17 '17 at 12:08
  • The problem is somewhere else. An update trigger where all the logic is contained in a block dependent on the trigger being an insert trigger effectively does nothing. – martin May 18 '17 at 0:28
  • @martin actually i rectified that but it worked using before update only. Neways thnx for the response – vineet May 18 '17 at 9:04
1

Take a look at the advice offered in the Apex Developer Guide on Triggers:

There are two types of triggers:

  • Before triggers are used to update or validate record values before they’re saved to the database.
  • After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.

You are performing a field update, so use before events and set the fields in memory (on your trigger context records); your changes will commit to the database.

trigger Account on Account (before update)
{
    for (Account record : trigger.new)
    {
        if (record.Gender__c == 'Male')
        {
            record.Name = 'Mr. ' + record.Name;
        }
    }
}
6
  • hi adrian thnkx for the response , i know that it will work with insert. but kindly help me in updating the existing records and not on the insert. Means what changes i can do to update the existing records. – vineet May 17 '17 at 12:10
  • @vineet You need to use before events and remove your additional update call. You used after update. That's your problem. – Adrian Larson May 17 '17 at 12:13
  • ok i got the your point . actually now i am using before update event. its updating also but in a wrong way. like if the name earlier is Ms. vineet,itz updating it to Mr. Ms. vineet. So,could you please help me to remove Ms. after updation. – vineet May 17 '17 at 12:21
  • You need to add conditions to your logic. Right now you perform your field update unconditionally. – Adrian Larson May 17 '17 at 12:25
  • i think this will work upto some extent. a.Name='Mr. '+a.Name.removeStart('Ms.'); – vineet May 17 '17 at 12:30

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