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

test class error:System.DmlException: Insert failed. First exception on row 0;

Cant able to cover the test class for this code getting error saying that


 System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]

my controller class is


public void AttendeeForm(){
//system.debug('check DynamicAttendeeForm'+DynamicAttendeeForm);
system.debug('check lstResult' + lstResult);
for(AttendeeCustomFieldsClass a : lstResult){
system.debug('check Defaultvalue' + a.DefaultValue);
system.debug('check acc' + acc);
acc =new contact();


public void UpdateAttendee(){

update acc;
system.debug('check check'+ acc);
Map<string,string> NewAnswers=new Map<string,string>();
system.debug('check atte'+lstResult);
for(AttendeeCustomFieldsClass EA : lstResult){
list<Attendee_custom_information__c> NewCustomAnswers=[select Field_name__c,Field_value__c from Attendee_custom_information__c where Event_name__c=:eid and attendee_session_Id__r.ContactId__c=:EditAttendeeId];

for(Attendee_custom_information__c NA : NewCustomAnswers){
update NewCustomAnswers;
displayPopup = false;

string DeleteAttendeeId;
public string getDeleteAttendeeId(){return DeleteAttendeeId;}
public void setDeleteAttendeeId(string DeleteAttendeeId){this.DeleteAttendeeId=DeleteAttendeeId;}

public void DaleteAttendee(){
system.debug('check DeleteAttendeeId'+DeleteAttendeeId);
Event_attendee_session__c DelAtten=[select id from Event_attendee_session__c where id=:DeleteAttendeeId];
delete DelAtten;


My test class is



 EventRegistration eventreg = new EventRegistration();

Contact c = new contact(FirstName='aaa',LastName='bbb',Email='',MailingCity='Hyderabad',MailingState='AP');
insert c;



Hope somebody  would came across this and found a solution.....If yes let me know 


Any further information required I can give 



Jia HuJia Hu
It seems this is only part of your code.
The error message means when you insert a Contact, you miss the value of LastName field.

for example, when you execute,
Contact c = new contact();
insert c;
you got exactly the same error.

But I can't see where you use insert in your code, may be hidden in the CreateContact(acc,'Attendee');

Except the Test part, does you code work in a Sandbox?


Hi Jia 

   Thanks for the reply......U r correct its only a part of the the solution....I need to set the contact acc and should innitiate a value for it


Here is the code for test class


contact con=new contact();
insert con;