1

i have created a trigger which creates an opprtunity when the accounte is created and i have a custom field No of contact on account which displays the count of related contact for a particular account whenever it is updated.

I have tested seperated that the opportnity is created but for the second part ,it displays a big error when i try to save a record.

I am not getting any error while saving a record.Can someone help

    trigger insertOpp on Account(After insert,after update,before update)
{
List<Opportunity> opplist=new List<Opportunity>();
for(Account acc:Trigger.new){

Opportunity op=new Opportunity();
op.Name='Cook';
op.accountid=acc.id;
op.CloseDate=System.today();
op.StageName='Qualification';
opplist.add(op);
}

insert opplist;




if(trigger.isupdate)
{
        Integer no_of_contact;
        String accID;

    for(Account c:Trigger.new )
    {
        accid = c.id;

    }

    account acclist = new account();
    acclist = [select No_of_Contacts__c from account where id = :accid ];
    no_of_contact = [SELECT count() FROM Contact WHERE AccountId = :accid];
    acclist.No_of_Contacts__c = no_of_contact;
    //update acclist; 
}
 }

ERROR:

Review all error messages below to correct your data. Apex trigger insertOpp caused an unexpected exception, contact your administrator: insertOpp: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00128000002r8gKAAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, insertOpp: maximum trigger depth exceeded Account trigger event AfterUpdate for [00128000002r8gK] Account trigger event AfterUpdate for [00128000002r8gK]

  • 3
    What error are you getting? – Arjun Khatri Jun 10 '15 at 10:49
  • Review all error messages below to correct your data. Apex trigger insertOpp caused an unexpected exception, contact your administrator: insertOpp: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00128000002r8gKAAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, insertOpp: maximum trigger depth exceeded Account trigger event AfterUpdate for [00128000002r8gK] Account trigger event AfterUpdate for [00128000002r8gK] – Ankita Singh Jun 10 '15 at 10:56
  • 2
    Thats not a compile time error its a runtime error saying that your trigger is recursively firing. Your trigger fires when an Account is updated or inserted and then it creates Opportunity records (for both inserts and updates!!) it then sets the No_of_Contacts__c field on Account which causes your trigger to fire again. – BarCotter Jun 10 '15 at 10:58
  • So according to BarCotter u need handle recursion in your code.. – Arjun Khatri Jun 10 '15 at 11:05
  • 2
    Why does you trigger execute in a before update AND an after update context ? Why not just one of them ? Why both ? Plus, in the if(trigger.isUpdate) block, you are launching another update ! So the trigger is basically calling itself, which is why you get the error... You could use a static variable in a class which stores the value of whether the trigger already executed or not. And I don't understand this for(Account c:Trigger.new ) { accid = c.id; } loop, since accid is always gonna be the value of the last Account in the trigger.new list – Julien Gaïotto Jun 10 '15 at 13:10
7

This error is generating because your trigger on after update is running recursively. You can resolve this error by using a static boolean variable check. For eg.

  • 1)create a class( say triggerHelperClass). create a public static boolean variable( say bool), with value = true/false;
  • 2) Add a check if(triggerHelperClass.bool ==true) "// if u set the value as true in class, then true here",in the first line inside trigger.
  • 3) Set the value of triggerHelperClass.bool = false; , before you update your list in trigger.

This will remove your error, and force your trigger to NOT run recursively on after update event. Please mark as Best Answer if it helps. Let me know.

3

The way you have your trigger written, it appears you are creating an opportunity when an account is created, also before an account is updated and then again after the account is updated:

{
List<Opportunity> opplist=new List<Opportunity>();
for(Account acc:Trigger.new){

Opportunity op=new Opportunity();
op.Name='Cook';
op.accountid=acc.id;
op.CloseDate=System.today();
op.StageName='Qualification';
opplist.add(op);
}

insert opplist;  

I would eliminate this part of the trigger all together and move it to the process builder instead.

The second part of your trigger appears to be trying to update the number of contacts on the account before the account is updated and after the account is updated. Since the trigger is on the account you do not need to query for the account fields you should be able to do this before update and assign the count to the field in the before update context, however I'm not sure this makes sense to run on an account since I'm assuming you want to know when a contact is added/deleted. Also it appears only the last record if bulk updates occurred would be updated. I would recommend taking a look at Andy in The Clouds "Declarative Rollups For Lookups", it's unbelievably easy to install and manage.

if(trigger.isupdate) //would run both before and after update
{
        Integer no_of_contact;
        String accID;

    for(Account c:Trigger.new ) //loops through all updated records but
    {
        accid = c.id;  //assigning to same variable inside loops so overwriting

    }

    account acclist = new account();
    acclist = [select No_of_Contacts__c from account where id = :accid ];//using a single account id to query the account and no need since you already have the contact fields available inside the trigger
    no_of_contact = [SELECT count() FROM Contact WHERE AccountId = :accid]; //again only getting contacts related to the single id
    acclist.No_of_Contacts__c = no_of_contact; 
    //update acclist; 
}
 }

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.