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
Ashu sharma 38Ashu sharma 38 

How to resolve the limit in batch apex

Hello,

As I am trying to insert more than 10000 records by using batch apex,getting error too many DML.
As 
 there is goverener limit that we can not perform dml more than 10000 records but I am using Batch apex,in that it overecome the governer limit by separting into batches(batches having their own limits).but not able to perform how to achiev this,

Please help me in this:
Thank you.
here is  my code: 

global class insertRecordsUsingBatchApex implements database.Batchable<sObject>{
    
    global database.QueryLocator start(database.BatchableContext bc){
        return database.getQueryLocator('select id,name,myEmail__c from Account limit 2000 ');
    }
    
    global void execute(database.BatchableContext bc,list<Account> scope){
        for(Account cc:scope){
            for(integer i=0;i<=10001;i++){
                //10001 records insert
                Account a=new Account(name='testingforInsertRecords' +i,myEmail__C='bsdh@gmail.com' );
                scope.add(a);
            }
            insert scope;
            
        }
        
    }
    global void finish(database.BatchableContext bc){       
        
    }
}

database.execute(**,100);
Shubham NandwanaShubham Nandwana
Hi Nitish,
I can't figure out exactly what you are trying to do from your code. You are getting 2000 account records and then looping them all to insert more than 10000 records for each iteration.
If you are trying to insert 10,001 account record using batch apex then please refer to below code. It will insert account records in batches.
global class insertRecordsUsingBatchApex implements database.Batchable<sObject>{
    
    global List<sObject> start(database.BatchableContext bc){
  		List<Account> scope=new List<Account>();
        for(integer i=0;i<=10001;i++){
                //10001 records insert
                Account a=new Account(name='testingforInsertRecords' +i,myEmail__C='bsdh@gmail.com' );
                scope.add(a);
            }
		return scope;
    }
    
    global void execute(database.BatchableContext bc,list<Account> scope){
        insert scope;
        
    }
    global void finish(database.BatchableContext bc){       
        
    }
}

 If you find it useful then select it as the best answer.
Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts