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
JN22JN22 

Test Class Error - System.LimitException: Too many SOQL queries: 101

Hello,

I have created a test class and trigger (see below).  The trigger works as expected but when I run the test I get the error:

System.LimitException: Too many SOQL queries: 101
Trigger.UpdateConsultantAcct: line 8, column 1


Does anyone know why I would be getting this error and how to resolve it?  Thanks in advance for any help!


Test Class:

@Istest(SeeAllData=true)
public class TestAccountPlan {

public static testMethod void testMyController1() {    
       
  Account acct1 = TestGeneralAcctCreate.createAcct(0);
    insert acct1;

    Account acct2 = TestStatusAcctCreate.createAcct(0);
  
    insert acct2;

        Profile ProDir2 = [SELECT Id
                          FROM Profile
                          WHERE Name='Client Delivery (Director)'];
               
            User Dir2 = new User(Alias = 'Dir2User',Country='United States',Email='Dir2User@testing.com',EmailEncodingKey='UTF-8', LastName='Testing1a', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProDir2.Id,TimeZoneSidKey='America/New_York', UserName='Dir2User@testing.com');
        insert Dir2;

        Profile ProTeamMgr2 = [SELECT Id
                              FROM Profile
                              WHERE Name='Client Delivery Team Manager'];
               
            User TM2 = new User(Alias = 'TM2User',Country='United States',Email='TM2User@testing.com',EmailEncodingKey='UTF-8', LastName='Testing2a', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProTeamMgr2.Id,TimeZoneSidKey='America/New_York', UserName='TM2User@testing.com');
        insert TM2;

        Profile ProCSM2 = [SELECT Id
                          FROM Profile
                          WHERE Name='Client Success Manager'];
               
            User CSM2 = new User(Alias = 'CSM2User',Country='United States',Email='CSM2User@testing.com',EmailEncodingKey='UTF-8', LastName='Testing3a', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProCSM2.Id,TimeZoneSidKey='America/New_York', UserName='CSM2User@testing.com');
        insert CSM2;

        Account acct2a = [SELECT id,Market_Director__c,Industry_Manager__c,Client_Advisor__c
                          FROM Account
                          WHERE id =: acct2.Id];
               acct2a.Market_Director__c = Dir2.Id;
               acct2a.Industry_Manager__c = TM2.Id;
               acct2a.Client_Advisor__c = CSM2.Id;
        
    Account acct3 = TestGeneralAcctCreate.createAcct(0);
    insert acct3;

    Account acct4 = TestGeneralAcctCreate.createAcct(0);
    insert acct4;

    Account acct5 = TestGeneralAcctCreate.createAcct(0);
    insert acct5;

    Opportunity opp1 = TestOppCreate3.createOpp(acct1.Id);
    insert opp1;

    Contract_Summary__c contsumm1 = TestContractCreate.createContSumm(opp1.Id);
    insert contsumm1;
  
        Contract_Summary__c contSumm2 = [SELECT id
                                         FROM Contract_Summary__c
                                         WHERE id =: contSumm1.id];
  
    Client_Status_Scorecard__c cssc1 = TestStatusSCCreate.createCSSC1(acct1.Id);
    insert cssc1;
    
        Client_Status_Scorecard__c cssc1a = [SELECT id
                                           FROM Client_Status_Scorecard__c
                                           WHERE id =: cssc1.id];
    
    Client_Status_Scorecard__c cssc2 = TestStatusSCCreate.createCSSC2(acct2.Id);
    insert cssc2;
    
    Client_Status_Scorecard__c cssc3 = TestStatusSCCreate.createCSSC3(acct3.Id);
    insert cssc3;
    
    Client_Status_Scorecard__c cssc4 = TestStatusSCCreate.createCSSC4(acct4.Id);
    insert cssc4;
    
    Client_Status_Scorecard__c cssc5 = TestStatusSCCreate.createCSSC5(acct5.Id);
    insert cssc5;
    
    Account_Plan__c plan1 = TestAcctPlanCreate.createAP1(acct1.Id);

    ApexPages.StandardController AcctPlan = new ApexPages.standardController(acct1);
    AcctPlanController APControl1 = new AcctPlanController(AcctPlan);
    APControl1.ap2 = plan1;
    APControl1.testsf();
    APControl1.save();
    
    Test.startTest();
    update acct1;
    update acct2;
    update acct2a;
    update acct3;
    update acct4;
    update acct5;
    update opp1;
    update contsumm1;
    delete contsumm2;
    update cssc1;
    update cssc3;
    update cssc4;
    update cssc5;
    delete cssc1a;
    Test.stopTest();
  
}

}

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

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

    // 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()];
        
    FOR(Account a : acct){
//    FOR(Account a : Trigger.new){

        // 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 oppsToUpdate; 
}



dev_sfdc1dev_sfdc1
Hi..

Try to call query insert and update correctly inside test.starttest and test.stoptest
Because test.starttest and test.stoptest method will avoid governor limit exception in test class..

It will helps you..

Thank You.
JN22JN22
What do you mean by calling it correctly?  Is it incorrect that way it is now?  Thanks.
dev_sfdc1dev_sfdc1
Hi..
Profile ProDir2 = [SELECT Id FROM Profile WHERE Name='Client Delivery (Director)'];
User Dir2 = new User(Alias = 'Dir2User',Country='United States',Email='Dir2User@testing.com',EmailEncodingKey='UTF-8', LastName='Testing1a', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProDir2.Id,TimeZoneSidKey='America/New_York', UserName='Dir2User@testing.com');
insert Dir2;


I mean you to call every query insertion or updation part (which I show sample query portion above) inside that test.start and stop test.
Do step by step save and run, then you will get idea to clear that error..

Thank you