8

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?

4 Answers 4

14

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.

4
  • Thanks Doug. Given my time constraints, and my not really wanting to write a batchable class for this one-off data cleanup task, I ended up doing exactly what you suggested. Thanks!
    – BBP27
    Commented Aug 10, 2013 at 16:18
  • 2
    @BBP27 You can also open multiple dev console/workbench instances and run jobs in parallel - I often do that if I need to generate 100k or so records in my scratch orgs.
    – dzh
    Commented Apr 25, 2018 at 4:55
  • How would you identify which 10k records were returned in the query? And how would you ensure that the remaining 10k records would be returned the next time you run the script? Commented Jan 13, 2020 at 19:15
  • 1
    @AyushGoyal each time you run the query you pick up some 10k records that match your criteria, and you update them. They no longer match the criteria, so they won't be selected when you run the query again.
    – Doug B
    Commented Jan 17, 2020 at 11:22
20

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.

3
  • 4
    awesome xD, I have to get a child xD Commented Aug 7, 2015 at 7:19
  • 1
    Classic! what a great solution! Commented Nov 4, 2015 at 17:00
  • 1
    Best instance of delegating work ever.
    – Richard N
    Commented Dec 27, 2015 at 23:23
3

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

0

I wrote a Go library called batchforce to make scripting these types of updates easy without having to create a batch apex class.

It takes SOQL query and a function that maps an input record to update records. It uses the Bulk API to perform updates so it's pretty quick in most cases.

Here's the example from the README that updates the Title on all Contacts based on their Birthdate:

package main

import (
    batch "github.com/octoberswimmer/batchforce"
    force "github.com/ForceCLI/force/lib"
    "time"
)

var (
    fiftyYearsAgo  = time.Now().AddDate(-50, 0, 0)
    thirtyYearsAgo = time.Now().AddDate(-30, 0, 0)
)

func main() {
    query := `
        SELECT
            Id,
            Birthdate
        FROM
            Contact
        WHERE
            Birthdate != null
    `

    batch.Run("Contact", query, setTitle)
}

func setTitle(record force.ForceRecord) (updates []force.ForceRecord) {
    birthdate, err := time.Parse("2006-01-02", record["Birthdate"].(string))
    if err != nil {
        return
    }
    update := force.ForceRecord{}
    update["Id"] = record["Id"].(string)
    switch {
    case birthdate.Before(fiftyYearsAgo):
        update["Title"] = "Geezer"
        updates = append(updates, update)
    case birthdate.After(thirtyYearsAgo):
        update["Title"] = "Whippersnapper"
        updates = append(updates, update)
    }
    return
}

You must log in to answer this question.

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