You need to sign in to do that
Don't have an account?
find the error
Review all error messages below to correct your data.
Apex trigger Count_Opp caused an unexpected exception, contact your administrator: Count_Opp: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0019000000Qeiu4AAB; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Count_Opp: maximum trigger depth exceeded Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4] Account trigger event AfterUpdate for [0019000000Qeiu4]: []: Trigger.Count_Opp: line 42, column 1
trigger Count_Opp on Account (After insert, after update) {
list<opportunity> opplist=new list<opportunity>();
list<Account> acclist=new list<account>();
list<Account> acclist1=new list<account>();
set<id> id =new set<id>();
integer won=0;
integer lost=0;
integer total=0;
for(Account a:Trigger.new)
{
id.add(a.id);
}
opplist=[select StageName,name from opportunity where Accountid in:id];
for(opportunity opp:opplist)
{
if(opp.StageName=='Closed Won'&&opp.name!=null)
{
won++;
total++;
}else if(opp.StageName=='Closed Lost'&&opp.name!=null)
{
lost++;
total++;
}
else{
total++;
}
}
acclist=[select No_of_won_opp__c,No_of_lost_opp__c,Total_opp__c from Account where id in:id];
for(Account acc:acclist)
{
acc.No_of_won_opp__c=won;
acc.No_of_lost_opp__c=lost;
acc.Total_opp__c=total;
acclist1.add(acc);
}
if(acclist.size()!=0){
update acclist1;
}
}
Since it is after update you can't update the same records that were updated; it causes recursion. Change your trigger to before update, before insert and the issue should be resolved.
Here Trigger is not needed to calulate total/won/lost opportunities on account. You can create rollup summary fields on account and select the object opportunity... and select count of records with appropriate filter criteria.
Trigger recursion is handled by implementing this recipe: http://developer.force.com/cookbook/recipe/controlling-recursive-triggers