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
Ramana123Ramana123 

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

I have given all required field values but still throwing that error


@isTest
private class UpdatingAccountRelatedContactsCount_Test {    
 static Testmethod void accountRelatedContacts()
    {
    List<Contact> conList =  new List<Contact>();
    Account Acc = New Account();
    Acc.Name = 'Sai';
    insert Acc;    
           
    Contact con = new Contact();
    con.LastName = 'Test1';
    con.AccountId = Acc.Id;
    con.Sum__c = 100;
    conList.add(con);       
        
    Contact con1 = new Contact();
    con.LastName = 'Test2';
    con1.AccountId = Acc.Id;
    con1.Sum__c = 200;
    conList.add(con1);            
    insert conList;
        
    System.debug('aaaaaaaaaaaaaaa'+conList);
   // delete con1;    
    Test.StartTest();           
    TriggerHelperClass obj = new TriggerHelperClass();
    update conList;
    Test.StopTest();
    }
}
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Srikanth,

Can you check if you are getting this error even when you are trying to insert from the anonymous window and also as you have used system.debug are you able to see the LastName in the list of contact in debug logs?

Looking forward to your response.

Thanks.
Ramana123Ramana123
In debug records are inserting
ravi soniravi soni
hi srikanth,
you made some mistake replace your  second contact object with following code.
Contact con1 = new Contact();
    con1.LastName = 'Test2';
    con1.AccountId = Acc.Id;
    con1.Sum__c = 200;
    conList.add(con1);            
    insert conList;
you forget add con1 in lastName.
let me know if it helps you and marking it as best.
Thank you