Database.Stateful and Database.SaveResult in a Batchable class giving internal errors

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.

Answer

You need to contact Salesforce and tell them you got an error at this time.., or cause the error to happen again so you can get an exact time.. They will be able to see the error that is actually happening in the DB and will be able to point you to the issue..

I’ve had this happen to me and it was actually a workflow rule that was cause it to blow up, may not be your case tho

Attribution
Source : Link , Question Author : Jesse Milburn , Answer Author : EricSSH

Leave a Comment