-1
public with sharing class searchpn {
    //variables
    Public Id parentId;
    public Integer qliRowNum1{get;set;}
    public Integer qliRowNum{get;set;}
    public Integer finalRowNum{get;set;}
    public Integer rowToClone{get;set;}
    Public Integer deleteBatchRow{get;set;}
    Public Integer deleteQliRow{get;set;}
    //map variables
    Public Map<Integer,Quote_line_Item__c> qouteLineItemMap{get;set;}
    public Map<integer,List<Batch__c>> batchMap{get;set;}
    //List variables
    public List<Batch__c> batchListTodelete;
    public List<Quote_line_Item__c> qliListtoDelete;
     public List<QuoteWrapper> QuoteWrapperList{get;set;}
     public List<QuoteWrapperOne> QuoteWrapperListOne{get;set;}
    public List<QuoteWrapperTwo> QuoteWrapperListTwo{get;set;}
    // constructor declaration
    public searchpn(ApexPages.StandardsetController controller) {}

    public void PNSearch(){
        //varible declarations
        parentId=Apexpages.currentPage().getParameters().get('id');
        //Query to retrieve all Quote line Items which are childs to current Quote
        list< Quote_line_Item__c> allQliList=[Select id,Name,Client_P_N__c,Comments__c,Quote1__c,Quote_Validity__c ,Description__c,Standard_Estimated_Lead_Time__c,createdDate,Other_Lead_Time__c  from Quote_Line_Item__c where Quote1__c=:parentId ORDER BY CreatedDate ] ;

        set<string> qliNameSet =new set<string>();
        List<String> qliNameList=new List<String>();
        list<Quote_line_Item__c> matchQliList=new list<Quote_line_Item__c>();
        List<Quote__c> QuoteList=new List<Quote__c>();
        Set<Id> nonMatchedQutIds=new Set<Id>();
        Set<id> QlId=new Set<Id>();
        Quote__c currentQuote;
        //creating instance to wrpper classes
        QuoteWrapperListTwo=new List<QuoteWrapperTwo>();
        QuoteWrapperListOne=new List<QuoteWrapperOne>();
        QuoteWrapperList=new List<QuoteWrapper>();
        // Declaring and creating instance to map
        map<id,List<Quote_line_Item__c>> allMatchedQlis= new map<id,List<Quote_line_Item__c>>();
        try{
            currentQuote=[select Account_Name__r.Name,id from Quote__c where id=:ParentId LIMIT 1];
        }catch(Exception e){System.debug('<----Quote exception------>'+e); }
        //condition to check quote lIne item  map size is greater than zero
        if(allQliList.size()>0){
            //iterating the loop to store the name quote line items in list and in set
            for(Quote_line_Item__c qli:allQliList){
                qliNameSet.add(qli.Name);
                qliNameList.add(qli.Name);
            }//End of for loop
            // Query to get all quoteline items  which are matched with current Quote line items names
            try{
                matchQliList=[Select id,Name,Quote1__r.Quote_Number_New__c,Quote1__r.Account_Name__c,Quote1__r.Account_Name__r.Name,Quote1__r.createdDate,Quote1__r.Name,Quote1__c from Quote_line_Item__c where Quote1__c!=:ParentId And Name in:qliNameSet];
            }catch(Exception e){ system.debug('<----Quote line item exception--------- >'+e);}
            //Iterating the loop to store the Quote ids in to the set            
            for(Quote_line_Item__c q:matchQliList){
                QlId.add(q.Quote1__c);
            }
            //Query to get all quoteline items  which are matched with current Quote line items names
            try{
                QuoteList=[Select id,Quote_Number_New__c,Account_Name__c,Account_Name__r.Name,createdDate,(select id,Name,Quote1__r.Quote_Number_New__c,Quote1__r.Account_Name__c,Quote1__r.Account_Name__r.Name,Quote1__r.createdDate,Quote1__r.Name from Quote__c.Quote_line_Items__r) from Quote__c where id in:QlId ORDER BY CreatedDate  desc LIMIT 50000];
            }catch(Exception e){ System.debug('<***** Quote line item exception****>'+e);}
            //Iterating the loop to place the quote line items in a map with key as Quote id and value as quoteline items
            for(Quote__c qut:QuoteList){
                List<Quote_line_Item__c> qliList=new List<Quote_line_Item__c>();
                for(Quote_line_Item__c ql:qut.Quote_line_Items__r){
                    qliList.add(ql);
                }//End of for loop
                allMatchedQlis.put(qut.id,qliList);
            }//End of for loop
            //iterating the map to seperate the Quote values which have exact match with the current quote and non exact quotes
            for(Id mapkey:allMatchedQlis.keyset()){
                String accName=allMatchedQlis.get(mapkey)[0].Quote1__r.Account_Name__r.Name;
                List<String> dupQliNameList=new List<String>();
                //Iterating the loop to store all the Quoteline item's names for each quote
                for(Quote_line_Item__c qli:allMatchedQlis.get(mapkey)){
                    dupQliNameList.add(qli.Name);
                }//End of for loop
                // sorting the lists which helps us to compare the current QUote line item name with other Quote child Quotelineitem's name 
                dupQliNameList.sort();
                qliNameList.sort();
                //condition to check for exact match
                if(qliNameList.equals(dupQliNameList)&& allMatchedQlis.get(mapkey).size()==qliNameList.size()){
                    // condition to check with the current account name and Adding values to display exact match with same accont
                    if(accName==currentQuote.Account_Name__r.Name && mapkey!=ParentId){
                        QuoteWrapperTwo qwt=new QuoteWrapperTwo();
                        qwt.Quote_Name_Two=allMatchedQlis.get(mapkey)[0].Quote1__r.Name;
                        qwt.Quote_Id_Two=allMatchedQlis.get(mapkey)[0].Quote1__c;
                        qwt.Account_Name_Two=accName;
                        qwt.Account_Id_Two=allMatchedQlis.get(mapkey)[0].Quote1__r.Account_Name__c;
                        qwt.Quote_Num_Two=allMatchedQlis.get(mapkey)[0].Quote1__r.Quote_Number_New__c;
                        qwt.Count_Two=qliNameList.size();
                        qwt.CR_Date_Two=Date.valueOf(allMatchedQlis.get(mapkey)[0].Quote1__r.CreatedDate);
                        QuoteWrapperListTwo.add(qwt);
                    }//End of if 
                    //Adding values to display exact match with different accont
                    else{
                        QuoteWrapperOne qwt=new QuoteWrapperOne();
                        qwt.Quote_Name_One=allMatchedQlis.get(mapkey)[0].Quote1__r.Name;
                        qwt.Quote_Id_One=allMatchedQlis.get(mapkey)[0].Quote1__c;
                        qwt.Account_Name_One=accName;
                        qwt.Account_Id_One=allMatchedQlis.get(mapkey)[0].Quote1__r.Account_Name__c;
                        qwt.Quote_Num_One=allMatchedQlis.get(mapkey)[0].Quote1__r.Quote_Number_New__c;
                        qwt.Count_One=qliNameList.size();
                        qwt.CR_Date_One=Date.valueOf(allMatchedQlis.get(mapkey)[0].Quote1__r.CreatedDate);
                        QuoteWrapperListOne.add(qwt);
                    }
                }//End of if
                //Storing non exact matched quote ids into a set
                else {
                    nonMatchedQutIds.add(mapkey);    
                }
            }//End of for loop
            //Query to get the Quote values in descending order depending on the counting of Quotelineitem names  which are in non exact match
            AggregateResult[] groupedResults   = [SELECT   COUNT_DISTINCT(Name) nameCount,count( Id ) idCount  ,Min( Quote1__r.Name  )   QuoteName  ,Min( Quote1__c  )   QuoteId  ,Min( Quote1__r.Account_Name__r.Name ) AccountName, Min( Quote1__r.Account_Name__c ) AccountId ,Min(Quote1__r.Quote_Number_New__c)QuoteNum, MIN(Quote1__r.CreatedDate) CRDate  FROM Quote_Line_Item__c  where   ( name in :qliNameSet  AND Quote1__c != null AND  Quote1__c in :nonMatchedQutIds)  Group By Quote1__r.Id ORDER BY COUNT_DISTINCT(Name) desc, MIN(Quote1__r.CreatedDate)desc];
            //Iterating the loop to display non exact matched quotes
            for(AggregateResult aggr:groupedResults){
                QuoteWrapper qw=new QuoteWrapper();
                qw.Quote_Name=String.valueOf(aggr.get('QuoteName'));
                qw.Quote_Id=String.valueOf(aggr.get('QuoteId'));
                qw.Account_Name=String.valueOf(aggr.get('AccountName'));
                qw.Account_Id=String.valueOf(aggr.get('AccountId'));
                qw.Quote_Num=String.valueOf(aggr.get('QuoteNum'));
                qw.Count=Integer.valueOf(aggr.get('nameCount'));
                qw.CR_Date=Date.valueOf(aggr.get('CRDate'));   
                QuoteWrapperList.add(qw); 
            }//End of for loop
        }//End of if 
    }//End of method     
    // Method to save the QuoteLineItems as well as batches  
    //Wrapper class to display non exact matched Quotes
      public class QuoteWrapper {
        Public string Quote_Name{get;set;}
        Public string Quote_Id{get;set;}
        Public string Account_Name{get;set;}
        Public string Account_Id{get;set;}
        Public string Quote_Num{get;set;}
        Public Integer Count{get;set;}
        Public Date CR_Date{get;set;}          
    }//End of class
       public PageReference cancel1(){
        PageReference pgr = new PageReference('/'+parentId);  
        pgr.setRedirect(True);
        return pgr;
    }
    //Wrapper class to display exact match wtih another account
     public class QuoteWrapperOne {
        Public string Quote_Name_One{get;set;}
        Public string Quote_Id_One{get;set;}
        Public string Account_Name_One{get;set;}
        Public string Account_Id_One{get;set;}
        Public string Quote_Num_One{get;set;}
        Public Integer Count_One{get;set;}
        Public Date CR_Date_One{get;set;}          
    }//End of class
    //Wrapper class to display exact match with same account
    public class QuoteWrapperTwo {
        Public string Quote_Name_Two{get;set;}
        Public string Quote_Id_Two{get;set;}
        Public string Account_Name_Two{get;set;}
        Public string Account_Id_Two{get;set;}
        Public string Quote_Num_Two{get;set;}
        Public Integer Count_Two{get;set;}
        Public Date CR_Date_Two{get;set;}          
    }//End of wrapper class
}//End of main class

Test class:

@IsTest

private class Testsearcpn{
    static testmethod void testMethod1(){
        Test.startTest();
        Account acc=new account(Name='NicoTestacc',BillingCountry='India');
        insert acc;
        System.assertEquals(acc.name,'NicoTestacc');

        Account acc1=new account(Name='kvptestacc1',BillingCountry='China');
        insert acc1;
        System.assertEquals(acc1.name,'kvptestacc1');

        Opportunity op= new opportunity(Name='NicoTestOpp',CloseDate=date.today(),StageName='Qualification',Product_Type__c='DPI',Accountid=acc.Id);
        insert op;
        System.assertEquals(op.name,'NicoTestOpp'); 

        Opportunity op1= new opportunity(Name='NicoTestOpp',CloseDate=date.today(),StageName='Qualification',Product_Type__c='DPI',Accountid=acc1.Id);
        insert op1;
        System.assertEquals(op1.name,'NicoTestOpp');

        Quote__c q1 = New Quote__c (Opportunity_Name__c = op.Id,Account_Name__c = acc.Id,Subject__c='ABCD');
        insert q1;
        System.assertEquals(q1.Subject__c,'ABCD');

        Quote__c q2 = New Quote__c (Opportunity_Name__c = op1.Id,Account_Name__c = acc1.Id,Subject__c='ABCD');
        insert q2;
        System.assertEquals(q2.Subject__c,'ABCD');

        Quote__c q3 = New Quote__c (Opportunity_Name__c = op.Id,Account_Name__c = acc.Id,Subject__c='ABCD');
        insert q3;
        System.assertEquals(q3.Subject__c,'ABCD');


        Quote_Line_Item__c  qll = new Quote_Line_Item__c (Name='test',Quote1__c = q1.id);
        insert qll;
        System.assertEquals(qll.name,'test');

        Quote_Line_Item__c  ql2 = new Quote_Line_Item__c (Name=qll.Name,Quote1__c = q1.id);
        insert ql2;
        System.assertEquals(ql2.name,'test');
        batch__c bat =new batch__c( Asked_Qty__c ='1111',Quote_Line_Item__c = qll .id);
        insert bat;
        System.assertEquals(bat.Asked_Qty__c ,'1111');

        batch__c bat1 =new batch__c( Asked_Qty__c ='222',Quote_Line_Item__c = ql2 .id);
        insert  bat1;
        Test.stopTest();
        System.assertEquals(bat1.Asked_Qty__c ,'222');
        list<Quote_line_item__c> tst=[select id from quote_line_item__c where id =:qll.id];
                ApexPages.StandardsetController sc = new ApexPages.StandardsetController(tst);
        ApexPages.currentPage().getParameters().put('CF00NN0000000KbJB_lkid',q1.Id);
        searchpn nqlibc=new searchpn(sc); 
        searchpn.QuoteWrapper qw=new searchpn.QuoteWrapper();
        searchpn.QuoteWrapperOne qwo=new searchpn.QuoteWrapperOne();
        searchpn.QuoteWrapperTwo qwt=new searchpn.QuoteWrapperTwo();   
           nqlibc.PNSearch();
            nqlibc.cancel1();
    } 

This sections in controller not covering in test class

if(qliNameList.equals(dupQliNameList)&& allMatchedQlis.get(mapkey).size()==qliNameList.size()){
                        // condition to check with the current account name and Adding values to display exact match with same accont
                        if(accName==currentQuote.Account_Name__r.Name && mapkey!=ParentId){
                            QuoteWrapperTwo qwt=new QuoteWrapperTwo();
                            qwt.Quote_Name_Two=allMatchedQlis.get(mapkey)[0].Quote1__r.Name;
                            qwt.Quote_Id_Two=allMatchedQlis.get(mapkey)[0].Quote1__c;
                            qwt.Account_Name_Two=accName;
                            qwt.Account_Id_Two=allMatchedQlis.get(mapkey)[0].Quote1__r.Account_Name__c;
                            qwt.Quote_Num_Two=allMatchedQlis.get(mapkey)[0].Quote1__r.Quote_Number_New__c;
                            qwt.Count_Two=qliNameList.size();
                            qwt.CR_Date_Two=Date.valueOf(allMatchedQlis.get(mapkey)[0].Quote1__r.CreatedDate);
                            QuoteWrapperListTwo.add(qwt);
                        }//End of if 
                        //Adding values to display exact match with different accont
                        else{
                            QuoteWrapperOne qwt=new QuoteWrapperOne();
                            qwt.Quote_Name_One=allMatchedQlis.get(mapkey)[0].Quote1__r.Name;
                            qwt.Quote_Id_One=allMatchedQlis.get(mapkey)[0].Quote1__c;
                            qwt.Account_Name_One=accName;
                            qwt.Account_Id_One=allMatchedQlis.get(mapkey)[0].Quote1__r.Account_Name__c;
                            qwt.Quote_Num_One=allMatchedQlis.get(mapkey)[0].Quote1__r.Quote_Number_New__c;
                            qwt.Count_One=qliNameList.size();
                            qwt.CR_Date_One=Date.valueOf(allMatchedQlis.get(mapkey)[0].Quote1__r.CreatedDate);
                            QuoteWrapperListOne.add(qwt);
                        }
                    }//End of if
                    //Storing non exact matched quote ids into a set
                    else {
                        nonMatchedQutIds.add(mapkey);    
                    }
                }//End of for loop
                //Query to get the Quote values in descending order depending on the counting of Quotelineitem names  which are in non exact match
                AggregateResult[] groupedResults   = [SELECT   COUNT_DISTINCT(Name) nameCount,count( Id ) idCount  ,Min( Quote1__r.Name  )   QuoteName  ,Min( Quote1__c  )   QuoteId  ,Min( Quote1__r.Account_Name__r.Name ) AccountName, Min( Quote1__r.Account_Name__c ) AccountId ,Min(Quote1__r.Quote_Number_New__c)QuoteNum, MIN(Quote1__r.CreatedDate) CRDate  FROM Quote_Line_Item__c  where   ( name in :qliNameSet  AND Quote1__c != null AND  Quote1__c in :nonMatchedQutIds)  Group By Quote1__r.Id ORDER BY COUNT_DISTINCT(Name) desc, MIN(Quote1__r.CreatedDate)desc];
                //Iterating the loop to display non exact matched quotes
                for(AggregateResult aggr:groupedResults){
                    QuoteWrapper qw=new QuoteWrapper();
                    qw.Quote_Name=String.valueOf(aggr.get('QuoteName'));
                    qw.Quote_Id=String.valueOf(aggr.get('QuoteId'));
                    qw.Account_Name=String.valueOf(aggr.get('AccountName'));
                    qw.Account_Id=String.valueOf(aggr.get('AccountId'));
                    qw.Quote_Num=String.valueOf(aggr.get('QuoteNum'));
                    qw.Count=Integer.valueOf(aggr.get('nameCount'));
                    qw.CR_Date=Date.valueOf(aggr.get('CRDate'));   
                    QuoteWrapperList.add(qw); 
                }//End of for loop
            }//End of if 

Can any body help to get the code coverage for the controller

Thankyou in advance.

2
  • seealldata=true it is showing full code coverage in sandbox .while the same i used in production it is not showing it is showing only 50% code coverage
    – Teja
    Jul 29, 2014 at 11:18
  • You are hardcoding the id for current page as in 'CF00NN0000000KbJB_lkid'. Try adding a id to pagereference as current page.
    – MnZ
    Oct 10, 2014 at 22:06

1 Answer 1

2

Here is the issue:

Your controller expects to get a parameter with key = 'id'

public void PNSearch(){
    //varible declarations
    parentId=Apexpages.currentPage().getParameters().get('id');

Yet your test method creates a parameter with a different key:

ApexPages.currentPage().getParameters().put('CF00NN0000000KbJB_lkid',q1.Id);
    searchpn nqlibc=new searchpn(sc); 
    searchpn nqlibc=new searchpn(sc); 
    searchpn.QuoteWrapper qw=new searchpn.QuoteWrapper();
    searchpn.QuoteWrapperOne qwo=new searchpn.QuoteWrapperOne();
    searchpn.QuoteWrapperTwo qwt=new searchpn.QuoteWrapperTwo();   
    nqlibc.PNSearch();

hence parentId in the controller is null

You can also simplify your testmethod somewhat by doing bulk operations, for example

List<Quote__c> qList = new List<Quote__c> {
  new Quote__c(Opportunity_Name__c = oList[0].Id,Account_Name__c = aList[0].Id,Subject__c='ABCD'),
  new Quote__c (Opportunity_Name__c = oList[1].Id,Account_Name__c = aList[1].Id,Subject__c='ABCD'),
  new Quote__c (Opportunity_Name__c = oList[0].Id,Account_Name__c = aList[0].Id,Subject__c='ABCD')
};
insert qList;

and equivalent for the accounts, opportunities, and quote items. This helps avoid too many SOQL limits in test methods, and verifies your triggers on these objects, if any, are properly bulkified

and lastly, in Quote__c, the field opportunity_name__c is not best practice for naming an ID field (confuses the reader in thinking it is the opportunity name - a text field) . SFDC convention would be to name this field opportunity__c.

Not the answer you're looking for? Browse other questions tagged .