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

test class values dynamically fetching

Hi,

 

 I have the test class written for the class which covers 81%, but i have hardcoded the ids of certian fields 

 

eg:cr.Call_plan__c='a0TW0000000eedn';

 

 

if i move this class to pd that might encounter an test class failure, since those ids wont be present,

 

Please help me hw do it with my code itself

 

@isTest
private class CallReportExtnTest
{


public class PADetails{


}
public static testMethod void CallReportExtn()
{


Test.startTest();
//User usr=[select id,UserName from User where OwnerId=:UserInfo.getUserId()];
//RecordType rt=[select id,developerName from RecordType where sObjectType='Account' and DeveloperName='Business Account'];
String RecTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;
ApexPages.StandardController sc = new ApexPages.standardController(new Call_Report__c());

// create an instance of the controller
CallReportExtn cre = new CallReportExtn(sc);

Call_Report__c cr = new Call_Report__c();
cr.Account__c = '001W00000072EXM';
cr.Name = 'Test GSK';
cr.Date__c=Date.today();
cr.OwnerId=UserInfo.getUserId();
cr.Call_plan__c='a0TW0000000eedn';
// cr.Objective__r.Name='gsktest';
// cr.Objective2__r.Name ='GSK Call report2';

insert cr;

Account acc = new account();
//acc.Name ='Test Account';
acc.RecordTypeId=RecTypeId;
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;
acc.LastName = 'Test';
acc.Job_Title__pc='Dentist';
//acc.Primary_Customer__pc ='001W00000072EXM';

//acc.Last_Activity__pc =Date.Today();


insert acc;



CallReportExtn.PADetails CRPAD = new CallReportExtn.PADetails();
Contact con = new Contact();
con.LastName = 'Test';
con.Phone='6853235351';
con.Job_Title__c='Dentist';
con.Has_a_computer__c='yes';
insert con;




CallReportExtn.PdfWrapper wrapper= new CallReportExtn.PdfWrapper();






Brand_Lookup__c bpl = new Brand_Lookup__c();
bpl.Brand_Priority__c='1';
bpl.Name='a0UW00000006P2k';
insert bpl ;







Call_Item__c ci= new Call_Item__c ();

//ci.id='a0WW0000000WEKu';
ci.Brand__c='a0UW00000006P4a';
//ci.Brand__r.name='test';
ci.Position__c='1';
ci.Brand_Call_Plan__c='a0TW0000000ecJv';
ci.Call_Template__c='a0TW0000000ecJv';
insert ci;
//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];



Customer_Brand__c cb = new Customer_Brand__c ();
cb.BBL__c='Unknown';
cb.HCP__c='003W0000009LKIz';
insert cb;


cre.baAccInfo();









Test.stopTest();

}
}

 

Thanks in Advance

Ron ReedRon Reed

You should be inserting sample data for all of the records you are trying to lookup to, and use the Ids of the newly create data. Something like this:

 

Account a = new Account(Name='My Account');
insert a;

Call_Plan__c cp = new Call_Plan__c(Name='My Call Plan');
insert cp;

Call_Report__c cr = new Call_Report__c();
cr.Account__c = a.Id;
cr.Name = 'Test GSK';
cr.Date__c=Date.today();
cr.OwnerId=UserInfo.getUserId();
cr.Call_plan__c = cp.id;

 

This is a good starting point:

How To Write Good Unit Tests

 

 

SFDC 007SFDC 007

yes i have written my class in the same way , but nw the thing is it decreases the percentage to 64%, while harding the ids it was 85%

 

 

This is my modified test  class

 

 

@isTest
 private class CallReportExtnTest 
 {




public class PADetails{




}
 public static testMethod void CallReportExtn()
 {    


 
    Test.startTest();
  //User usr=[select id,UserName from User where  OwnerId=:UserInfo.getUserId()];
  //RecordType rt=[select id,developerName from RecordType where sObjectType='Account' and DeveloperName='Business Account'];
   RecordType Cprt=[select id,developerName from RecordType where sObjectType='Call_Template__c' and DeveloperName='Call_Plan'];
   String CprtId=[select id,developerName from RecordType where sObjectType='Call_Template__c' and DeveloperName='Call_Plan'].Id;
    String RecTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;
   ApexPages.StandardController sc = new ApexPages.standardController(new Call_Report__c());


      // create an instance of the controller

 
        Account acc = new account();
        //acc.Name ='Test Account';
        acc.RecordTypeId=RecTypeId;
        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;
        acc.LastName = 'Test';
        acc.Job_Title__pc='Dentist';
        //acc.Primary_Customer__pc ='001W00000072EXM';
       
        //acc.Last_Activity__pc =Date.Today();     
       insert acc; 
       
     
       
       
  Date dt = Date.today();
  Call_Template__c cp = new Call_Template__c();
  cp.RecordTypeId =CprtId;
  cp.Name = 'Test Call Plan';
  cp.Description__c='Desc';
  cp.Cycle__c = '1';
  cp.Start_Date__c =dt;
  cp.End_date__c=dt.addDays(2);
  insert cp;  

  
  
CallReportExtn cre = new CallReportExtn(sc);
  
Call_Report__c cr = new Call_Report__c();
  cr.Account__c = acc.Id;
  cr.Name = 'Test GSK';
  cr.Date__c=Date.today();
  cr.OwnerId=UserInfo.getUserId();
  cr.Call_plan__c=cr.Id;
// cr.Objective__r.Name='gsktest';
//  cr.Objective2__r.Name ='GSK Call report2';
 
  insert cr;


 
 
 
   CallReportExtn.PADetails   CRPAD = new CallReportExtn.PADetails();
         Contact con = new Contact();
         con.LastName = 'Test';
         con.Phone='6853235351';
         con.Job_Title__c='Dentist';
         con.Has_a_computer__c='yes';
         insert con;
         
         
         
         
   CallReportExtn.PdfWrapper wrapper= new CallReportExtn.PdfWrapper();
 
 
 
 
     
  
   Brand_Lookup__c bpl = new Brand_Lookup__c();
   bpl.Brand_Priority__c='1';
  // bpl.Name='test';
   insert bpl ;
   
   
Customer_Brand__c cb = new Customer_Brand__c ();
 cb.BBL__c='Unknown';
 cb.HCP__c=con.Id;
 insert cb;
   
   
   
   
 
 Call_Item__c  ci= new Call_Item__c ();
 
 //ci.id='a0WW0000000WEKu';
 ci.Brand__c=bpl.Id;
 //ci.Brand__r.name='test';
 ci.Position__c='1';
 ci.Brand_Call_Plan__c=ci.Id;
 ci.Call_Template__c=cp.Id;
 insert ci;
 //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];
 
 
 
 
 


 
 cre.baAccInfo();
 
 
 
 
 
 
 
 
 
 Test.stopTest();
 
 }
 }