6

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?

10

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.

  • 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 Aug 10 '13 at 16:18
  • 1
    @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 Apr 25 '18 at 4:55
13

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
    awesome xD, I have to get a child xD – Pablo Martinez Aug 7 '15 at 7:19
  • 1
    Classic! what a great solution! – Caspar Harmer Nov 4 '15 at 17:00
  • 1
    Best instance of delegating work ever. – Richard N Dec 27 '15 at 23:23
3

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

http://www.salesforce.com/us/developer/docs/apexcode/Content/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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.