+ Start a Discussion
ch ranjeethch ranjeeth 

how to insert 1 millions records using batch apex?

sfdc Beginnersfdc Beginner
Use Database.QueryLocator to insert 1 Million Records through Batch Apex.

If this Solces your Problem, Then select my Answer as Best Answer. So that it may helpful for Others!!
ch ranjeethch ranjeeth
Please give somw sample code
Anilkumar KotaAnilkumar Kota
Here is the sample code for inserting records.
 
global class InsertAccountContactimplements Database.Batchable<sObject>{
 
     global ExampleBatchClass(){
                // Batch Constructor
     }
     
     // Start Method
     global Database.QueryLocator start(Database.BatchableContext BC){
     String Query = ''; // Generate your string query on any object here with limit 10000
      return Database.getQueryLocator(Query); //Query is Required on object which you want to run Batch
     }
    
   // Execute Logic
    global void execute(Database.BatchableContext BC, List<sObject>objlist){
    List<Account> acclist = new List<Account>();
list<Contact> conlist = new list<contact>()
          for(Sobject obj: objlist){
                Account acc = new account();
                acc.name = 'testname';
                acclist.add(acc);
          }
       insert acclist;
    for(Account acc : Acclist){
          Contact con = new Contact();
           con.lastname = 'testname';
           con.accountid = acc.id;
           conlist.add(con);
   }  
   Insert conlist;
    global void finish(Database.BatchableContext BC){
         // Logic to be Executed at finish
    }
 }

You can insert 100000 contact and account record in same batch class, But it also depends on your requirment. 
You can use data loader or import wizard for the same. 
Data Loader autometically inserts records in batches.