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

One time I had to do this. My 7-year old spent about half an hour at my desk clicking the button repeatedly while I went downstairs and made a sandwich.

Still easier than writing and promoting a batch for a one time data cleanup.


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.


We can use Batch apex for these kinda problems. Please refer the below link for details

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

http://salesforcetrekbin.blogspot.in/2010/04/how-to-use-batch-apex-in-salesforce.html

Hope it helps. Thanks

Tags:

Dml

Apex