Wednesday, July 29, 2015

Resolution for too many DML rows in Apex when inserting/updating Error


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 
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;

Create a class and have a future method. Please find the code below
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

1 comment :

Labels

visualforce page ( 13 ) apex integration ( 5 ) apex trigger ( 4 ) csv file from vf page ( 4 ) javascript ( 4 ) csv visualforce page ( 3 ) Too many ( 2 ) call out ( 2 ) integration ( 2 ) rest api ( 2 ) salesforce rest api ( 2 ) salesforce to salesforce integration ( 2 ) sfdc rest api ( 2 ) trigger ( 2 ) 15 digit to 18 digit ( 1 ) DML rows in Apex ( 1 ) Date Conversion ( 1 ) Date/Time conversion ( 1 ) Deploy ( 1 ) Objects to Future Annotated Methods ( 1 ) SFDC limits ( 1 ) Sobject to Future Annotated Methods ( 1 ) Test Class ( 1 ) TimeZone Conversion ( 1 ) Too many dml rows ( 1 ) Too many future calls ( 1 ) annotations ( 1 ) apex code ( 1 ) closed opportunities ( 1 ) commit ( 1 ) convert ( 1 ) create records ( 1 ) csv create records ( 1 ) custom setting ( 1 ) deployment ( 1 ) deployment changeset ( 1 ) disable apex class ( 1 ) disable apex trigger ( 1 ) disable in production ( 1 ) document ( 1 ) download ( 1 ) field name ( 1 ) formula fields ( 1 ) iframe ( 1 ) inactive ( 1 ) intellisense ( 1 ) jsforce ( 1 ) limits ( 1 ) matrix report in vf page ( 1 ) multi select ( 1 ) multi select salesforce ( 1 ) multiselect ( 1 ) paypal ( 1 ) picklist ( 1 ) record type ( 1 ) rollback ( 1 ) salesforce limits ( 1 ) salesforce list ( 1 ) salesforce map ( 1 ) salesforce rest ( 1 ) salesforce set ( 1 ) salesforce1 ( 1 ) sandbox deployment ( 1 ) sfdc collection ( 1 ) sfdc list ( 1 ) sfdc map ( 1 ) sfdc rest ( 1 ) sfdc set ( 1 ) uncommitted ( 1 ) updated field ( 1 ) user ( 1 ) validation rule opportunity ( 1 ) validation rules opportunities ( 1 ) vf page ( 1 )

Ad