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
Hughbert StrongHughbert Strong 

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

Hello,

My trigger is running fine in the sandbox. When I run my test class, I receive the following error: System.LimitException: SBQQ:Too many SOQL queries: 101. Can someone take a look at my code and point me in the right direction?

Trigger:
trigger QLAdjustListTotal on SBQQ__QuoteLine__c (after insert, after update, after delete) {
    Decimal ADJrec = 0.00;
    Decimal ADJup = 0.00;
    Decimal ADJhware = 0.00;
    Decimal NETrec = 0.00;
    Decimal NETup = 0.00;
    Decimal NEThware = 0.00;    
    String qid;
    
    list<SBQQ__QuoteLine__c> l;
    
    if(Trigger.isInsert || Trigger.isUpdate){
        l = Trigger.new;
    }
    
    if(Trigger.isDelete){
        l = Trigger.old;
    }
    
    try{
    for(SBQQ__QuoteLine__c q : l){
        qid=q.SBQQ__Quote__c;
    }
 
    for(SBQQ__QuoteLine__c f : l){
        if(f.Adjusted_List_Line_Item__c > 0 && f.Quote_Line_Grouping__c == 'Recurring Fee'){
            ADJrec+=f.Adjusted_List_Line_Item__c;
            if(f.SBQQ__NetTotal__c > 0){
            NETrec+=f.SBQQ__NetTotal__c;
            }
        }
         if(f.Adjusted_List_Total__c > 0 && f.Quote_Line_Grouping__c == 'Upfront Fee'){
            ADJup+=f.Adjusted_List_Total__c;
            if(f.SBQQ__NetTotal__c > 0){ 
            NETup+=f.SBQQ__NetTotal__c;
            }     
        }
        if(f.Adjusted_List_Total__c > 0 && f.Quote_Line_Grouping__c == 'Hardware'){
            ADJhware+=f.Adjusted_List_Total__c;
            if(f.SBQQ__NetTotal__c > 0){ 
            NEThware+=f.SBQQ__NetTotal__c;
                }    
            }
        }
    SBQQ__Quote__c q = [Select Id from SBQQ__Quote__c where Id =: qid];
    q.Adjusted_List_Total_Recurring_Fee_2__c = ADJrec;
    q.Adjusted_List_Total_Upfront_Fee_2__c = ADJup;
    q.Adjusted_List_Total_Hardware_2__c = ADJhware;
    q.Net_Total_Recurring_Fee_2__c = NETrec;
    q.Net_Total_Upfront_Fee_2__c = NETup;
    q.Net_Total_Hardware_2__c = NEThware;
    update q;   
    }catch(QueryException ex){
    System.debug(ADJrec);
    System.debug(ADJup);
    System.debug(ADJhware);
    System.debug(NETrec);
    System.debug(NETup);
    System.debug(NEThware);}     
    
}

Test Class
@isTest(SeeAllData = true)
public class TestQLAdjustListTotal {
    static testMethod void TestQL(){
        //Start Test
        Test.startTest();
        
        //Declare Variables and Objects
        Decimal ST = 12;
        Date StartQuote = Date.today();
        Account a = new Account();
        Opportunity o = new Opportunity();
        SBQQ__Quote__c q = new SBQQ__Quote__c();
        SBQQ__QuoteLine__c qlRec = new SBQQ__QuoteLine__c();
        SBQQ__QuoteLine__c qlUp = new SBQQ__QuoteLine__c();
        SBQQ__QuoteLine__c qlHware = new SBQQ__QuoteLine__c();        

        //Create Account Record
        a.Name = 'TestAccount';
        insert a;
        update a;
        
        //Create Opporunity
        o.Name = 'Test Opp';
        o.AccountId = a.Id;
        o.Type = 'New Business 2';
        o.CloseDate = StartQuote;
        o.Type_of_Sale__c = 'New System';
        o.ECi_Product__c = 'Spruce';
        o.StageName = 'Owned Lead';
        o.Cloud_Deal__c = True;
      o.Pricebook2 = [Select id from Pricebook2 where id ='01s0v000000PRox'];
        insert o;

        
        //Create Quote Record
        q.SBQQ__Account__c = a.Id;
        q.SBQQ__Opportunity2__c = o.Id;
        q.SBQQ__Status__c = 'Draft';
        q.SBQQ__Type__c = 'Quote';
        q.SBQQ__StartDate__c = StartQuote;
        q.CurrencyIsoCode = 'USD';
        q.SBQQ__Primary__c = True;
        q.SBQQ__SubscriptionTerm__c = ST;
        q.SBQQ__PriceBook__c = [Select id from Pricebook2 where id ='01s0v000000PRox'].id;
        insert q;

        //Create Quote Line Records
        
        //Create Recurring Fee Quote Line
        qlRec.SBQQ__Quote__c = q.Id;
        qlRec.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bna'].id;
        insert qlRec;
        delete qlRec;
        
        //Create Upfront Fee Quote Line
        qlUp.SBQQ__Quote__c = q.Id;
        qlUp.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bnV'].id;
        insert qlUp;
        delete qlUp;        

        //Create Hardware Fee Quote Line
        qlHware.SBQQ__Quote__c = q.Id;
           qlHware.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bnf'].id;

        insert qlHware;
        delete qlHware;        
        Test.stopTest(); 
    }

}
Aslam ChaudharyAslam Chaudhary
I will suggest you to 
1- Use SeeAllData = false
2- Create product as well
3- Use Test Class Property as shown in below code
@isTest
public class PriceBookTest {
    // Utility method that can be called by Apex tests to create price book entries.
    static testmethod void addPricebookEntries() {
        // First, set up test price book entries.
        // Insert a test product.
        Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Hardware');
        insert prod;
        
        // Get standard price book ID.
        // This is available irrespective of the state of SeeAllData.
        Id pricebookId = Test.getStandardPricebookId();
        
        // 1. Insert a price book entry for the standard price book.
        // Standard price book entries require the standard price book ID we got earlier.
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        // Create a custom price book
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        // 2. Insert a price book entry with a custom price.
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        // Next, perform some tests with your test price book entries.
    }
}

refernce taken from 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm
Suresh Kumar Arjunan1Suresh Kumar Arjunan1
You might need to re-factor your Apex Test Class to have multiple test methods. I see you have one test method to cover multiple scenario within start and stop test context. 

I would suggest creating 6 test methods which ensures fresh governor limits instantiated using start and stop test for test scenario...for example:
 
static testMethod void TestQL1(){
     
        Test.startTest();

//Create Recurring Fee Quote Line
        qlRec.SBQQ__Quote__c = q.Id;
        qlRec.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bna'].id;
        insert qlRec;
		test.stopTest();
 }
 
  static testMethod void TestQL2(){
     
        

//Create Recurring Fee Quote Line
        qlRec.SBQQ__Quote__c = q.Id;
        qlRec.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bna'].id;
        insert qlRec;
		
		Test.startTest();
		  delete qlRec;
		test.stopTest();
 }
 
  static testMethod void TestQL3(){
     
        Test.startTest();

        //Create Upfront Fee Quote Line
        qlUp.SBQQ__Quote__c = q.Id;
        qlUp.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bnV'].id;
        insert qlUp;
		
		test.stopTest();
 }
 
  static testMethod void TestQL4(){
     
        

        //Create Upfront Fee Quote Line
        qlUp.SBQQ__Quote__c = q.Id;
        qlUp.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bnV'].id;
        insert qlUp;
		Test.startTest();
		    delete qlUp; 
		test.stopTest();
 }
 
  static testMethod void TestQL5(){
     
        Test.startTest();

        //Create Hardware Fee Quote Line
        qlHware.SBQQ__Quote__c = q.Id;
           qlHware.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bnf'].id;

        insert qlHware;
		test.stopTest();
 }
  static testMethod void TestQL6(){   
        

        //Create Hardware Fee Quote Line
        qlHware.SBQQ__Quote__c = q.Id;
           qlHware.SBQQ__Product__c = [Select id from Product2 where id ='01t0v0000010bnf'].id;

        insert qlHware;
		
		Test.startTest();
			delete qlHware;     
		test.stopTest();
 }

Note: You might need to check your test debug logs to understand all 100+ SOQL queries if root cause is something else.

Let me know if this helps.