+ Start a Discussion
JN22JN22 

System.LimitException: Too many SOQL queries: 101

Hello,

I have the trigger below that fires on updates to my Account objects.  The trigger works fine and tests fine, but when I try to make mass changes via the data loader I get the error message:

UpdateConsultantAcct: System.LimitException: Too many SOQL queries: 101

Can someone help me figure out why I am getting this error?  Thanks,

trigger UpdateConsultantAcct on Account (after update){
       
    List<Account> acct = new List<Account>();

    Opportunity[] oppsToUpdate = new List<Opportunity>();  

    FOR(Account a : Trigger.new){

    // Query Opportunity records realted to Account that is being created/edited
    List<Opportunity> opps = [SELECT AccountId, IsWon, IsClosed, Consultant_Partner__c, Consultant_Type__c
                              FROM Opportunity
                              WHERE AccountId IN: Trigger.newMap.keySet()];
       
        // Iterating through all the Opportunity records and updating them as per the conditions
        for(Opportunity opps2 :opps){

            // Update Consultant Partner field on Opportunity if Opp is Open and Consultant field changes at Account level
            IF(opps2.IsWon == FALSE && opps2.IsClosed == FALSE && opps2.Consultant_Type__c == 'Primary Consultant' && trigger.NewMap.get(opps2.AccountId).Consultant_Partner_Primary__c != trigger.OldMap.get(opps2.AccountId).Consultant_Partner_Primary__c){
                opps2.Consultant_Partner__c = a.Consultant_Partner_Primary__c;
                oppsToUpdate.add(opps2);
            }
           
            ELSE IF(opps2.IsWon == FALSE && opps2.IsClosed == FALSE && opps2.Consultant_Type__c == 'Secondary Consultant' && trigger.NewMap.get(opps2.AccountId).Consultant_Partner_Secondary__c != trigger.OldMap.get(opps2.AccountId).Consultant_Partner_Secondary__c){
                opps2.Consultant_Partner__c = a.Consultant_Partner_Secondary__c;
                oppsToUpdate.add(opps2);
            }
        }
       
        // Update all realted Opportunity records
        //IF(opps.size() > 0)
        //   update opps;
    }
    update oppsToUpdate;
}
Best Answer chosen by JN22
GunnarGunnar
You want to gather all foreign keys and foreign records before you do your loop - not in the loop ...

Move this to above your FOR LOOP.

List<Opportunity> opps = [SELECT AccountId, IsWon, IsClosed, Consultant_Partner__c, Consultant_Type__c
                              FROM Opportunity
                              WHERE AccountId IN: Trigger.newMap.keySet()];


All Answers

GunnarGunnar
You want to gather all foreign keys and foreign records before you do your loop - not in the loop ...

Move this to above your FOR LOOP.

List<Opportunity> opps = [SELECT AccountId, IsWon, IsClosed, Consultant_Partner__c, Consultant_Type__c
                              FROM Opportunity
                              WHERE AccountId IN: Trigger.newMap.keySet()];


This was selected as the best answer
JN22JN22
Thanks Gunnar, that did the trick!  I didn't realize it would be so simple!