+ Start a Discussion

Datebase.saveResult returns only the first error

When invoking Datebase.saveResult and trying to collect the errors, it only seems to get the  first error and ignores other errors.

For example suppose i have 2 fields on an object and have validation on each of them. If I try and get the errors via getErrors(), only the error on the first field is retrieved. This seems different from how the errors are show on the UI - both validation errors are show on the UI.


Can you Post Your 2 line Code .where you are saving records.


Below is a code snippet. The error size is always 1 and it seems like it only puts the last error encountered into the list. seems like a bug to me.


List<Temp__c> tempList = new List<Temp__c>();
tempList.add(new Temp__c());

Integer i =0;
Database.SaveResult[] sResults;
sResults = Database.insert(tempList,false);
for(Database.SaveResult sr : sResults){
if(!sr.isSuccess()) {
System.debug(Logginglevel.ERROR,'Error Size' + sr.getErrors().size());
for(Integer j =0 ; j < sr.getErrors().size();j++){
String errMessage = sr.getErrors()[j].getMessage();
System.debug('Error Message' + errMessage);
System.debug(Logginglevel.ERROR,'Field Names' + sr.getErrors()[j].getFields());


In the trigger I have validation like the following


Temp__c temp = (Temp__c)newLine;
temp.Field1__c.addError('Field1  error');
temp.Field2__c.addError('Field2 Error');
temp.addError('Error while inserting'); 


Always only the last addError message seems to show up.