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
veerna551.3942206611176638E12veerna551.3942206611176638E12 

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

to insert multiple contacts for Account Simutaneously.
when i click save button it shows this error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Last Name]: [Last Name]
Error is in expression '{!save}' in component <apex:commandButton> in page baskartask2

Class.AccAndCons1.save: line 41, column 1

<apex:page standardController="Account" extensions="AccAndCons1">
  <apex:form >
   <apex:pageBlock >
    <apex:pageBlockSection title="AccountInfo" columns="1">
     <apex:inputText value="{!Account.name}" label="Name"  />
    </apex:pageBlockSection>
    <apex:pageBlockSection title="conatctInfo" columns="2" >
    <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtTable">
    <apex:column headerValue="S.NO">
    <apex:outputText value="{!wrapper.iden}"/>
    </apex:column>
     <apex:column headerValue="FirstName">
            <apex:inputField value="{!wrapper.con.FirstName}"/>
         </apex:column>
         <apex:column headerValue="LastName">
            <apex:inputField value="{!wrapper.con.FirstName}"/>
         </apex:column>
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    <br></br>
    <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtTable">
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/>
      </apex:commandButton>
      <apex:commandButton value="Save" action="{!save}"/>
  
  </apex:pageBlock>
</apex:form>
</apex:page>





public with sharing class AccAndCons1 {
public list<contactWrapper> Wrappers{get;set;}
private Integer nextIdent=0;
public static Integer addCount {get; set;}
public  Account acc{get;set;}
public string name{get;set;}
public id accId{get;set;}
public list<contact> cons{get;set;}
    public AccAndCons1(ApexPages.StandardController controller) {
    this.accId=Apexpages.currentPage().getParameters().get('id');
    acc=(Account)controller.getRecord();
        wrappers=new List<contactWrapper>();
         cons=new List<contact>();
        for (Integer idx=0; idx<2; idx++)
      {
        wrappers.add(new ContactWrapper(nextIdent++));
       
      }
    }
   
    public void addRows()
   {
     for (Integer idx=0; idx<addCount; idx++)
   {
      wrappers.add(new ContactWrapper(nextIdent++));
   }
}
public PageReference save()
{


for (ContactWrapper wrap : wrappers)
  {
   wrap.AccountID=accId;
   cons.add(wrap.con);
   for(contact c:cons){
    system.debug(c.firstname);
    system.debug(c.lastname);
    }
  }
  insert cons;

  return null;
  }
public class contactWrapper{
public integer iden{get;set;}
public Contact con{get;set;}
public id AccountId{get;set;}
  public contactWrapper(integer idenent){
    iden=idenent;
    con=new contact();
  }
}
}
Ramu_SFDCRamu_SFDC
Last name is mandatory for Contact record hence it is throwing the error. make sure you have some value passed to lastname field before saving/inserting.
Shri RajShri Raj
Before inserting are you getting a value in the Last Name? 
system.debug(c.lastname); ?? 

If its Null do not call the insert functionality. Add a error message on the page. 

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'LAST NAME IS MISSING PLEASE ENTER.');
                    ApexPages.addMessage(myMsg);
Ramu goswami 1Ramu goswami 1
Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Last Name]: [Last Name]
public class AddPrimaryContact implements Queueable {
    
    private Contact con;
    private String state;
    
    public AddPrimaryContact(Contact con, String state) {
        this.con = con;
        this.state = state;
    }
    public void execute(QueueableContext context) {
     List<Account> accounts = [Select Id, Name, (Select FirstName, LastName, Id from Contacts)
                              from Account where BillingState = :state Limit 200];
        List<Contact> primaryContacts = new List<Contact>();
        for(Account acc:accounts){
            Contact c = con.clone();
            c.AccountId = acc.Id;
            primaryContacts.add(c);
        }
        
        if(primaryContacts.size() > 0) {
            insert primaryContacts;
        }
        
    }
    
}







@isTest
public class AddPrimaryContactTest {
    static testmethod void testQueueable(){ 
        List<Account> testaccounts = new List<Account>();
        for(Integer i=0;i<50;i++){
            testAccounts.add(new Account(Name='Account'+i,
                                        BillingState='CA'));
            
        }
     
        for(Integer j=0;j<50;j++){
            testAccounts.add(new Account(Name='Account'+j,
                                        BillingState='NY'));
        }
        insert testAccounts;
         
           Contact testContact = new Contact(FirstName='John', LastName='Doe');
        
        
        insert testAccounts;
    
        AddPrimaryContact addit = new addPrimaryContact(testContact, 'CA');
        // startTest/stopTest block to force async processes to run
        Test.startTest();        
        System.enqueueJob(addit);
        Test.stopTest();        
        // Validate the job ran. Check if record have correct parentId now
        System.assertEquals(50, [select count() from Contact where accountId in(Select Id from Account where BillingState = 'CA')]);
    }
    
}