[SalesForce] How to avoid DML Insert/Update Limit of 10,000 rows when running anonymous apex

Is there any way to bulkify an insert/update operation performed from anonymous apex?

For instance, if I have a script that builds a list of 15,000 records that need to be updated, is there any way to avoid hitting the "Too many DML rows: 10001" error when trying to insert/update?

Best Answer

For a quick and dirty approach if I have more than 10,000 rows to update through anonymous apex I will sometimes do something like this

List<Thing__c> things = [select id from thing__c where status__c = 'Old value' limit 10000];
for (Thing__c thing : things) {
   // make changes
   thing.status__c = 'New Value';
}
update things;

Then run the script twice or whatever. This relies on the change you're making being something that you can filter in the where condition. Also this will get dull if there are many tens of thousands of records to update and you have to kick off the script again and again.

Related Topic