Find your content:

Search form

You are here

Unable to iterate Database.SaveResult list

 
Share

I am doing an insert using Database.Insert in a batch class as follows:

Database.SaveResult[] srList = Database.insert(clonedList, false);

for (integer i=0; i<srList.size(); i++) {

    system.debug('srList : '+i+' : '+srList[i].isSuccess());

    if(!srList[i].isSuccess()){

        errorMap.put(clonedList[i].ParentId__c,srList[i].getErrors().get(0).getStatusCode()+':'+srList[i].getErrors().get(0).getMessage());

    }

}

When I check the debug logs, I see the code has not entered the srList loop.

I know there are some errors and records not inserted, but I am not getting the errorMap populated.


Attribution to: NHK

Possible Suggestion/Solution #1

You should only find this scenario (not entering the loop) when clonedList is empty.

Here's my own snippet of code I tested against and had no issues.

Update Case

source code:

List<Account> accounts = new List<Account>{new Account(), new Account(), new Account()};
Database.SaveResult[] saveResult = Database.update(accounts, false);

for(Database.SaveResult saveRes : saveResult) {
    if(!saveRes.isSuccess()) {
        for(Database.Error err : saveRes.getErrors()) {
            system.debug(err.getMessage());
        }
    }
}

And before anyone says anything with my update, I also did the same test with insert:

Insert Case


Attribution to: Double A
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34963

My Block Status

My Block Content