You need to sign in to do that
Don't have an account?
mohan 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
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
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
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)