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
PAWAN THOSARPAWAN THOSAR 

create new 20 contact record in salesforce with atleast five fields

SwethaSwetha (Salesforce Developers) 
Try below
List<Contact> contactList = new List<Contact>();

// Create 20 Contact records
for (Integer i = 0; i < 20; i++) {
    Contact newContact = new Contact();
    newContact.FirstName = 'First Name ' + String.valueOf(i);
    newContact.LastName = 'Last Name ' + String.valueOf(i);
    newContact.Email = 'email' + String.valueOf(i) + '@example.com';
    newContact.Phone = '123-456-7890';
    newContact.MailingCity = 'City ' + String.valueOf(i);
    
    contactList.add(newContact);
}

// Insert the Contact records
insert contactList;

Here, we are creating a list of 20 Contact records (contactList). Within the loop, we initialize a new Contact object, set the required fields (FirstName, LastName, Email, Phone, and MailingCity) with dummy values, and add it to the contactList. Finally, we use the insert DML statement to insert the list of Contact records into Salesforce.

You can execute this Apex code using the Developer Console, anonymous Apex, or within a custom Apex class/method, depending on your specific requirements.

If this information helps, please mark the answer as best. Thank you
PAWAN THOSARPAWAN THOSAR
anonymous code
Arun Kumar 1141Arun Kumar 1141

Hi Pawan,
 
public class CreateContacts {
    public static void createContacts() {
        try {
        List<Contact> contactList = new List<Contact>();
        
        for (Integer i = 1; i <= 20; i++) {
            Contact con = new Contact();
            con.FirstName = 'John';
            con.LastName = 'Doe ' + i;
            con.Email = 'john.doe' + i + '@example.com';
            con.Phone = '(123) 456-7890';
            con.Title = 'Manager';
            
            contactList.add(con);
        }
        
            insert contactList;
        } catch (DmlException e) {
            System.debug('An error occurred while creating contacts: ' + e.getMessage());
        }
    }
}

Hope this will help.
Thanks!​​​​​​​