Usually when I write a class that implements Database.Batchable
I end up saving errors in a variable(s) to sort through in the finish
method and send out something about successes and failures. This time I decided to save all of my Database.SaveResult
's in a collection and iterate through those in my finish method.
However, when I tried doing this I kept getting a hung job and one of my SerialBatchApexRangeChunkHandler
's was failing with the following error:
Internal Salesforce.com Error
Class (errors):
global class AnnualAccountRebase implements Database.Batchable<sObject>, Schedulable, Database.Stateful {
global List<Database.SaveResult> saveResults = new List<Database.SaveResult>();
global void execute(Database.BatchableContext BC, List<Account> scope) {
//do stuff to scope
//inside try catch
saveResults.addAll(Database.update(scope, false));
}
global void finish(Database.BatchableContext BC) {
for (Database.SaveResult result : saveResults) System.debug(result);
}
}
But if I change the variable to a List<String>
type and get the data I want from the SaveResult
's everything is fine.
Class (works):
global class AnnualAccountRebase implements Database.Batchable<sObject>, Schedulable, Database.Stateful {
global List<String> errors = new List<String>();
global void execute(Database.BatchableContext BC, List<Account> scope) {
//do stuff to scope
//inside try catch
List<Database.SaveResult> saveResults = Database.update(scope, false);
for (Database.SaveResult saveResult : saveResults) {
if (!saveResult.isSuccess()) {
for (Database.Error e : saveResult.getErrors()) {
errors.add(e.getMessage());
}
}
}
}
global void finish(Database.BatchableContext BC) {
System.debug(errors);
}
}
I also tried marking the first collection (SaveResult
) as transient
with no success.
Dug around for a while but could not figure out why this is happening. Perhaps my 'Google-Fu' is weak? Any insight would be appreciated.
Database.Error
collection, which isn't serializable, and I got the serializable error.