+ Start a Discussion

System.LimitException: Too many SOQL queries: 101 on an Old test class

HI ,

I am trying to move some new fields and getting this error in one my test class . I can manually cretae new fileds but wnat to knoiw why this error is Occuring . And my test class was running perfectly and now i get this error .

Class TestOverrideOpportunityNameTrigger
Method Name testNotUltimateButLocalClient
Pass/Fail Fail
Error Message System.LimitException: Too many SOQL queries: 101
Stack Trace Trigger.OpportunityBeforeTrigger: line 40, column 1

// This is the method in Test class

static testMethod void testNotUltimateButLocalClient() {
        // specify local client only and pull the ultimate client by traversing hierarchy
        System.debug('Testing insert case where Ultimate Parent Client IS NOT populated but Local Client IS populated which has Parent Account tied to it.');
        Account ultimateAccount= new Account(name='TestOverrideClient Thirteen',BillingStreet='TestOverrideThirteenSt', Client_Short_Name__c ='ulttiCL',
                Phone='123454321',BillingState='ca', BillingPostalCode='95456', BillingCountry='US', BillingCity='TestOverrideThirteenCity', Ultimate_Parent__c = true);
        insert ultimateAccount;
        Account localAccount  = new Account(name='TestOverrideClient Fourteen',BillingStreet='TestOverrideFourteenSt', Client_Short_Name__c ='localCL', Ultimate_Parent_Client__c =ultimateAccount.id,
                        BillingState='ca', BillingPostalCode='95457', BillingCountry='US', BillingCity='TestOverrideFourteenCity', Ultimate_Parent__c = false,phone='111-111-1111');
        insert localAccount;
        Opportunity ultimateOpp = new Opportunity(Local_Client__c=localAccount.Id, Name='testoppfornamechange',Solution_Type__c='Standard',
                        Opportunity_Short_Description__c='test desc',Active_Latent__c='Active',Opportunity_Type__c='parent',
                        StageName='plan & engage',CloseDate=System.today()+2);
        insert ultimateOpp;
        Opportunity ulOpp = [select id, name, accountId, Local_Client__c from Opportunity where id =:ultimateOpp.Id];
        system.assertEquals('TestOver - 0 - test desc', ulOpp.Name);
        system.assertEquals(localAccount.id, ulOpp.Local_Client__c);
        system.assertEquals(ultimateAccount.Id , ulOpp.accountId);
        System.debug('Testing update case where Ultimate Parent Client IS NOT populated but Local Client IS populated which has Parent Account tied to it.');
        Account ultimateAccount2= new Account(name='TestOverrideClient Fifteen',BillingStreet='TestOverrideFifteenSt', Client_Short_Name__c ='ulttiC2',
                BillingState='ca', BillingPostalCode='95458', BillingCountry='US', BillingCity='TestOverrideFifteenCity', Ultimate_Parent__c = true,phone='111-111-1111');
        insert ultimateAccount2;
        Account localAccount2  = new Account(name='TestOverrideClient Sixteen',BillingStreet='TestOverrideSixteenSt', Client_Short_Name__c ='localC2',Ultimate_Parent_Client__c = ultimateAccount2.id,
                        BillingState='ca', BillingPostalCode='95459', BillingCountry='US', BillingCity='TestOverrideSixteenCity', Ultimate_Parent__c = false,phone='111-111-1111');
        insert localAccount2;
        ultimateOpp.AccountId = null;
        ultimateOpp.Local_Client__c = localAccount2.id;
        update ultimateOpp;
        Opportunity ulOpp2 = [select id, name, accountId, Local_Client__c from Opportunity where id =:ultimateOpp.Id];
        system.assertEquals('TestOver - STD - 0 - 0 - 0 - test desc', ulOpp2.Name);
        system.assertEquals(localAccount2.id, ulOpp2.Local_Client__c);
        system.assertEquals(ultimateAccount2.Id , ulOpp2.accountId);

This is the code mentioned in Stack Trace

trigger OpportunityBeforeTrigger on Opportunity (before insert,before update) {
   Set<Id> parentOppIdSet = new Set<Id>();
   Set<Id> validOppIdSet = new Set<Id>();
    for(Opportunity opp: Trigger.new) {
        if(opp.Parent_Opportunity__c != null) {
            if(opp.Opportunity_Type__c == 'Parent')
                opp.addError('Parent should not have another opportunity as parent.');
        } else {
            if(opp.Opportunity_Type__c == 'Child')
                opp.addError('Please enter a Parent Opportunity. ');
    if(validOppIdSet != null) {
        try {
            Map<Id, Opportunity> childOppMap = new Map<Id, Opportunity>([Select Id, Name, Parent_Opportunity__c from Opportunity where Parent_Opportunity__c IN: validOppIdSet limit 30]);
            Set<Id> errorOppIdSet = new Set<Id>();
            List<OpportunityLineItem> oppLineItemList = new  List<OpportunityLineItem>();
            if(childOppMap != null && childOppMap.size() > 0) {
                Set<Id> childOppIdSet = childOppMap.keySet();
                    oppLineItemList = [Select Id, OpportunityId from OpportunityLineItem where OpportunityId IN: childOppIdSet limit 50];
                catch(Exception e){
                if(oppLineItemList != null && oppLineItemList.size() > 0) {
                    for(Opportunity childOpp: childOppMap.values()) {
                        Id childOppId = childOpp.Id; 
                        Boolean isOppLineItemExists = false;
                        for(OpportunityLineItem oppLineItem : oppLineItemList) {
                            if(childOppId == oppLineItem.OpportunityId) {
                                isOppLineItemExists = true;

Swati GSwati G

Use Test.StartTest() and Test.stopTest() to reset you governor limits. And also you can combine the dml statements for account insert.
Hi Akhil,

Hope you aware about the gevernor limits in salesforce (https://developer.salesforce.com/page/Apex_Code_Best_Practices).  You have limitations on SOQL, DML, etc. while you are running your code on salesforce. As per your posted code can't see something related to violating the limits. But you have to make sure that in your test execution, you are inside the limits with respect to the total number of SOQLs(which can be fired from triggers, classes, etc.)

It's always good to have startTest() and stopTest() (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm#apex_System_Test_startTest) in your test class so that it assign a new set of governor limits. 

One another good option is to use Limits (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_limits.htm)class in salesforce to check how many queries are remaining for you.