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
shakila Gshakila G 

Code coverage error?

Hi, Still my Trigger is getting 72 % code coverage.

Please let me know where am doing wrong?

My Trigger :

trigger v2_QuoteLineItemTaxUpdate on QuoteLineItem (Before Insert, after insert) {

    list<QuoteLineItem> lstUpdateQLI = new list<QuoteLineItem>();
    set<id> setQuoteId = new set<id>();
    set<id> setQLIId = new set<id>();
    set<string> setdiscount = new set<string>();
    set<String> setHSNCode = new set<String>();    
    map<string, list<Tax_Master__c>>mapTaxMasToTaxKey = new map<string, list<Tax_Master__c>>();    
        
    for(QuoteLineItem iQLI : Trigger.New){    
    
        setQLIId.add(iQLI.id);
        setQuoteId.add(iQLI.QuoteId);
        system.debug('****setQLIId'+setQLIId); 
            
    }
    
    if(setQuoteId.size() > 0){
    
        for(Quote iQuot : [SELECT id,(SELECT id, Product2.HSN_Code__c FROM QuoteLineItems WHERE ID IN: setQLIId) 
            FROM Quote WHERE Id IN :setQuoteId]){
        
            for(QuoteLineItem iQLI : iQuot.QuoteLineItems){
                  
                    if(iQLI.Product2.HSN_Code__c != ''){
                    
                        setHSNCode.add(iQLI.Product2.HSN_Code__c);
                    }    
            }   
            system.debug('****setHSNCode'+setHSNCode);                    
        }
        
        if(setHSNCode.size() > 0){
        
            if(setHSNCode.size() > 0){ 
            
                for(Tax_Master__c iTM : [SELECT id, Product_Family__c,Product_Sub_Family__c,HSN_Code__c, IGST__c, CGST__c, SGST__c, Active__c,Start_Date__c,End_date__c  FROM Tax_Master__c 
                    WHERE HSN_Code__c IN :setHSNCode]){
                
                    String ikey = iTM.HSN_Code__c;
                    if(!String.isblank(iTM.Product_Family__c))
                    {
                        ikey = ikey + iTM.Product_Family__c;                         
                    } 
                    if(!String.isblank(iTM.Product_Sub_Family__c))
                    {
                        ikey = ikey + iTM.Product_Sub_Family__c;                         
                    } 
                    list<Tax_Master__c> lstTax = new list<Tax_Master__c>();
                    lstTax.add(iTM);
                    if(mapTaxMasToTaxKey.containskey(ikey))
                    {
                        lstTax.addall(mapTaxMasToTaxKey.get(ikey));
                    }
                    mapTaxMasToTaxKey.put(ikey, lstTax);   
                 }    
                 
                         system.debug('****mapTaxMasToTaxKey'+mapTaxMasToTaxKey);       

            }  
                   
            
            for(Quote iQuot : [SELECT id, Tax_Classification__c, Discount_Amount__c, Anki_total_Amt__c,
                (SELECT Product2.family,Q_Discount__c, Product2.HSN_Code__c, Product2.Sub_Family__c,Tax_Master__c, 
                    IGST__c, CGST__c, SGST__c FROM QuoteLineItems WHERE ID IN: setQLIId) FROM Quote WHERE Id IN :setQuoteId]){
            
                for(QuoteLineItem iQLI : iQuot.QuoteLineItems){  
                         
                   
                       system.debug('****iQuot.Discount_Amount__c'+iQuot.Discount_Amount__c); 
                       
                       system.debug('****iQuot.Anki_total_Amt__c'+iQuot.Anki_total_Amt__c); 
                          
                         
                        if(iQuot.Discount_Amount__c <= 0){
                                iQLI.Q_Discount__c = 0;    
                          
                            } else {
                                if(iQuot.Discount_Amount__c != null && iQuot.Discount_Amount__c != 0 && iQuot.Anki_total_Amt__c !=null && iQuot.Anki_total_Amt__c !=0){
                                iQLI.Q_Discount__c =(iQuot.Discount_Amount__c/iQuot.Anki_total_Amt__c) * 100;
                                
                                }
                            }                               
                        system.debug('****Q_Discount__c'+iQLI.Q_Discount__c);
                        Tax_Master__c itax = new Tax_Master__c();
                        if(mapTaxMasToTaxKey.containsKey(iQLI.Product2.HSN_Code__c+iQLI.Product2.Family+iQLI.Product2.Sub_Family__c)){
                            for(Tax_Master__c irow : mapTaxMasToTaxKey.get(iQLI.Product2.HSN_Code__c+iQLI.Product2.Family+iQLI.Product2.Sub_Family__c))
                            {
                                if(irow.Start_Date__c <= System.today() && irow.End_Date__c  >= System.today())
                                {
                                
                                    itax = irow;
                                    break;
                                }
                            }
                        }
                        else if(mapTaxMasToTaxKey.containsKey(iQLI.Product2.HSN_Code__c+iQLI.Product2.Family) ){
                            
                            for(Tax_Master__c irow : mapTaxMasToTaxKey.get(iQLI.Product2.HSN_Code__c+iQLI.Product2.Family))
                            {
                                if(irow.Start_Date__c <= System.today() && irow.End_Date__c  >= System.today())
                                {
                                
                                    itax = irow;
                                    break;
                                }
                            }
                        }else if(mapTaxMasToTaxKey.containsKey(iQLI.Product2.HSN_Code__c))
                        {
                            for(Tax_Master__c irow : mapTaxMasToTaxKey.get(iQLI.Product2.HSN_Code__c))
                            {
                                if(irow.Start_Date__c <= System.today() && irow.End_Date__c  >= System.today())
                                {
                                
                                    itax = irow;
                                    break;
                                }
                            }
                        }
                        
                    if(!String.isblank(itax.id)){
                    
                        iQLI.Tax_Master__c = itax.id;
                        
                        
                        
                        if(iQuot.Tax_Classification__c == 'IGST'){
                        
                            iQLI.IGST__c = itax.IGST__c;
                            iQLI.SGST__c = 0; 
                            iQLI.CGST__c = 0;
                            
                        } 
                        
                         
                        if(iQuot.Tax_Classification__c == NUll){
                        
                            iQLI.IGST__c = 0;
                            iQLI.SGST__c = 0; 
                            iQLI.CGST__c = 0;
                            
                        } 
                        
                        if(iQuot.Tax_Classification__c == 'SGST'){
                        
                            iQLI.SGST__c = itax.SGST__c;
                            iQLI.CGST__c = itax.CGST__c;    
                            iQLI.IGST__c = 0;  
                                                 
                        }                       
                        
                        lstUpdateQLI.add(iQLI);                            
                    }                                         
                }    
            }  
            
            if(lstUpdateQLI.size() > 0){
            
                update lstUpdateQLI;
            }          
        }
    }
}


My Tess Class:

@isTest(seealldata=true)
Public class v2_QuoteLineItemTaxUpdate_Test{
    static testMethod void Testv2_QLITaxUpdate(){
        
        GetInitialData Getdata = new GetInitialData();
        list<Account> acc = Getdata.getAccount();
        list<Product2> lstprod = Getdata.getProd();
        list<Opportunity> Opp = Getdata.getOpp();
        list<Pricebook2> lstPB = Getdata.getpricebook();
        list<Pricebookentry> lststdPbe = Getdata.getstdpbe();
        list<Pricebookentry> lstPbentry = new list<Pricebookentry>();
        insert lstprod; 
        
        Integer indexvalue = 0;
        if(lstprod.size()>0)
        {
            for(pricebookentry irow :lststdPbe)
            {
                irow.product2id = lstprod[indexvalue].id;
                
                indexvalue++;
            }
        }
        
        
        insert lststdPbe;
        
        insert lstPB; 
        
        
        if(lstprod.size()>0)
        {
            for(pricebook2 irow :lstPB)
            {
                for(Product2 ipro : lstprod ){
                    Pricebookentry pbe = new Pricebookentry();
                    pbe.product2id = ipro.id;
                    pbe.pricebook2id = irow.id;
                    pbe.unitprice = 3000;
                    pbe.isActive = true;
                    lstPbentry.add(pbe);
                }
            }
        }

        insert lstPbentry;
        insert acc[0];
        
        Opp[0].accountId = acc[0].id;
        insert Opp[0]; 
        
        Opp[0].Pricebook2Id = lstPB[0].id;
        update Opp[0];
        
        OpportunityLineItem OL = new OpportunityLineItem(opportunityid=Opp[0].id, Quantity=1, unitprice=8000,  
            pricebookentryid=lstPbentry[0].id);
        insert OL; 
        
        Tax_Master__c   txm= new Tax_Master__c(CGST__c=14.00,HSN_Code__c=lstprod[0].HSN_Code__c,IGST__c=28.00,SGST__c=14.00, Start_Date__c =  Date.newInstance(2017, 07, 06), End_Date__c = System.today()+2);
        insert txm;  
            
        Quote Q = new Quote();
        Q.Name = 'QuoteOrbit';
        Q.OpportunityId = Opp[0].id; 
        Q.Quote_Count__c = 00000000;
        Q.Quote_Number__c ='000000';
        Q.Approved__c = false;
        Q.Quotation_Date__c = date.newinstance(2013,3,5);
        Q.Pricebook2Id = lstPB[0].id;
        Q.Tax_Classification__c='IGST';
        Q.Discount_Amount__c = 100;
        
        insert Q;  
        
          Q.Tax_Classification__c='IGST';
          Q.Discount_Amount__c = 100; 
          update Q ; 
        
        
        
      
       
        
        Quotelineitem QL = new Quotelineitem(quoteId=Q.id,SGST__c=6, CGST__c=6,IGST__c =0,Quantity =1, Unitprice=8000,pricebookentryid=lstPbentry[0].id);
        insert QL;  
        
           Opp[0].SyncedQuoteId = Q.id;
           update Opp[0];
           
            Q.Approved__c = true;   
          Update Q;
          
          Q.Approved__c = true;           
           update Opp[0];
        
        
          QuoteLineitem qli = [Select Id From QuoteLineItem where quoteId=:Q.id limit 1]; 
          Q.Tax_Classification__c='IGST';
          qli.SGST__c =6;
          QLI.CGST__c=6;
          QLI.IGST__c =0;          
          update qli;     
          
           
        
                
         
         
           
               
       
         Quote Q1 = new Quote();
        Q1 .Name = 'QuoteOrbit';
        Q1 .OpportunityId = Opp[0].id; 
        Q1 .Quote_Count__c = 00000000;
        Q1 .Quote_Number__c ='000000';
        Q1 .Approved__c = false;
        Q1 .Quotation_Date__c = date.newinstance(2013,3,5);
        Q1 .Pricebook2Id = lstPB[0].id;
        Q1 .Tax_Classification__c='IGST';
        Q1 .Discount_Amount__c = 100;
        
        insert Q1 ;    
        
        
        Q1 .Tax_Classification__c='SGST';  
        UPdate Q1;
                 
       
        
   
        
    }
    }
Shamsi 110Shamsi 110
Check in Developer console which lines are not covered.
Specially check each condition , you must have validation check for if and else in your test methods . 
jane1234jane1234
could you please attach the screenshot of coverage of test class in apex
shakila Gshakila G
Please see this User-added image
jane1234jane1234

this is a deployment error 

did you include your test class in your deployement?

shakila Gshakila G
Yes
jane1234jane1234
RUN THE TEST CLASS IN SANDBOX AND CHECK THE PERCENTAGE OF COVERAGE