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
Vignesh RamshettyVignesh Ramshetty 

Below is the code for one record if i want to insert 5 records a time then what would be the code

Contact con  = new  Contact();

con.Firstname = 'Vignesh';
con.Lastname = 'Ramshetty';
con.Phone = '9666266129';

insert con;
Best Answer chosen by Vignesh Ramshetty
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

In that scenerio you can insert each contact but the best way is to add to the list and inset the list at once so only one DML wil count as below.
 
List<Contact> conlist= new List<Contact>();

Contact con = new Contact();
con.Firstname = 'Vignesh' ; 
con.Lastname = 'Ramshetty';
con.Phone = '9666266129';
conlist.add(con);
Contact con1 = new Contact();
con1.Firstname = 'Tony' ; 
con1.Lastname = 'mogli';
con1.Phone = '9666786129';
conlist.add(con1);
Contact con2 = new Contact();
con2.Firstname = 'Tillu' ;
con2.Lastname = 'mogli';
con2.Phone = '9665288129';
conlist.add(con2);
Contact con3 = new Contact();
con3.Firstname = 'Karthik';
con3.Lastname = 'sai';
con3.Phone = '9665277129';
conlist.add(con3);
Contact con4 = new Contact();
con4.Firstname = 'Harry' ;
con4.Lastname = 'jerry';
con4.Phone = '9662688129';
conlist.add(con4);

insert conlist;

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

The code can be as below.
List<Contact> conlist= new List<Contact>();
for(Integer i=0; i<5;i++){

Contact con  = new  Contact();

con.Firstname = 'Vignesh' +i;
con.Lastname = 'Ramshetty';
con.Phone = '9666266129';
conlist.add(con);
    

}
insert conlist;

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Vignesh RamshettyVignesh Ramshetty
so i have five  different data like

con.Firstname = 'Vignesh' 
; con.Lastname = 'Ramshetty';
con.Phone = '9666266129';


con.Firstname = 'Tony' 
; con.Lastname = 'mogli';
con.Phone = '9666786129';

con.Firstname = 'Tillu' 
; con.Lastname = 'mogli';
con.Phone = '9665288129';

con.Firstname = 'Karthik' +i
; con.Lastname = 'sai';
​​​​​​​ con.Phone = '9665277129';

con.Firstname = 'Harry' 
; con.Lastname = 'jerry';
​​​​​​​ con.Phone = '9662688129';
Vignesh RamshettyVignesh Ramshetty
so i have five different data like con.Firstname = 'Vignesh' ; con.Lastname = 'Ramshetty'; con.Phone = '9666266129'; con.Firstname = 'Tony' ; con.Lastname = 'mogli'; con.Phone = '9666786129'; con.Firstname = 'Tillu' ; con.Lastname = 'mogli'; con.Phone = '9665288129'; con.Firstname = 'Karthik' +i ; con.Lastname = 'sai'; con.Phone = '9665277129'; con.Firstname = 'Harry' ; con.Lastname = 'jerry'; con.Phone = '9662688129';
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

In that scenerio you can insert each contact but the best way is to add to the list and inset the list at once so only one DML wil count as below.
 
List<Contact> conlist= new List<Contact>();

Contact con = new Contact();
con.Firstname = 'Vignesh' ; 
con.Lastname = 'Ramshetty';
con.Phone = '9666266129';
conlist.add(con);
Contact con1 = new Contact();
con1.Firstname = 'Tony' ; 
con1.Lastname = 'mogli';
con1.Phone = '9666786129';
conlist.add(con1);
Contact con2 = new Contact();
con2.Firstname = 'Tillu' ;
con2.Lastname = 'mogli';
con2.Phone = '9665288129';
conlist.add(con2);
Contact con3 = new Contact();
con3.Firstname = 'Karthik';
con3.Lastname = 'sai';
con3.Phone = '9665277129';
conlist.add(con3);
Contact con4 = new Contact();
con4.Firstname = 'Harry' ;
con4.Lastname = 'jerry';
con4.Phone = '9662688129';
conlist.add(con4);

insert conlist;

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer