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
JavagalJavagal 

run time exception DML rows error

Hi I have a Web service class which is having an issue row lock contention . could you please suggest how to reduce  Concurrency of these requests.  Many thanks ..

 error Message : Too many DML rows: 10001

Global class BulkDelayProcessUtility {
 
  webservice static boolean BulkDelayExistingUtility(){
    boolean processCompleted=true;
    datetime dateNow=datetime.now();
    datetime cutoff=dateNow.addMinutes(-15);
    List<Id> delayReasonIds=new List<Id>();
    for(Delay_Reason__c dr: [select Id from Delay_Reason__c where CreatedDate < :cutoff]){
      delayReasonIds.add(dr.Id);
    }
    if(!delayReasonIds.isEmpty()){
      try{
        database.delete(delayReasonIds);
      }
      catch(Exception ex){
        System.debug('Failed to delete existing Delay Reasons! ' +ex);
        processCompleted=false;
      }
    }
    return processCompleted;
  }

}

 
Best Answer chosen by Javagal
Amit Chaudhary 8Amit Chaudhary 8
To resolve row lock contention issue you can use order by and You can use apex batch to perform DML operation involving more than 10,000 records.

Please check below link:-
https://developer.salesforce.com/blogs/engineering/2014/07/record-locking-cheat-sheet.html
https://developer.salesforce.com/blogs/engineering/2013/01/reducing-lock-contention-by-avoiding-account-data-skews.html

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8

Hi Javagal,

You are getting  "Too many DML rows: 10001" issue because your query is returining more then 10000 record. Please add Limit 10000 and try below code.

Global class BulkDelayProcessUtility 
{
 
  webservice static boolean BulkDelayExistingUtility()
  {
    boolean processCompleted=true;
    datetime dateNow=datetime.now();
    datetime cutoff=dateNow.addMinutes(-15);
    List<Id> delayReasonIds=new List<Id>();

    for(Delay_Reason__c dr: [select Id from Delay_Reason__c where CreatedDate < :cutoff limit 10000 ] )
    {
      delayReasonIds.add(dr.Id);
    }
    if(!delayReasonIds.isEmpty())
    {
      try
      {
        database.delete(delayReasonIds);
      }
      catch(Exception ex)
      {
        System.debug('Failed to delete existing Delay Reasons! ' +ex);
        processCompleted=false;
      }
    }
    return processCompleted;
  }

}

Thanks,
Amit Chaudhary
JavagalJavagal
Amit,

Thank you i already done that ... but when i do that i am getting row lock contention issue.  SF is suggesting us reducing the concurrency of these requests... my Question is What is the other way to optimize.
Amit Chaudhary 8Amit Chaudhary 8
To resolve row lock contention issue you can use order by and You can use apex batch to perform DML operation involving more than 10,000 records.

Please check below link:-
https://developer.salesforce.com/blogs/engineering/2014/07/record-locking-cheat-sheet.html
https://developer.salesforce.com/blogs/engineering/2013/01/reducing-lock-contention-by-avoiding-account-data-skews.html

Thanks
Amit Chaudhary
This was selected as the best answer
JavagalJavagal
Thanks amith