18

I am using Database.SaveResult and Database.update(..., false) for the very first time and ask myself how to get the Id of the record where a dml has failed.

I expected Database.SaveResult.getId()` to give me that answer but NO...it is empty when the dml failed.

Help ;-)!

35

We can use List index Property to get the Id of the failed record .The Index of the List which we are updating and also the result set retrieved has the same index .Using the above property for our advantage we can draft a pseduo code as below

List<Database.SaveResult> updateResults = Database.upsert(arudate, false);
for(Integer i=0;i<updateResults.size();i++){
    if (updateResults.get(i).isSuccess()){
        updateResults.get(i).getId();

    }else if (!updateResults.get(i).isSuccess()){
        // DML operation failed
        Database.Error error = updateResults.get(i).getErrors().get(0);
        String failedDML = error.getMessage();
        arudate.get(i);//failed record from the list
        system.debug('Failed ID'+arudate.get(i).Id);
     }

}
  • 2
    does anyone understand why the failed ones don't include the ID? – Phil B Oct 5 '15 at 21:09
  • 3
    because salesforce failed to properly implement the function – NSjonas Apr 5 '17 at 3:54
  • thanks it works, and if you have Map can use same principle, but you just need convert map into list "accMap.values().get(i).Id;" – Radamanf Jun 20 '18 at 9:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.