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
DipthiDipthi 

How can I get Name filed value (standard field) from Contact in to LeaveEntry Obj in a Test Class

I am new to Apex and working on Leave Management App. Contact is Parent to LeaveEntry Object having Master-detail relationship on Name field.
( On contact Object while creating a record, I have First Name and LastName but on LeaveEntry Object while creating record I will select Employee full name which will populate from Contact ) 

I need clarification on 2 issues where I see errors (made their font bold)
1. Why am I getting Null value when I want to retrieve Name at System.debug
2. How can I pull Name value from contact to LeaveEntry?

@isTest
public class OnLeaveApprovalTest {
 
@isTest
public static void CreateContactRecord(){
Contact emp = new Contact ();

emp.FirstName = 'Jon';
emp.LastName = 'Snow';
emp.Emp_ID__c = 'Sp-34324';
insert emp;
system.debug('Full Name of emp is : ' + emp.Name);  // plz clarify
system.debug(emp);
 
//Enter record details on Leaves Object
Leaves__c leaveReq = new Leaves__c();
Emp Name = emp.FirstName + ' ' + emp.LastName;  //Plz Clarify
leaveReq.Request_Start_Date__c = date.newInstance(2020,06,10);
leaveReq.Request_End_Date__c = date.newInstance(2020,06,15);
Insert leaveReq;
  
}
}
Best Answer chosen by Dipthi
Santosh Kumar 348Santosh Kumar 348

Hi Deepthi,

You are not getting any value in System.debug as you are utilising the instance created by you in test class, so it only holds the value you have provided in test class. Whereas Name field is calculated when contact is inserted that is after your DML operation in Test class.

So if you want to use the value of NAME please use a simple SOQL to fetch the value and you will get the value from field. You can refer below example:
 

static testMethod void createContact(){
         Contact con = new Contact();
         con.FirstName = 'testFirstName';
         con.LastName = 'testLastName';
         con.Email= 'santoshlmar29@gmail.com';
         insert con;
         System.debug('con : '+ con.Name +' FirstName: '+ con.FirstName); // Here you will get null in NAME
         Contact conIns = [Select Name, FirstName FROM Contact WHERE id =: con.id];
         System.debug('con : '+ conIns.Name +' FirstName: '+ conIns.FirstName);        //Here you will get value as I have fetched it using SOQL
     }

So from above code snippet you can use conIns.Name to refer the name in you code.
 
Emp Name = conIns.Name;


If you find the above solution helpful. Please mark as Best Answer to help others too.

Thanks and Regards,
Santosh Kumar

All Answers

Santosh Kumar 348Santosh Kumar 348

Hi Deepthi,

You are not getting any value in System.debug as you are utilising the instance created by you in test class, so it only holds the value you have provided in test class. Whereas Name field is calculated when contact is inserted that is after your DML operation in Test class.

So if you want to use the value of NAME please use a simple SOQL to fetch the value and you will get the value from field. You can refer below example:
 

static testMethod void createContact(){
         Contact con = new Contact();
         con.FirstName = 'testFirstName';
         con.LastName = 'testLastName';
         con.Email= 'santoshlmar29@gmail.com';
         insert con;
         System.debug('con : '+ con.Name +' FirstName: '+ con.FirstName); // Here you will get null in NAME
         Contact conIns = [Select Name, FirstName FROM Contact WHERE id =: con.id];
         System.debug('con : '+ conIns.Name +' FirstName: '+ conIns.FirstName);        //Here you will get value as I have fetched it using SOQL
     }

So from above code snippet you can use conIns.Name to refer the name in you code.
 
Emp Name = conIns.Name;


If you find the above solution helpful. Please mark as Best Answer to help others too.

Thanks and Regards,
Santosh Kumar
This was selected as the best answer
DipthiDipthi

HI Santosh,

Thank you very much for your response. My first issue is working fine with your solution of creating an Soql query. 

For my second issue  : How can I pull Name value from contact to LeaveEntry?  is showing the below error

"02:14:03:821 FATAL_ERROR System.StringException: Invalid id: Jon Snow"

Here is the code : 
@isTest
private class OnLeaveApprovalTest {
 
@isTest
static void CreateContactAndLeave(){
// Create a new contact
test.startTest();
    
Contact emp = new Contact ();
emp.FirstName = 'Jon';
emp.LastName = 'Snow';
emp.Emp_ID__c = 'Sp-343245';
insert emp;
system.debug(emp);

Contact EmpInfo = [Select Name from Contact Where id =: Emp.Id]; 
system.debug('Full Name of emp is : ' + EmpInfo.Name);

    
//Enter record details on Leaves Object
Leaves__c leaveReq = new Leaves__c();
leaveReq.Employee_Name__c = EmpInfo.Name;
LeaveReq.Request_Type__c = 'Other';
leaveReq.Request_Start_Date__c = date.newInstance(2020,06,10);
leaveReq.Request_End_Date__c = date.newInstance(2020,06,15);
Insert leaveReq;

system.debug(leaveReq);
test.stopTest();    
}
}

Santosh Kumar 348Santosh Kumar 348
After going through the error it seems like 'Employee_Name__c' is a reference field of Contact on Leaves.
So you need to pass the id ion this field.
leaveReq.Employee_Name__c = EmpInfo.Id;

If you find the above solution helpful. Please mark as Best Answer to help others too.

Thanks and Regards,
Santosh Kumar
DipthiDipthi
Hi Santosh,
This seems not working. I see this error :
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Contact_ID__c]: [Contact_ID__c]


I changed the Soql query:
Contact EmpInfo = [Select Name,Id from Contact Where id =: Emp.Id];
leaveReq.Employee_Name__c = EmpInfo.Id;
Santosh Kumar 348Santosh Kumar 348
Could you please help me to understand on which object you are having this field "Contact_ID__c"?
 
DipthiDipthi
I appended leaveReq.Contact_ID__c = Emp.Id; 
It worked. I believe it is becoz of the relationship between Contact and LeaveReq Object (Master-Detail).
Thank you very much for your help!!