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
praveen avvaripraveen avvari 

How to resolve "System.LimitException: Too many SOQL queries: 101"

Hi,
I am getting "System.LimitException: Too many SOQL queries: 101" from belo test class, please help me to reslove this isse.
----------------------------------------------------------------------------------------------
@isTest
public class OpportunityProductTriggerTest {
    
        private static Map<String, Id> getRecordTypes() {
        Map<String, Id> recordTypeByName = new Map<String, Id>();
        List<RecordType> rts = [select id,developerName from RecordType];
        system.debug(rts);
        for(RecordType r: rts) {
            recordTypeByName.put(r.developerName, r.Id);
        }
       
            return recordTypeByName;
        }
    
        @isTest static void TestInsert() {
      
        Map<String, Id> rts = getRecordTypes();
        Product2 p = TestDataFactory.createProduct('Test', 'Goods', 'Food goods', 'Fixed Amount', true);
        p.CPM_applicable__c = true;
        p.IsActive = true;
  //      p.RevenueRecognition__c = 'POC Cost';
        insert p;

        Id pbId = Test.getStandardPricebookId();
        PriceBookEntry pb = TestDataFactory.creatPriceBook(p.Id, 10, true, pbId);
        insert pb;

        SYSTEM.debug('== ' + rts.keySet());
        Account company = TestDataFactory.createAccount('Au_feminin','test1@gmail.com', 'France', 'Paris', 'Street', 'France', 'Paris', 'Paris street', 'LUX-TVA', '2211', rts.get('Company'));
        insert company;

        Account acc = TestDataFactory.createAccount('Apple','test2@gmail.com', 'Belgium', 'Liege', 'Street', 'Belgium', 'Liege', 'street', 'LUX-TVA', '223', rts.get('Annonceur'));
        insert acc;

        Opportunity opp = TestDataFactory.createOpportunity('Test', 'Qualification', Date.today(), company.Id, acc.Id, null, 'CW8_AGENT_TYPE-1',rts.get('Default'), 'No', 'CUSTOMER_CONTRACT_TYPE-2');
        opp.Probability = 4;
        insert opp;

        OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp.id,
                                                                 //PriceBookEntryID=pbe1.id, 
                                                                 PriceBookEntryID=pb.id, 
                                                                 //fatima
                                                                 Product2Id = p.id,
                                                                 ServiceDate = System.today() + 500,
                                                                 Realization_to_date__c = System.today() + 510,
                                                                 UnitPrice = 50,
                                                                 Contact_volume__c = 500,
                                                                 Quantity=1, 
                                                                 Cost_Center_Company__c = 'Alfemminile',
                                                                 Cost_Center__c = 'CC-ALF51',
                                                                 DeleteScheduleItem__c = true,
                                                                 Estimated_costs_copy_from_line_item_sche__c = 5
                                                                 
                                                                );

        insert lineItem1;

        system.debug('Foued_toto => ' + lineItem1.Id);
                    
            
        
        SetStaticVariable.AutomaticCreation = true;
            
        OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule(ActualCosts__c = 100, EstimatedCost__c = 500, 
                                                                           OpportunityLineItemId = lineItem1.Id,
                                        Type='Revenue', Revenue = 200, ScheduleDate = System.today()+220,
                                        FromDate__c = system.today()+250, EndDate__c = system.today() + 880,
                                        Revenue_Recognition_Product__c = 'Custom');
        insert olis;
            
                        Billing_Schedule__c bil = new Billing_Schedule__c(
                                    Opportunity__c = opp.id,
                                    Amount__c = 20.5,
                                    Opportunity_Product_Id__c = lineItem1.Id,
                                    Start_Date__c = System.today() + 500,
                                     End_Date__c = System.today() + 510

            
                );              
            insert bil; 
            
            
            
                    Product2 p2 = TestDataFactory.createProduct('Test', 'Goods', 'Food goods', 'Fixed Amount', true);
        p2.CPM_applicable__c = true;
        p2.Volume__c = 700;
        p2.IsActive = true;
        insert p2;

        Id pbId2 = Test.getStandardPricebookId();
        PriceBookEntry pb2 = TestDataFactory.creatPriceBook(p2.Id, 10, true, pbId2);
        insert pb2;

        SYSTEM.debug('== ' + rts.keySet());
        

        Opportunity opp2 = TestDataFactory.createOpportunity('Test', 'Qualification', Date.today(), company.Id, acc.Id, null, 'CW8_AGENT_TYPE-1',rts.get('Default'), 'No', 'CUSTOMER_CONTRACT_TYPE-2');
        opp2.Probability = 4;
        insert opp2;

        OpportunityLineItem lineItem2 = new OpportunityLineItem (OpportunityID=opp2.id,
                                                                 //PriceBookEntryID=pbe1.id, 
                                                                 PriceBookEntryID=pb2.id, 
                                                                 //fatima
                                                                 Product2Id = p2.id,
                                                                 ServiceDate = System.today() + 500,
                                                                 Realization_to_date__c = System.today() + 510,
                                                                 UnitPrice = 50,
                                                                 Contact_volume__c = null,
                                                                 Quantity=1, 
                                                                 Cost_Center_Company__c = 'Alfemminile',
                                                                 Cost_Center__c = 'CC-ALF51',
                                                                 DeleteScheduleItem__c = true,
                                                                 Estimated_costs_copy_from_line_item_sche__c = 5
                                                                 
                                                                );

        insert lineItem2;
            lineItem2.UnitPrice = 8;
            lineItem2.Contact_volume__c = 500;
            lineItem2.Discount = 7;
            update lineItem2;
            
      //      lineItem2.UnitPrice = null;
            lineItem2.Quantity = null;
            lineItem2.TotalPrice = 5;
            update lineItem2;
            
            OpportunityLineItem lineItem3 = new OpportunityLineItem (OpportunityID=opp2.id,
                                                                 //PriceBookEntryID=pbe1.id, 
                                                                 PriceBookEntryID=pb2.id, 
                                                                 //fatima
                                                                 Product2Id = p2.id,
                                                                 ServiceDate = System.today() + 500,
                                                                 Realization_to_date__c = System.today() + 510,
                                                                 UnitPrice = null,
                                                                 Contact_volume__c = null,
                                                                 Quantity=1, 
                                                                 Cost_Center_Company__c = 'Alfemminile',
                                                                 Cost_Center__c = 'CC-ALF51',
                                                                 DeleteScheduleItem__c = true,
                                                                 Estimated_costs_copy_from_line_item_sche__c = 5
                                                                 
                                                                );

        insert lineItem3;
            
                    
        p.RevenueRecognition__c = 'POC Cost';
        update p;
           
        lineItem1.ServiceDate = System.Today() + 600;
        lineItem1.Realization_to_date__c = System.Today() + 610;
        lineItem1.UnitPrice = 20;
        lineItem1.Quantity = 7;
            
        update lineItem1;
            
        delete lineItem1;
        }
                

}
--------------------------------------------------------------------------------------------
SwethaSwetha (Salesforce Developers) 
HI Praveen,
Have you setup debug logs to see the stacktrace of SOQL that is throwing this error? Thanks
Alejandro BraunschweigAlejandro Braunschweig
Hi Praveen,

Just as Swetha said, you should inspect the test's resulting log to try and identify the issue. In your code, I cannot see SQOL calls inside of loops, so there is a good change there is a Trigger or Process Flow working on the background wich is not "Bulkified", thus causing the limit to be hit.

I spot about 21 SOQLs in your test code; add a debug call right before each SQOL operation, run your test and then do a simple Search over the log (opened in raw format). The result will most likely be about a 100 hits. Then look for those SOQL calls that are not preceeded by your debug call, there you'll find the culprit.