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
MaheemSamMaheemSam 

System.LimitException: Too many SOQL queries: 101 test class conflict

Hi, 

  There are two different class  I am trying to get the  code coverage for the below class
public class QuoteLineClaimTriggerUtils {

   public static void processRollupQuoteLine(List<Quote_Line_Claim__c> newLst){

        Set<ID> qIds = new Set<ID> ();
        for(Quote_Line_Claim__c qlc : newLst) {
            system.debug(qlc.CPQ_Quote_Product1__c);
            if(qlc.CPQ_Quote_Product1__c != null)
                qIds.add(qlc.CPQ_Quote_Product1__c);

        }
         system.debug('Quote ID  ' + qIds);
         Map<id, SBQQ__QuoteLine__c> qlMap =  new Map<id, SBQQ__QuoteLine__c>([select id, name, CPQ_Forticare_Quote_ID__c, SBQQ__NetTotal__c,SBQQ__Quantity__c,
                                                                                         SBQQ__Quote__r.SBQQ__Opportunity2__c                                               
                                                                                      from SBQQ__QuoteLine__c 
                                                                                      where id in :qIds and  CPQ_Forticare_Quote_ID__c != null]); 
        List<SBQQ__QuoteLine__c> qlLstUpdate = new List<SBQQ__QuoteLine__c>();
        for(AggregateResult result : [SELECT sum(CPQ_Amount__c) amt, sum(CPQ_Quantity__c) qty, CPQ_Quote_Product1__c 
                                            FROM Quote_Line_Claim__c
                                            where  CPQ_Quote_Product1__c = :qIds
                                            group by CPQ_Quote_Product1__c] ) {
            system.debug('result ' + result);
            SBQQ__QuoteLine__c ql = new SBQQ__QuoteLine__c(id = (String)result.get('CPQ_Quote_Product1__c'));
            system.debug(qlMap);
            if(qlMap.containsKey(ql.id)) {
                ql.CPQ_ClaimedAmountToDate__c = qlMap.get(ql.id).SBQQ__NetTotal__c;
                ql.CPQ_Claimed_Quantity_To_Date__c = qlMap.get(ql.id).SBQQ__Quantity__c;
                system.debug('fully claimed for Coterm or renewal');
              } else {
                ql.CPQ_ClaimedAmountToDate__c = (Decimal)result.get('amt');
                ql.CPQ_Claimed_Quantity_To_Date__c = (Decimal)result.get('qty');
            }
            system.debug(ql);
            qlLstUpdate.add(ql);
        }
       if(!qlLstUpdate.isEmpty()){
            update qlLstUpdate;

            Set<ID> opportunityIds = new Set<ID>();
            for(SBQQ__QuoteLine__c ql : [select id, name, CPQ_Forticare_Quote_ID__c, SBQQ__NetTotal__c,SBQQ__Quantity__c,
                                                                                         SBQQ__Quote__r.SBQQ__Opportunity2__c                                               
                                                                                      from SBQQ__QuoteLine__c 
                                                                                      where id in :qIds]) {
                   opportunityIds.add(ql.SBQQ__Quote__r.SBQQ__Opportunity2__c);
            }
            List<opportunity> optyLstUpdate = new List<opportunity>();
            for(AggregateResult result : [select  sum(Total_Claimed_Amount_to_Date__c) amt, SBQQ__Opportunity2__c from SBQQ__Quote__c  where  SBQQ__Opportunity2__c in :opportunityIds
                                                group by SBQQ__Opportunity2__c] ) {
                system.debug('result ' + result);
                opportunity opp = new opportunity(id = (String)result.get('SBQQ__Opportunity2__c'));
                opp.Oracle_Claimed_Amount__c = (Decimal)result.get('amt');
                system.debug(opp);
                optyLstUpdate.add(opp);
            }
            if(!optyLstUpdate.isEmpty()) {
                update optyLstUpdate;
            }
        }



   }

   public static void processErpClaims(List<Quote_Line_Claim__c> newLst){
   
     Set<String> erpUuniqueIds = new Set<String>();
     Set<String> prdNbrs = new Set<String>();
     Map<String, SBQQ__QuoteLine__c> prdMap = new Map<String, SBQQ__QuoteLine__c>();
     
     for(Quote_Line_Claim__c sc: newLst) {
       erpUuniqueIds.add(sc.CPQ_ERP_Unique_Id__c);
       prdNbrs.add(sc.CPQ_ERP_Quote_Product_Number__c);
     }   
     List<SBQQ__QuoteLine__c> sprPrds = [select id, name, CPQ_Forticare_Quote_ID__c
                                          from SBQQ__QuoteLine__c 
                                          where name in :prdNbrs]; 
     for(SBQQ__QuoteLine__c sPrd : sprPrds) {
            prdMap.put(sPrd.name, sPrd);
        }   
     
       for(Quote_Line_Claim__c sc: newLst) {
            SBQQ__QuoteLine__c sPrd = prdMap.get(sc.CPQ_ERP_Quote_Product_Number__c);
            if(sPrd != null) {
                sc.CPQ_Quote_Product1__c = sPrd.id;
                sc.CPQ_Result__c ='';
            } else {
                sc.CPQ_Result__c ='DEL:Invalid product number';
            }
            system.debug('cliams : '+ sc);
        }
        
    }
     
     
     
   }
  Test Class
@isTest(seealldata=true)
public class QuoteLineClaimTriggerUtilsTest
{  
    public static testMethod void method1(){
        
        Profile profileObj =[select id from Profile where name='System Administrator' limit 1];
        User u = new User(
            FirstName = 'Sudhir',
            LastName = 'Testing Usre',
            Alias = 'tstN',
            Email = 'test11@abc.com',
            Username = 'partne1r11@abc.com',
            CommunityNickname = 'testi11',
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            profileId = profileObj.Id,
            spr_region__c = 'LATAM',
            NFR_User__c  = false,
            SPR_to_Forticare_Linking__c = true
        );
        insert u; 
        
        Distributor__c Dist = [select id from Distributor__c where CPQ_DistributorStatus__c  = true limit 1];
        
        Account Pact = [select id from account where recordtype.name = '.Partner' and Is_In_RV_Portal__c = true and Partner_Status__c = 'Activated' limit 1];
        
        Contact Pcnt = [select id from contact where accountid = :Pact.id  limit 1];    
        
        Account Act = new Account( Name = 'Test Sudhir Ac',Website='www.sudhir.com',Industry='Legal',BillingStreet='894', BillingCity='sunnyvalley', BillingState='CA', 
                                  BillingPostalCode='997',BillingCountry='United States',Customer_Status__c='Current Customer');  
        
        insert Act; 
        
        Contact C = [select id from contact limit 1];
        
        Opportunity opp = new Opportunity(
            AccountId=Act.id,
            StageName='Omit from Forecast',
            Amount = 0,
            Name = 'Test Sudhir',
            CloseDate = Date.today(),
            Market_Segmentation__c = 'Education',
            End_User_Industry__c = 'Education',
            End_Customer_Country__c = 'United States',
            Deal_Type__c='Refresh',
            Primary_Opportunity_Contact__c =  c.id,
            Partner_Initiated__c = 'No',
            Distributor__c = Dist.id,
            Reason_for_Won_Opportunity__c = 'Other'
        );
        
        insert opp;
        
        OpportunityTeamMember oppmem = new OpportunityTeamMember(
            Opportunityid = opp.id,
            OpportunityAccessLevel = 'Read' ,
            TeamMemberRole = 'CAM',
            Userid = u.id ); 
        
        insert oppmem;
        
        SBQQ__Quote__c SQ = new SBQQ__Quote__c(SBQQ__Opportunity2__c = opp.id,
                                               CPQ_Distributor__c = Dist.id,
                                               CPQ_Partner_Account__c = Pact.id,
                                               CPQ_Partner_Contact__c = Pcnt.id
                                               ,SBQQ__Primary__c = true
                                              );
        
        
        insert SQ;
        
        
        PricebookEntry pbe = [ select   Id, IsActive, Name, Pricebook2Id, Product2Id, ProductCode, SystemModstamp, UnitPrice, UseStandardPrice 
                              from PricebookEntry where isactive = true and usestandardprice = true limit 1];
        
        Product2 Prd = [select id from product2 where isactive = true limit 1];
        
        //Product2 PrdCTRM = [select id from product2 where isactive = true and productcode = 'COTERM' limit 1];
        
        SBQQ__QuoteLine__c SQln = new SBQQ__QuoteLine__c(SBQQ__Product__c = Prd.id,
                                                         SBQQ__Quantity__c = 1,
                                                         SBQQ__Quote__c = SQ.id,
                                                         SBQQ__ListPrice__c = 111,
                                                         SBQQ__NetPrice__c = 111,
                                                         CPQ_Reseller_Discount__c = 22,
                                                         Category__c = 'A'
                                                        );
        insert SQln;                                                                                                    
        
      /*  SBQQ__QuoteLine__c SQlnCTRM = new SBQQ__QuoteLine__c(SBQQ__Product__c = PrdCTRM.id, 
                                                             SBQQ__Quantity__c = 1,
                                                             SBQQ__Quote__c = SQ.id,
                                                             SBQQ__ListPrice__c = 111,
                                                             SBQQ__NetPrice__c = 111,
                                                             CPQ_Reseller_Discount__c = 22,
                                                             CPQ_Forticare_Quote_ID__c = 'SUDHIR',
                                                             Category__c = 'B'
                                                            );
        insert SQlnCTRM;    */                                                                                                
        
        
        Quote_Line_Claim__c QLClm = new Quote_Line_Claim__c(CPQ_Quote_Product1__c = SQln.id,
                                                            CPQ_Quantity__c =2,
                                                            CPQ_Claim_Number__c='123',
                                                            CPQ_ERP_PO_or_Claim__c = 'test',
                                                            CPQ_ERP_Quote_Product_Number__c = '342');
                                                           
        
        
        insert QLClm;
        
        
       /* SBQQ__Quote__share SQS = new SBQQ__Quote__share(UserOrGroupId = u.id,
                                                        ParentId = SQ.id,
                                                        AccessLevel  = oppmem.OpportunityAccessLevel
                                                       );
        
        insert SQS; */
        
        //Quote_Category_Metrics__c QCM = new Quote_Category_Metrics__c(Name='a', CPQ_Quote__c=SQ.id,CPQ_Gross_Margin__c = 1,CPQ_List_Price__c =1,CPQ_Disti_Price__c =1);    
        
        //insert QCM;
    }
  
}

I am getting error from 

 Pass/FailFail
Error MessageSystem.LimitException: Too many SOQL queries: 101
Stack TraceClass.CpqQuoteLineTriggerUtils.processQuoteMatrics: line 107, column 1
Trigger.CpqQuoteLineTrigger: line 22, column 1

Below the class where the error is from please suggest me how to fix. 
public class CpqQuoteLineTriggerUtils {

    public static void calcMarigns(List<SBQQ__QuoteLine__c> newLst) {
        Set<Id> product2IDs = new Set<Id>();
        Set<String> bundleSkus = new Set<String>();
        ID pbId = null;
        Map<String, String> bundleSkuMap = new Map<String, String>();
        Map<String, PriceBookEntry> pbeMap = new Map<String, PriceBookEntry>();
        system.debug(newLst);

        for (SBQQ__QuoteLine__c line : newLst) {
            if(pbId == null) {
                pbId = [select SBQQ__PriceBook__c from SBQQ__Quote__c where id = :line.SBQQ__Quote__c].SBQQ__PriceBook__c;
            }
            if(line.CPQ_IS_Dummy_Product_Formula__c == false) {
                product2IDs.add(line.SBQQ__Product__c);
                if(line.CPQ_Product_Code__c != null && line.CPQ_Product_Code__c.contains('-BDL')) {
                    bundleSkus.add(line.CPQ_Product_Code__c);
                }
            }
        }

        for(Bundled_Product_Map__c pmap: [SELECT Id, Bundle_SKU__c, Hardware_OR_Software_SKU__c 
                                                FROM Bundled_Product_Map__c where Bundle_SKU__c in :bundleSkus]) {
            system.debug('pmap.Hardware_OR_Software_SKU__c' + pmap.Hardware_OR_Software_SKU__c);
            system.debug('pmap.Bundle_SKU__c' + pmap.Bundle_SKU__c);                                     
            bundleSkuMap.put(pmap.Bundle_SKU__c, pmap.Hardware_OR_Software_SKU__c); 
        }

        for(PriceBookEntry pbe : [Select id, Product2.COGS__c,Product2.Product_Type__c,Product2.Productcode, UnitPrice,
                                        Product2.Isactive,Product2.CPQ_IS_Dummy_Product__c 
                                    from PriceBookEntry 
                                    where pricebook2.IsStandard = false 
                                        and (Product2Id In :product2IDs or Product2.ProductCode in :bundleSkuMap.values())
                                        and pricebook2ID = :pbId]) {
            pbeMap.put(pbe.Product2.ProductCode, pbe);
            system.debug( pbe.Product2.Isactive + 'cogs== '+ pbe.Product2.COGS__c+  'pbe' + pbe);
        }
        system.debug('pbeMap' + pbeMap);
        for (SBQQ__QuoteLine__c line : newLst) {
            System.debug('line '+ line);
            PriceBookEntry pbe = pbeMap.get(line.CPQ_Product_Code__c);
            if(line.CPQ_IS_Dummy_Product_Formula__c != true && pbe != null) {
                system.debug( 'cogs== '+ pbe.Product2.COGS__c+  'pbe' + pbe);
                system.debug('quoteline'+line);

                Double cogs = pbe.Product2.COGS__c;
                Double listPrice = line.SBQQ__ListPrice__c != null ? line.SBQQ__ListPrice__c : pbe.UnitPrice;
                if(line.CPQ_Product_Code__c.contains('COTERM')) {
                    cogs = listPrice * 0.1;
                }
                Double distiDiscount = ( 1 - (line.CPQ_DistDiscount__c == null ? 0 : line.CPQ_DistDiscount__c) /100);
                Double distiNetUnitPrice = listPrice * distiDiscount;
                Double quantity = line.SBQQ__Quantity__c;
                System.debug('quantity=' + quantity + 'cogs=' + cogs);
                Double grossMargin = (distiNetUnitPrice - cogs) * quantity;

                line.COGS__c = cogs;
                //line.Gross_Margin__c = grossMargin;
                system.debug( 'distiNetUnitPrice=' + distiNetUnitPrice + 'distiDiscount=' + distiDiscount + 'pbe' + pbe);

                Double totalBilling = distiNetUnitPrice * quantity;
                Double cogsBilling = cogs * quantity;
                line.Is_BundleMapping_missing__c = false;
                line.Bundle_Related_HW_Product__c = null;
                if(line.CPQ_Product_Code__c.contains('-BDL') ) {
                     if(bundleSkuMap.containsKey(line.CPQ_Product_Code__c)){
                        String mappingSku = bundleSkuMap.get(line.CPQ_Product_Code__c);
                         system.debug('mappingSku ' + mappingSku); 
                        PriceBookEntry hwProduct = pbeMap.get(mappingSku);
                         system.debug('hwProduct ' + hwProduct );
                         system.debug('hwProduct.Product2.cogs__c' + hwProduct.Product2.cogs__c);
                        cogsBilling = hwProduct.Product2.cogs__c * quantity;
                        Double hwListPice = hwProduct.UnitPrice;                
                        Double hwCogs = hwProduct.Product2.cogs__c;
                        Double hwSalesPrice = (hwListPice * distiDiscount);
                        Double hardwareBilling = hwListPice * distiDiscount * quantity;
                        system.debug('hwListPice =' + hwListPice + ' hwSalesPrice =' + hwSalesPrice );
                        if(hwProduct.Product2.product_Type__c == 'HW' || hwProduct.Product2.product_Type__c == 'Hardware') {
                            line.Bundle_Related_HW_Product__c = hwProduct.Product2Id;
                            line.Hardware_billings_in_amount__c = hardwareBilling;
                            line.Hardware_Gross_Margin_in_amount__c = (hwSalesPrice - hwCogs)* quantity;
                            line.Hardware_Gross_Margin_Percentage__c = 100*(hardwareBilling -cogsBilling )/hardwareBilling; 
                        } 
                    } else {
                        line.Is_BundleMapping_missing__c = true;
                        system.debug( '_log_' + line);
                        //throw new PP_HandleException (p.ProductCode +  ' - this bundle product has not mapped with appropriate Hardware sku mapping, please contain admin with screenshot.' );
                    }
                } else if(pbe.Product2.product_Type__c == 'HW' || pbe.Product2.product_Type__c == 'Hardware') {
                    line.Hardware_billings_in_amount__c = totalBilling;
                    line.Hardware_Gross_Margin_in_amount__c = grossMargin;
                    line.Hardware_Gross_Margin_Percentage__c = 100*(totalBilling -cogsBilling )/totalBilling;
                } else if(pbe.Product2.Product_type__c == 'SW') {
                    line.Software_Billings_in_amount__c = totalBilling;
                } 
            }
        }
    }
    public static void processQuoteMatrics(List<SBQQ__QuoteLine__c> newLst) {
        Set<ID> qIds = new Set<ID> ();
        for(SBQQ__QuoteLine__c ql : newLst) {
            qIds.add(ql.SBQQ__Quote__c);
        }
         system.debug('Quote ID  ' + qIds);
        List<Quote_Category_Metrics__c> insertMatrics = new List<Quote_Category_Metrics__c>();
        for(AggregateResult result : [select Category__c,SBQQ__Quote__c, sum(CPQ_marginDollars__c) a, 
                                            sum(SBQQ__ListTotal__c) b, sum(SBQQ__NetTotal__c) c 
                                        from SBQQ__QuoteLine__c 
                                        where SBQQ__Quote__c  IN :qids and SBQQ__Quote__r.ApprovalStatus__c != 'Approved'  group by Category__c, SBQQ__Quote__c] ) {
            Quote_Category_Metrics__c qc = new Quote_Category_Metrics__c();
            system.debug('Category C ' + (String)result.get('Category__c'));
            system.debug('Category A  ' + (Decimal)result.get('a'));
            system.debug('Category B  ' + (Decimal)result.get('b') );
            system.debug('Category C  ' + (Decimal)result.get('c') );
            qc.Name = (String)result.get('Category__c');
            if(qc.name != null) {
                qc.CPQ_Gross_Margin__c = (Decimal)result.get('a');
                qc.CPQ_List_Price__c = (Decimal)result.get('b');
                qc.CPQ_Disti_Price__c = (Decimal)result.get('c');
                
                qc.CPQ_Quote__c = (String)result.get('SBQQ__Quote__c');
                insertMatrics.add(qc);
            }
        }

        List<Quote_Category_Metrics__c> existingRec = [select id from Quote_Category_Metrics__c where CPQ_Quote__c in :qIds];
        if(!existingRec.isEmpty()) {
            delete existingRec;
        }

        if(!insertMatrics.isEmpty()){
            insert insertMatrics;
        }
    }

Thanks
Sudhir
Vishal Ranjan 14Vishal Ranjan 14
This type of exception comes when you are hitting governor limits. Please ensure you are not using soql queries inside any for loop.
MaheemSamMaheemSam
Hi Vishal,

   There is no SOQL inside any code I double checked any suggestion to fix this issue

Thanks
Sudhir
Vishal Ranjan 14Vishal Ranjan 14
HI Maheem

I can see Class CpqQuoteLineTriggerUtils is having SOQL inside FOR loop. 

Better you perform insertion by resetting governor limits in your test class by using Test.startTest() and Test.StopTest()