We have created a trigger that generates some information for a column on the Opportunity object. We now want all 400,000 existing opportunities to have the same information. The logic is encapsulated into a class, and is fairly complicated so ideally I'd like to run the same logic inside salesforce so I can be sure the rules are applied to historical data exactly the same as they would be with the trigger.

I tried using a batch apex class that inherits from Database.Batchable but I found that some of my records weren't being updated and it's been very difficult to figure out why because different records get ignored each time. When I ran the batch process for individual records, they seem to update fine. Running the process again for the whole batch updated some more but not all. I can't run the process more than once in live because it has to finish overnight and I want to be sure everything has been updated.

Is a batch apex class the best way to do this or are there other options?

  • 2
    Given the number of records, your best choices are either batch apex or an empty data load. Is there any criteria on the record that the trigger uses to choose whether or not to process? Like watching a field change, etc? If not, and it will always run, you could do a DataLoader update using just the record Ids for the data. This would cause the trigger to fire even though nothing is technically getting updated (by your data load). – ScottW Sep 19 '14 at 14:30
  • 2
    Is the batch class not finding some of the opportunities that need updating or is the update not updating some of the opportunities it finds? Logging the number of records found might help determine this. Also, are you using querylocator or iterable in your batch class? – Jagular Sep 19 '14 at 14:48
  • @Jagular the batch trigger says that it has 14 rows to update when it is first run but then only runs the batch update 10 times (I was trying a batch size of 1 to try to debug). I'm using queryLocator. – Helephant Sep 19 '14 at 15:05
  • @scottW thanks for the suggestion, an empty data load would definitely work for us. I'm just a bit concerned that we'll have the same problems with not everything being updated. It's worth trying if I can't get batch apex to work. :) – Helephant Sep 19 '14 at 15:08
  • 2
    if using batchable class, are you using database.update() or update ? - In the former case, you have the optAllOrNone flag to use which will permit partial successes within the batch scope of (default=200) records. Records that fail can have their errors collected and sent to you in an email during the finish() method. The reason it might be working for the scope size = 1 is that your trigger might be failing due to governor limits when presented with 200 records – cropredy Sep 19 '14 at 22:15

Your Answer

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

Browse other questions tagged or ask your own question.