This error is seen when a user performs DML operation on an entity(s) which contains more than 10000 records in a single transaction , i,e if a user executes upsert/update/insert operation on a collection object which contains more than 10000 records a user friendly error message "Error of Too many DML rows when a DML statement has been executed "is displayed. This is governor limit error.
if you ran the Below code in the execute anonymous block or in the Work bench apex Execute
Create a class and have a future method. Please find the code below
if you ran the Below code in the execute anonymous block or in the Work bench apex Execute
List<Account> lstAccount=new List<Account>(); for(Integer i=0;i<10001;i++){ Account acc=new Account(); acc.Name= 'Test Sample'+i; lstAccount.add(acc); } insert lstAccount;
public class LoadBulkData{ @future public static void loadData(List<String> lstAccountInfo){ if(!lstAccountInfo.isEmpty() && lstAccountInfo.size() <= 10000) { List<Account> lstAccount=new List<Account>(); for(String str:lstAccountInfo) { lstAccount.add((Account) JSON.deserialize(str, Account.class)); } if(!lstAccount.isEmpty()){ System.debug('Size In Account'+lstAccount.size()); insert lstAccount; } } } }Execute the Below Code in develper console or Apex Execute on Work bench
List<Account> lstAccount=new List<Account>(); for(Integer i=0;i<10001;i++){ Account acc=new Account(); acc.Name= 'Test Sample'+i; lstAccount.add(acc); } if(lstAccount.size()>10000){ account[] mylist2 = new account[0]; account[] mylist3 = new account[0]; for(Integer j=0;j<lstAccount.size();j++){ if(j== lstAccount.size()-1){ mylist3.add(lstAccount[j]); }else{ mylist2.add(lstAccount[j]); } } System.debug(mylist2.size()); System.debug(mylist3.size()); if(!mylist2.isEmpty()){ insert mylist2; } if(!mylist3.isEmpty()){ List<String> lstStringaccounts=new List<String>(); for(Account ac:mylist3) lstStringaccounts.add(JSON.serialize(ac)); LoadBulkData.loadData(lstStringaccounts); } }In order to resolve this issue, asynchronous method (@future annotation) needs to be used. Asynchronous methods increase the limits to 10,000, but do not scale with batch size (note there is also a limit for the number of asynchronous method invocations per transaction of 10). Please note, @future method runs only there is enough system resource available