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 

Calculation part in test class is not getting covered

Hi,

 I am writing a test class I am not able to proceed further in the calculation part in class need you suggestion how to cover. 
 
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;
        }
    }


}
Below lines are not getting covered. 
User-added image
Test Class
@isTest(seealldata=true)
public class CpqQuoteLineTriggerUtilsTest
{  
    public static testMethod void method1(){
        
       list<SBQQ__QuoteLine__c> SBQQlst = new list<SBQQ__QuoteLine__c>();
       
        
        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
        );
        
        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
                                              );
        
        
        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 and COGS__c <> null limit 1];
        
        Product2 PrdCTRM = [select id from product2 where isactive = true and productcode = 'COTERM' and COGS__c <> null 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;                                                                                                    
        
        SBQQlst.add(SQlnCTRM);
        
        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;
        
         Test.startTest();

          CpqQuoteLineTriggerUtils.calcMarigns(SBQQlst);
     
         Test.stopTest();
 
    }
    
    
    public static testMethod void method2(){
        
        SBQQ__Quote__c  quote = [ select  id, NAme,CPQ_Distributor__c, SBQQ__PriceBook__c, Deal_Type__c, Quote_Has_CSPP_Partner_Account__c,
                                 CPQ_Distributor__r.Account__r.BillingCountry,CPQ_Distributor__r.Account__c,
                                 (select SBQQ__Product__c, CPQ_Quote_Line_Item_Type__c, SBQQ__Quote__c,Category__c,SBQQ__Quantity__c,
                                  SBQQ__ListPrice__c, CPQ_DistDiscount__c, SBQQ__CustomerPrice__c, SBQQ__NetPrice__c,CPQ_Forticare_Quote_ID__c,
                                  CPQ_DISTDiscOverride__c, CPQ_ResellerDiscOverride__c,CPQ_Reseller_Discount__c
                                  from SBQQ__LineItems__r)
                                 from SBQQ__Quote__c limit 1 ];
        
        Quote_Category_Metrics__c QCM = [select id from Quote_Category_Metrics__c where CPQ_Quote__c = :quote.id limit 1]; 
        
        delete qcm;
        
    }  
    
   
    
    
}

Thanks
Sudhir​​​​​​​
Rishab TyagiRishab Tyagi

Hello Sudhir,

I believe that there is some problem meeting the criteria for your If condition on line 43. Either there are no pricebookentry or the formula field is true. Please check those values as that is the only possible reason for code not being covered is the Test class is successfully executing.

Also please check that the test execution did not throw any errors.

Hope that answers your question.