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
Sasidhar Reddy BSasidhar Reddy B 

Test Class code coverage for SOQL Quries

Hi Guys,

I coverd 27% code coverage .

Can you please guide me ? How to cover list<Loyalty__c> ListRecods Soql Qureis? 


public class SearchAndEditLoyaltyRecord {
   public String Email{get;set;}
    public String Email1{get;set;}
    public String HomePhone{get;set;}
    public String HomePhone1{get;set;}
    public String mobilePhone{get;set;}
    public String mobilePhone1{get;set;}
    public String name{get;set;}
     public String name1{get;set;}
    public String BusinessPhone{get;set;}
     public String BusinessPhone1{get;set;}
    public String ContactName{get;set;}
    public String ContactName1{get;set;}
    public List<Loyalty__c> Loyalty_records{get;set;}
     public List<Loyalty__c> Loyalty_records1 {get;set;}
    public List<loyaltyWrapper> loyaltywrapperlist{get; set;}
    public List<loyaltyWrapper1> loyaltywrapperlist1{get; set;}
    Public list<Loyalty__c> SelectedLoyalty{get; set;}
    public Boolean Searched{get;set;}
    public Boolean searched2 {get;set;}
     
    
    public SearchAndEditLoyaltyRecord(){
        searched = false;
        searched2 = false;
       
        //SelectedLoyalty=new list<Loyalty__c>();
        String EmailtoSearch = ApexPages.currentPage().getParameters().get('Email');
        String HomePhonetoSearch = ApexPages.currentPage().getParameters().get('HomePhone');
        String mobilePhonetoSearch = ApexPages.currentPage().getParameters().get('mobilePhone');
        String NametoSearch = ApexPages.currentPage().getParameters().get('name');
        String BusinessPhonetoSearch = ApexPages.currentPage().getParameters().get('BusinessPhone');
         String contactNametoSearch = ApexPages.currentPage().getParameters().get('ContactName');
         
          String EmailtoSearch1 = ApexPages.currentPage().getParameters().get('Email1');
        String HomePhonetoSearch1 = ApexPages.currentPage().getParameters().get('HomePhone1');
        String mobilePhonetoSearch1 = ApexPages.currentPage().getParameters().get('mobilePhone1');
        String NametoSearch1 = ApexPages.currentPage().getParameters().get('name1');
        String BusinessPhonetoSearch1 = ApexPages.currentPage().getParameters().get('BusinessPhone1');
         String contactNametoSearch1 = ApexPages.currentPage().getParameters().get('ContactName1');
         
        if(null!=EmailtoSearch||null!=HomePhonetoSearch||
        null!=mobilePhonetoSearch ||null!=NametoSearch||null!=BusinessPhonetoSearch||null!=contactNametoSearch ){
            Email=EmailtoSearch;
            HomePhone=HomePhonetoSearch ;
            mobilePhone=mobilePhonetoSearch ;
            Name=NametoSearch;
            BusinessPhone=BusinessPhonetoSearch;
            ContactName=contactNametoSearch;
            system.debug('BusinessPhone>>>>>>>'+BusinessPhone);
            executeSearch();
           // newLoyalty();
        }
        
        if(null!=EmailtoSearch1||null!=HomePhonetoSearch1||
        null!=mobilePhonetoSearch1 ||null!=NametoSearch1||null!=BusinessPhonetoSearch1||null!=contactNametoSearch1 ){
            Email1=EmailtoSearch1;
            HomePhone1=HomePhonetoSearch1 ;
            mobilePhone1=mobilePhonetoSearch1 ;
            Name1=NametoSearch1;
            BusinessPhone1=BusinessPhonetoSearch1;
            ContactName1=contactNametoSearch1;
            system.debug('BusinessPhone>>>>>>>'+BusinessPhone1);
            //executeSearch();
         newLoyalty();
        }
    }
    
   
    public PageReference executeSearch(){
    
        if(Loyalty_records==null){
            SelectedLoyalty=new list<Loyalty__c>();
           Loyalty_records=new list<Loyalty__c>();
       }else{
           Loyalty_records.clear();
           SelectedLoyalty.clear();
       }
        String SearchMail = email;
        String SearchmobilePhone=mobilePhone;
        String SearchHomephone =HomePhone;
        String SearchLoyaltyId = Name;
        String SearchBusinessPhone =BusinessPhone;
        String SearchContactName = contactName;
        system.debug('SearchBusinessPhone >>>>>>>>'+SearchBusinessPhone );
        searched = true;
        system.debug('SearchMail >>>>>>>>'+SearchMail);
        system.debug('SearchMail >>>>>>>>'+SearchMail.length() );
        if(SearchMail!=''&&SearchMail.length()!=0||SearchmobilePhone!=''&&SearchmobilePhone.length()!=0 ||
        SearchHomephone !=''&&SearchHomephone .length()!=0 ||
        SearchLoyaltyId !=''&&SearchLoyaltyId .length()!=0 || SearchBusinessPhone !=''&&SearchBusinessPhone .length()!=0
        || SearchContactName !=''&&SearchContactName.length()!=0){
          
           if(SearchMail!=''&&SearchMail.length()!=0)
        Loyalty_records =[SELECT Id,Name,Type__c,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.Email  =:SearchMail  order by Loyalty_Program_Expiry_Date__c desc ];
                    
                         }
                            if(SearchmobilePhone!=''&&SearchmobilePhone.length()!=0){
        Loyalty_records =[SELECT Id,Name,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.mobilePhone,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.mobilePhone=:SearchmobilePhone order by Loyalty_Program_Expiry_Date__c desc ];
                    
                         }
                                        if(SearchHomephone !=''&&SearchHomephone .length()!=0){
        Loyalty_records =[SELECT Id,Name,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.mobilePhone,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,Contact_Name__r.Homephone,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.Homephone=:SearchHomephone  order by Loyalty_Program_Expiry_Date__c desc ];
                    
                         }
                          if(SearchLoyaltyId !=''&&SearchLoyaltyId .length()!=0){
        Loyalty_records =[SELECT Id,Name,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.mobilePhone,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  name=:SearchLoyaltyId  order by Loyalty_Program_Expiry_Date__c desc nulls last ];
                    
                         }
          if(SearchBusinessPhone.length()!=0){
        Loyalty_records =[SELECT Id,Name,Contact_Name__r.Business_Phone__c,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.mobilePhone,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.Business_Phone__c =:SearchBusinessPhone  order by Loyalty_Program_Expiry_Date__c desc Nulls Last];
                    
                         }
                          if(SearchContactName.length()!=0){
        Loyalty_records =[SELECT Id,Name,Contact_Name__r.Business_Phone__c,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.mobilePhone,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.name=:SearchContactName order by Loyalty_Program_Expiry_Date__c desc Nulls Last];
                    
                         }
                         
                        /* else {
                          if(SearchMail  == '' || SearchMail == null||SearchmobilePhone== '' || SearchmobilePhone== null||SearchHomephone == '' || SearchHomephone == null){
                system.debug('yes it is entering into this');
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Valid Input'));         
                  } 
                        
                  }*/
                        system.debug('Loyalty_records>>>>'+Loyalty_records); 
         
                
                   /*if(SearchMail != ''&& Loyalty_records.size()==0 )  {
                     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No Loyalty Records With this contact Mail'));     
                  }
                  else {*/
                  if(SearchMail != ''&& Loyalty_records.size()>0  )
                  {
                    Loyalty_records =[SELECT Id,Name,Type__c,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where Contact_Name__r.Email  LIKE :SearchMail  order by Loyalty_Program_Expiry_Date__c desc];
                  }
                    if(SearchmobilePhone!= ''&& Loyalty_records.size()>0 )
                  {
                    Loyalty_records =[SELECT Id,Name,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.MobilePhone,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.mobilePhone =:SearchmobilePhone order by Loyalty_Program_Expiry_Date__c desc];
                  }
                   if(SearchHomephone != ''&& Loyalty_records.size()>0 )
                  {
                    Loyalty_records =[SELECT Id,Name,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.HomePhone,Contact_Name__r.MobilePhone,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.Homephone=:SearchHomephone order by Loyalty_Program_Expiry_Date__c desc];
                  }
                 if(SearchLoyaltyId != ''&& Loyalty_records.size()>0 )
                  {
                    Loyalty_records =[SELECT Id,Name,Contact_Name__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.HomePhone,Contact_Name__r.MobilePhone,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Name=:SearchLoyaltyId order by Loyalty_Program_Expiry_Date__c desc];
                  }
         if(SearchBusinessPhone!=''&& Loyalty_records.size()>0 )
                  {
                    Loyalty_records =[SELECT Id,Name,Contact_Name__c,Contact_Name__r.Business_Phone__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.HomePhone,Contact_Name__r.MobilePhone,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.Business_Phone__c=:SearchBusinessPhone order by Loyalty_Program_Expiry_Date__c desc];
                  }
                    if(SearchContactName!=''&& Loyalty_records.size()>0  )
                  {
                    Loyalty_records =[SELECT Id,Name,Contact_Name__c,Contact_Name__r.Business_Phone__c,Loyalty_Eligibility__c,Loyalty_Inclusion__c,Contact_Name__r.HomePhone,Contact_Name__r.MobilePhone,Contact_Name__r.Email,Contact_Name__r.Name,Contact_Name__r.Sales_Lifetime_Value__c,
                         Loyalty_Program_Exclusion__c,Loyalty_Lifetime_Value__c,Loyalty_Program_Expiry_Date__c,
                         Loyalty_Sales_Inclusion_Value__c FROM Loyalty__c where  Contact_Name__r.Name=:SearchContactName order by Loyalty_Program_Expiry_Date__c desc];
                  }
                   system.debug('Loyalty_records >>>>>>'+Loyalty_records );
            
        
        return null;
    }
}
Best Answer chosen by Sasidhar Reddy B
M.sfM.sf
Hi
Test class is just like you are doing a unit testing on your code.
You can create data of the all the objects/settings in the test class and do update of data if required. If you can create enough data that satify all your business reqirements it will cover the code automatically . Else you might written some extar code or missed some businees cases.

A very bad way to cover the code is when you provide seealldata = true. But Salesforcesuggest now to follow this way to cover the code.

Hope this helps,

Thanks

All Answers

GarryPGarryP
You should create the test data which is needed to run this code.
In your case you will have to create all test data which is dependent to your Loyality records includinga all values that are being refrenced in thos code or required for the logic calculation
M.sfM.sf
Hi
Test class is just like you are doing a unit testing on your code.
You can create data of the all the objects/settings in the test class and do update of data if required. If you can create enough data that satify all your business reqirements it will cover the code automatically . Else you might written some extar code or missed some businees cases.

A very bad way to cover the code is when you provide seealldata = true. But Salesforcesuggest now to follow this way to cover the code.

Hope this helps,

Thanks
This was selected as the best answer