You need to sign in to do that
Don't have an account?
Exception
Hi
I have written a trigger
trigger oneTimeTrigger on Account (after insert, after update) {
Account myAcc = trigger.new[0];
Account[] accs = [SELECT Id, Name FROM Account];
list<Account> accList = new list<Account>();
integer count = 0;
if(accs.size() > 0) {
for(Account a : accs){
count++;
if(count >= 0 && count <= 500) {
Opportunity[] opp = [select Id From Opportunity where AccountId = :a.Id];
if(opp != null) {
a.Number_Of_Opportunities__c = opp.size(); // count();
accList.add(a);
}
}
}
update accList;
}
}
But i am getting a Error
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger oneTimeTrigger caused an unexpected exception, contact your administrator: oneTimeTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001L00000033S7PIAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, oneTimeTrigger: maximum trigger depth exceeded Account trigger event AfterUpdate for [001L00000033S0b] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event AfterUpdate for [001L00000033S0b, 001L00000033S7P] Account trigger event BeforeUpdate for [001L00000033S0b, 001L00000033S7P]: []: Trigger.oneTimeTrigger: line 17, column 9
Why is this happening? Please help
Thanks and Regards
Hari G S
To have a count of number of opportunities attached to a particulat account you can make use of rollup summary fields. Which will hold the count of child records related to any parent.
The exception is coming as the trigger is going in an infinite loop as the code is updating account itself.
All Answers
Hi,
Can you explain your scenario?
You have done some mistakes in this trigger.
Post your scenario to understand the exception.
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Thanks Chamil for the reply
My scenario is(I would like to call this trigger as oneTimeTrigger once excecuted it will be inactivated)
I have added a new field Number of opportunities in the Account object to know how many opportunities are added to a particular account. I need to invoke a trigger and this trigger should update all the Number of opportunities field of the existing accounts. This trigger is just a dummy to get into this code. What i am trying to do here is load all the accounts first and iterate through these accounts to get the number of opportunities added to that account. So is there any way i can avoid the SOQL inside the for loop? or is there any other better way for doing this requirment.?
Thanks and Regards
Hari G S
To have a count of number of opportunities attached to a particulat account you can make use of rollup summary fields. Which will hold the count of child records related to any parent.
The exception is coming as the trigger is going in an infinite loop as the code is updating account itself.
Hi
Thanks for the reply.
Let me try the rollup summary fields :)
Thanks
Hari GS