function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hari G SHari G S 

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

Best Answer chosen by Admin (Salesforce Developers) 
raseshtcsraseshtcs

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

Chamil MadusankaChamil Madusanka

Hi,

 

Can you explain your scenario?

You have done some mistakes in this trigger.

  • You have use only first triggered record (trigger.new[0])
  • Do not use SOQL queries in for loops.

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.

Hari G SHari G S

 

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

raseshtcsraseshtcs

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.

This was selected as the best answer
Hari G SHari G S

Hi

 

Thanks for the reply.

 

Let me try the rollup summary fields :)

 

Thanks

Hari  GS