+ Start a Discussion
SureshSuresh 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateContact: maximum trigger depth exceeded Contact trigger event AfterInsert

Hi,

I am trying to create one more  contact whenever a contact is created,my code is as follows:

trigger CreateContact on Contact (after insert) {
    Contact con = trigger.new[0];
    Contact contact = new contact(LastName=con.LastName);
    insert contact;

}


can any one help me why below exception is raising
Exception:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CreateContact caused an unexpected exception, contact your administrator: CreateContact: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateContact: maximum trigger depth exceeded Contact trigger event AfterInsert for [00328000001tPuI] Contact trigger event AfterInsert for [00328000001tPuJ] Contact trigger event AfterInsert for [00328000001tPuK] Contact trigger event AfterInsert for [00328000001tPuL] Contact trigger event AfterInsert for [00328000001tPuM] Contact trigger event AfterInsert for [00328000001tPuN] Contact trigger event AfterInsert for [00328000001tPuO] Contact trigger event AfterInsert for [00328000001tPuP] Contact trigger event AfterInsert for [00328000001tPuQ] Contact trigger event AfterInsert for [00328000001tPuR] Contact trigger event AfterInsert for [00328000001tPuS] Contact trigger event AfterInsert for [00328000001tPuT] Contact trigger event AfterInsert for [00328000001tPuU] Contact trigger event AfterInsert for [00328000001tPuV] Contact trigger event AfterInsert for [00328000001tPuW] Contact trigger event AfterInsert for [00328000001tPuX]: []: Trigger.CreateContact: line 4, column 1
 
Best Answer chosen by Suresh
BDatlaBDatla
Hi Suresh,

Before insert also it is same,You need to use static variable to stop recurssion.

As per your requirement you are creating contact every time contact trigger executes,but you need to remember as you are creating contact in trigger this process will go on and on .. thats why you are getting the exception.

Please gothrough the below article to get more understanding on trigger best practices and how trigger execution order works.
http://www.iterativelogic.com/salesforce-apex-trigger-best-practices/
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm

Regards,
BDatla
 

All Answers

BDatlaBDatla
Hi ,

try to use before innsert and static variable to avoid innner loop
trigger CreateContact on Contact (Before insert) {


Regards,
BDatla
SureshSuresh
Hi,

But also still getting same error
sandeep sankhlasandeep sankhla
Hi Suresh,

It went into recurrsion ...When you are inserting one Contact record then your trigger is firing and then again you are inserting contact and then again trigger is firing then again its inserting..it will go in infinite loop..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
BDatlaBDatla
Hi ,

You also need to use static variabl to avoid recurrsion.

Example :

Class : 
public class p { public static boolean firstRun = true; }

trigger CreateContact on Contact (after insert) {
 if(Trigger.isafter){
      if(Trigger.isinsert){
            if(p.firstRun){ 
             Contact con = trigger.new[0];
            Contact contact = new contact(LastName=con.LastName);
            insert contact;
              p.firstRun=false;
                }
          }
     }
}

Let me know if you need any clarification.

Regards,
BDatla

 
SureshSuresh
Hi BDatla,

is it possible to create without recursionby using before insert also. can u also explain how does trigger fire multiple times and go to infinite loop.
BDatlaBDatla
Hi Suresh,

Before insert also it is same,You need to use static variable to stop recurssion.

As per your requirement you are creating contact every time contact trigger executes,but you need to remember as you are creating contact in trigger this process will go on and on .. thats why you are getting the exception.

Please gothrough the below article to get more understanding on trigger best practices and how trigger execution order works.
http://www.iterativelogic.com/salesforce-apex-trigger-best-practices/
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm

Regards,
BDatla
 
This was selected as the best answer
BDatlaBDatla
Hi Suresh,

Please let me know if you need any more clarification.
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Regards,
BDatla
build app 1build app 1
Hello all i got following error in after insert and after update event trigger.

first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactFieldUpdate: maximum trigger depth exceeded Contact trigger event AfterInsert Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate: 

Hello can you please help me to solve this.