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]