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
MeekoMeeko 

When calling the method "createContact", the system throws an error "LastName is required", what line needs to be updated

ShrutikaShrutika
Last name is mandetory field in salesforce ..so you should take input for last name in visualforce page..like
<apex:inputField value="{!con.lastname}"/>

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.
 
MeekoMeeko
public with sharing class ApexClassHomeWork 
{
    public List<Account> accs {get; set;}
    
    // retrieves the list of accounts backing the page
    public ApexClassHomeWork()
    {
        accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account  WHERE Name LIKE 'Test%' LIMIT 10];
    }
    
    public void createContact(Id acctId)
    {
        for(Integer i=0; i<500; i++)
        {
            Contact c = new Contact(AccountId=acctId, "last name");
            insert c;
        }
    }
}

public class Contact P
    public contact(ID.AccountID) throw LastNameException
        if lastName.isEmpty {
        
            throw LastNameException
            
            
            
            
mukesh guptamukesh gupta
Hi Meeko,

I have tested my code, Please use this code your side:-
 
try {
    Account acct = new Account(Name='New Account');
    insert acct;

   
    ID acctID = acct.ID;

    // Add a contact to this account.
    Contact con = new Contact(
        FirstName='mukesh',
        LastName='gupta',
        Phone='12454654654',
        AccountId=acctID);
    insert con;
} catch(DmlException e) {
    System.debug('An unexpected error has occurred: ' + e.getMessage());
}

Please MARK AS A BEST ANSWER!!!!

REgards
Mukesh
ShrutikaShrutika
You can check this code  in createContact() :

accs=[select id FROM Account where name like 'Test%'];

 for(Account a:accs){
  a.id=accs[0].id;
  
Contact c = new Contact();

      c.AccountID = a.id;
      c.FirstName='UJWALA';
      c.LastName='MANE';

     insert c;