You need to sign in to do that
Don't have an account?
Dipthi
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;
}
}
( 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;
}
}
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:
So from above code snippet you can use conIns.Name to refer the name in you code.
If you find the above solution helpful. Please mark as Best Answer to help others too.
Thanks and Regards,
Santosh Kumar
All Answers
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:
So from above code snippet you can use conIns.Name to refer the name in you code.
If you find the above solution helpful. Please mark as Best Answer to help others too.
Thanks and Regards,
Santosh Kumar
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();
}
}
So you need to pass the id ion this field.
If you find the above solution helpful. Please mark as Best Answer to help others too.
Thanks and Regards,
Santosh Kumar
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;
It worked. I believe it is becoz of the relationship between Contact and LeaveReq Object (Master-Detail).
Thank you very much for your help!!