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
mohan s 37mohan s 37 

Unable to insert more than 50k records using batch apex

Hi Fiends,

                 I am using batch apex for inserting the 100000 records but, the following exception throws while executing the batch. System.LimitException: "Too many DML rows:10001". I have tried giving batch size  as low and maximum but in both the cases the it throws the same exception.

global class InsertionOfOneLakhRecords implements database.Batchable<Sobject>,database.stateful{
   public integer count = 0;
  global database.QueryLocator start(database.BatchableContext bc){
    return database.getQueryLocator('select id, Name FROM Contact');
}
global void execute(database.BatchableContext bc, List<Contact> sobj){
    List<Contact> cons = new List<Contact>();
   for(integer i=0; i<100000; i++){
    Contact c = new Contact(LastName = 'test'+i, Email='test@salesforce.com', Phone = '123456');
    count++;
    cons.add(c);
}
    try{
    Database.SaveResult[] result =database.insert(cons);
 }
    catch(exception e){
     system.debug('Exception:'+e.getMessage());
    }
}
global void finish(database.BatchableContext bc){
 system.debug('total:'+count);   
  }
}

Thanks & Regards
Best Answer chosen by mohan s 37
Asif Ali MAsif Ali M
You are trying to insert more than the Max limit allowed by Salesforce. The max number of records in a DML statement is 10000. You can change your logic to insert 10000 per call. May be add one outer loop like this
 
for (integer j = 0; j < 10; j++) {
    List<Contact> cons = new List<Contact>();
    for (integer i = 0; i < 10000; i++) {
        Contact c = new Contact(LastName = 'test' + i, Email = 'test@salesforce.com', Phone = '123456');
        count++;
        cons.add(c);
    }
    try {
        Database.SaveResult[] result = database.insert(cons);
    } catch (exception e) {
        system.debug('Exception:' + e.getMessage());
    }
}
Please also note that there is an issue in your code. For every existing 200 records it is going to insert 100000 records. If that is what you want you can change the logic as mentioned above. If the real purpose is just load bulk data then I would create a csv file and load it using Data Loader.
Check this documentation on Governors Limits. (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

 

All Answers

Asif Ali MAsif Ali M
You are trying to insert more than the Max limit allowed by Salesforce. The max number of records in a DML statement is 10000. You can change your logic to insert 10000 per call. May be add one outer loop like this
 
for (integer j = 0; j < 10; j++) {
    List<Contact> cons = new List<Contact>();
    for (integer i = 0; i < 10000; i++) {
        Contact c = new Contact(LastName = 'test' + i, Email = 'test@salesforce.com', Phone = '123456');
        count++;
        cons.add(c);
    }
    try {
        Database.SaveResult[] result = database.insert(cons);
    } catch (exception e) {
        system.debug('Exception:' + e.getMessage());
    }
}
Please also note that there is an issue in your code. For every existing 200 records it is going to insert 100000 records. If that is what you want you can change the logic as mentioned above. If the real purpose is just load bulk data then I would create a csv file and load it using Data Loader.
Check this documentation on Governors Limits. (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

 
This was selected as the best answer
mohan s 37mohan s 37
Thank you Asif Ali for giving your valuable response.