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
Yogesh BiyaniYogesh Biyani 

Why is the following code giving Too many DML rows:10001 error message

I am trying to update some accounts in the anonymous window and get the Too many DML rows error message. What am I doing wrong? 
 
​List<Account> acc = new List<Account>();

for(Account a:[SELECT Id, Industry from Account WHERE Industry='Architect']){
    
    a.Industry='Architecture';
    acc.add(a);
}

update acc;

Yogesh
Best Answer chosen by Yogesh Biyani
Salesforce DeveloperSalesforce Developer
You are getting this error because there is limit of max records one DML Statement can update, and the limit is of 10000. So, your query is getting more than 10000 and you are seeing this error. If I am not wrong then you are trying to update the Industry picklist value "Architect" to "Architecture", you can directly edit the picklist value and all the records which have this value will automatically be updated.

No need to something like above.

All Answers

Shawn Reichner 29Shawn Reichner 29
Instead of doing it through the Anonymous window, please export all of your accounts using Dataloader or Workbench and make the change on the csv file and do an mass update.  It will run in Asynchronus which batches your records and you should be fine. 

Hope this helps,

Shawn 

 
Srinath R 9Srinath R 9
Put a limit clause there in the SOQL
SELECT Id, Industry from Account WHERE Industry='Architect' limit 10000.

In case you do not want to update the same rows over and over again , add another filter using lastmodifieddate to ignore the ones which are updated today. This filter you can add after the first run of anonymous block.
Salesforce DeveloperSalesforce Developer
You are getting this error because there is limit of max records one DML Statement can update, and the limit is of 10000. So, your query is getting more than 10000 and you are seeing this error. If I am not wrong then you are trying to update the Industry picklist value "Architect" to "Architecture", you can directly edit the picklist value and all the records which have this value will automatically be updated.

No need to something like above.
This was selected as the best answer