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
SFDC Forum 007SFDC Forum 007 

Test class for the following apex class

Hi,

 

Please help me to write a testclass for the following apex class

 

 

//Apex class to generate a PDF from Account view page on button click

//Displays all the business account details, Person account details, brands with related BBL information,time,lastvisit,callobjective


public class CallReportExtn{
    
    public class PdfWrapper{
        public Call_Report__c cr{get;set;}
        public List<PADetails> pernAcctList{get;set;}
        public List<PADetails> pernAcctList2{get;set;}
        public Call_Item__c callitemList{get;set;}
        public List<Customer_Brand__c> customerbrndbbl {get;set;}
        public String baId{get;set;}
    }
    public class PADetails{
        public List<Contact> cclist{get;set;}
        public String paName{get;set;}
        public String paaccName{get;set;}
        public String paJobTitle{get;set;}
        public String paLastvisit{get;set;}
        public String paBrand1{get;set;}
        public String paBrand2{get;set;}
        public String paBrand3{get;set;}
        public String callObjective1{get;set;}
        public String callObjective2{get;set;}
        public String crdate{get;set;}
    }
    set<id> top3bbl =  new set<id>();      
    public String brand1{get;set;}
    public String brand2{get;set;}
    public String brand3{get;set;}
    public list<string> callnames{get;set;}
    public List<Call_Report__c> crList{get;set;}
    public List<Customer_Brand__c> cbList{get;set;}
    public List<PdfWrapper> pdfWrapperList{get;set;}
    public List<PADetails> PADetailsList{get;set;}    
    public String userid=UserInfo.getUserId();
    Map<Id,List<PADetails>> paMap=new Map<Id,List<PADetails>>();
    Map<Id,Call_Item__c> callitempaMap=new Map<Id,Call_Item__c>();
    Map<Id,Call_Report__c> callReppaMap=new Map<Id,Call_Report__c>();
    Map<String,Customer_Brand__c> cCBpaMap=new Map<String,Customer_Brand__c>();
    Map<String,Contact> conmap=new Map<String,Contact>();
    Map<Id,List<PADetails>> cpmap=new Map<Id,List<PADetails>>();
    List<Customer_Brand__c> customerbrnd = new List<Customer_Brand__c>();
    List<Call_Item__c>  calltemplst=new List<Call_Item__c>();
        
        public CallReportExtn(ApexPages.StandardController controller){
        pdfWrapperList=baAccInfo();       
    }
    public List<PdfWrapper> baAccInfo(){
        List<PdfWrapper> pdfWrapper=new List<PdfWrapper>();
        Set<Id> accIdSet=new Set<Id>();
        Set<Id> callplanIdSet=new Set<Id>();
        if(userid !=null)
        //Fetching all the business account fields from call report
        crList=[select id,Contact__c,Date__c,Call_plan__c,Account__r.Name,Account__c,Account__r.id,Account__r.BillingCity,Account__r.BillingStreet,Account__r.BillingState,Account__r.BillingCountry,Account__r.BillingPostalCode,Account__r.phone,Account__r.Type,Account__r.Segment_Status__c,Account__r.Total_Number_Of_Patients__c,Account__r.No_of_Seats__c,Objective__r.Name,Objective2__r.Name from Call_Report__c where CallDate__c=TODAY and OwnerId=:userid];
       System.debug('crList==> '+crList);   
        for(Call_Report__c callRep:crList)
        {
             if(callRep.Account__c !=null)
             {
              if(!accIdSet.contains(callRep.Account__c))
              {
                 accIdSet.add(callRep.Account__c);
                   

                 }
           }
                 
            if(callRep.Call_plan__c !=null)
            {
            if(!callplanIdSet.contains(callRep.Call_plan__c))
            callplanIdSet.add(callRep.Call_plan__c);
              }
              callReppaMap.put(callRep.Contact__c,callRep);   
                             
                                    
        }  
        //Fetching all the person accounts from a list
       List<Account> paList=[select Name,Job_Title__pc,Primary_Customer__pc,id,PersonContactId,Last_Activity__pc  from Account where Primary_Customer__pc in:accIdSet];
       //Fetching the last activity date from contact object
       List<Contact> cclist=[select Last_Activity__c from contact where Last_Activity__c !=null];
        System.debug('cclist ==> '+cclist );
        //Fetching the top three priority brands id callplan is added to callreport  
        if(callplanIdSet.size()>0){
             calltemplst = [select id,Brand__c,Brand__r.name,Position__c,Brand_Call_Plan__c from Call_Item__c where Brand_Call_Plan__c in:callplanIdSet ORDER BY Position__c limit 3];
             System.debug('calltemplst ==> '+calltemplst );        
        }
            
            
      
        for(Id baId:accIdSet){
        //Fetching all the person account headers and details dynamically
            PADetailsList=new List<PADetails>();           
            PADetails paDetHeader=new PADetails();
            paDetHeader.paName='Name';
            paDetHeader.paJobTitle='Job Title';
        
           
            if(crList!=null && crList.size()>0 && crList.get(0)!=null && crList.get(0).Date__c!=null){
                paDetHeader.crdate='Time';     
            }else{
                paDetHeader.crdate='';    
            }
            
           if(calltemplst!=null && calltemplst.size()>0 && calltemplst.get(0)!=null && calltemplst.get(0).Brand__r.name!=''){
                paDetHeader.paBrand1=calltemplst.get(0).Brand__r.name;
                top3bbl.add(calltemplst.get(0).Brand__c);               
                                
            }else{
                paDetHeader.paBrand1='';    
            }
            
            
       
            if(calltemplst!=null && calltemplst.size()>0 && calltemplst.get(0)!=null && calltemplst.get(0).Brand__r.name!=''){
                paDetHeader.paBrand1=calltemplst.get(0).Brand__r.name;
                top3bbl.add(calltemplst.get(0).Brand__c);               
                                
            }else{
                paDetHeader.paBrand1='';    
            }
            
            if(calltemplst!=null && calltemplst.size()>1 && calltemplst.get(1)!=null && calltemplst.get(1).Brand__r.name!=''){
                paDetHeader.paBrand2=calltemplst.get(1).Brand__r.name;  
                top3bbl.add(calltemplst.get(1).Brand__c);                
            }else{
                paDetHeader.paBrand2='';    
            }
            
            if(calltemplst!=null && calltemplst.size()>2 && calltemplst.get(2)!=null && calltemplst.get(2).Brand__r.name!=''){
                paDetHeader.paBrand3=calltemplst.get(2).Brand__r.name;
                top3bbl.add(calltemplst.get(2).Brand__c);               
            }else{
                paDetHeader.paBrand3='';    
            }
            
            
             if(crList!=null && crList.size()>0 && crList.get(0)!=null && crList.get(0).Objective__r.Name!=''){
                paDetHeader.callObjective1='Objective1';     
            }else{
                paDetHeader.callObjective1='';    
            }
            
            if(crList!=null && crList.size()>0 && crList.get(0)!=null && crList.get(0).Objective__r.Name!=''){
                paDetHeader.callObjective2='Objective2';     
            }else{
                paDetHeader.callObjective2='';    
            }
            
             if(cclist!=null){
                paDetHeader.paLastvisit='Lastvisit';     
            }else{
                paDetHeader.paLastvisit='';    
            }
            
           //Fetching the top three brands from customer brand object based on related HCP
       if(top3bbl.size()>0){
           cbList= [select BBL__c,HCP__c,Brand__r.Name from Customer_Brand__c where  Brand__c in:top3bbl and HCP__c in:callReppaMap.keyset()];
           System.debug('cbList==> '+cbList);
           if(cbList.size()>0)
           {
           for(Customer_Brand__c cb:cbList){
               cCBpaMap.put(cb.HCP__c+cb.Brand__r.Name,cb);    
           }
           }
       }
       
      
            
            PADetailsList.add(paDetHeader);
                   
            for(Account ac:paList){
                    System.debug('ac.Name ==> '+ac.Name);
                    PADetails paDet=new PADetails();
                    paDet.paName=ac.Name;
                    if(ac.Primary_Customer__pc !=null)
                    
                    paDet.paJobTitle=ac.Job_Title__pc;
                    
                 if(callReppaMap.get(ac.personContactId)!=null){
                    paDet.crdate=String.valueof(callReppaMap.get(ac.personContactId).Date__c.format('hh:mm'));
                    }else{
                        paDet.crdate='';

                    }
 
                   
                   
                       
                  
                    if(callReppaMap.get(ac.personContactId)!=null){
                        paDet.callObjective1=callReppaMap.get(ac.personContactId).Objective__r.Name;
                    }else{
                        paDet.callObjective1='';

                    }
                    if(callReppaMap.get(ac.personContactId)!=null){
                        paDet.callObjective2=callReppaMap.get(ac.personContactId).Objective2__r.Name;
                    }else{
                        paDet.callObjective2='';

                    }
                    if(calltemplst.size()>0)
                    {
                    if(cCBpaMap.get(ac.personContactId+calltemplst.get(0).Brand__r.name)!=null){
                        paDet.paBrand1=cCBpaMap.get(ac.personContactId+calltemplst.get(0).Brand__r.name).BBL__c;
                    }else{
                        paDet.paBrand1='';

                    }
                    }
                    if(calltemplst.size()>1)
                    {
                    if(cCBpaMap.get(ac.personContactId+calltemplst.get(1).Brand__r.name)!=null){
                        paDet.paBrand2=cCBpaMap.get(ac.personContactId+calltemplst.get(1).Brand__r.name).BBL__c;
                    }else{
                        paDet.paBrand2='';

                    }
                    }
                    if(calltemplst.size()>2)
                    {
                    if(cCBpaMap.get(ac.personContactId+calltemplst.get(2).Brand__r.name)!=null){
                        paDet.paBrand3=cCBpaMap.get(ac.personContactId+calltemplst.get(2).Brand__r.name).BBL__c;
                    }else{
                        paDet.paBrand3='';

                    }
                    }                   
                    if(ac.Last_Activity__pc!=null){
                        System.debug('ac.Last_Activity__pc ==> '+ac.Last_Activity__pc);
                        String str=String.valueOf(ac.Last_Activity__pc);                        
                        paDet.paLastvisit=str.substring(8,10)+'/'+str.substring(5,7)+'/'+str.substring(0,4);
                       
                        
                     }
                     
                     else
                     {
                         paDet.paLastvisit='';
                         
                     }
                     
                 
                     
                     
                     
                    
                     if(ac.Primary_Customer__pc !=null)
                     {
                    if(baId == ac.Primary_Customer__pc)
                    {
                    PADetailsList.add(paDet);
                    paMap.put(baId,PADetailsList);    
                    }
                    }
          }
          
         }
        
         
        if(crList.size()>0){
        Map<String,String> baIdMap=new Map<String,String>();
        for(Call_Report__c callRep:crList){
            if(baIdMap!=null && baIdMap.get(callRep.Account__c)==null){    
                PdfWrapper pw=new PdfWrapper();
                pw.cr=callRep;
                if(callRep.Account__c !=null){
                    baIdMap.put(callRep.Account__c,callRep.Account__c);
                    pw.pernAcctList=paMap.get(callRep.Account__c);
                }
           
                if(callRep.Call_plan__c !=null)
                pw.callitemList=callitempaMap.get(callRep.Call_plan__c);
                pdfWrapper.add(pw);
            }
                                
        }
        }
        
        return pdfWrapper;
    }
    
    

}

SFDC Forum 007SFDC Forum 007

Hi,

 

 I have written the test class for the above class try to cover the  test coverage for accounts and cal report object alone, but the class coverage stillremains null , Please help me with this

 

here is my code

 

 @isTest
 private class CallReportExtnTest 
 {
 
 public static testMethod void CallReportExtn()
 {    
 
    Test.startTest();
    User usr=[select Id,UserName from User where email='test@test.com'];
    RecordType rt=[select id,developerName from RecordType where sObjectType='Account' and DeveloperName='Business_Account'];
 
        Account acc = new account();
        acc.Name ='Test Account';
        acc.RecordTypeId=rt.Id;
        acc.type='Hospital';
        acc.BillingStreet='No:12, Sagar street';
        acc.Billingcity='Hyderabad';
        acc.BillingCountry='India';
        acc.BillingState='Andhra Pradesh';
        acc.BillingPostalCode='500052';
        acc.phone='893964563';
        acc.type='Organization';
        acc.Segment_Status__c='a';
        acc.Total_Number_Of_Patients__c=2;
        acc.No_of_Seats__c=2;
        insert acc;
 
      Call_Report__c cr = new Call_Report__c();
       cr.MM_Account__c = acc.Id;
       cr.Name = 'Test ';
      // cr.Objective__r.Name='test';
      // cr.Objective2__r.Name ='callreport2';
       insert cr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Test.stopTest();
 
 }
 }