I have tried to optimize the code, but still it throws the error:

Too many DML rows: 10001

Below is my code:

if(setAccID != null && !setAccID.isEmpty())
        list<Custom__c> lst = [select id, MyAcc_Id__c from Custom__c where MyAcc_Id__c in:setAccountID];
         if(lst != null && !lst.isEmpty())
           for(Custom__c objCustom : lst)
                objCustom.Date_Time__c = system.now();
                objCustom.Code__c = 'C';              
           update lst;


Sometimes, I get the error and not always. I have removed the update lst; out of the for loop.

I read in developer forums and some posts that we need to pass data in batch or write a batch job but I am not sure how to achieve this.

Please suggest!


2 Answers 2


When you get such errors you need to read them carefully; this one is talking about the number of rows not the number of DML statements. While nominally the problem could be addressed by starting a batchable from the trigger, be aware of Is using Database.executeBatch from a trigger an anti-pattern?.

Best you take a step back and re-examine the requirement: what is the code aiming to do? Starting from that you may be able to construct a solution that doesn't suffer from this problem.


System.LimitException: Too many DML rows: 10001 error occurs when we try to do DML operations to more than 10000 records at a time.

If we want to do DML operations to more than 10000 records at a time, we have to call a batch class from the current class to handle it separately.

You can learn the basics of batch class here:-

  1. Use Batch Apex
  2. Using Batch Apex
  3. How to execute Batch Apex Using Apex Trigger?

If you want to avoid, You can optimize based on do you really need to update all the list rows without checking any condition.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .