function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sai.sfsai.sf 

Database.saveresult ..failed record ids

HI All,

 

 How to collect the failed record ids from database.saveresult.

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi,

 

I couldn't come across any such standard method that returns Id's in case of failures. Even SaveResult's getId() method works only for successful DML.

 

You can refer this code and modify it as per your need:

 

Set<Id> allAccountIds = new Set<Id>();
Set<Id> successfulAccountIds = new Set<Id>();
Set<Id> failedAccountIds = new Set<Id>();
List<Account> lstAccounts = new List<Account>([Select Id, Name From Account Limit 2]); // any 2 random accounts
for(Account acc : lstAccounts)
{
    acc.Name = '';  // since it's a required field, update will fail
    allAccountIds.add(acc.Id); // store all id's that you're going to process
}
Database.SaveResult[] lsr = Database.update(lstAccounts, false);

// Iterate through the Save Results 
    for(Database.SaveResult sr:lsr)
{
   // fetch all success DML id's in this set
   if(sr.isSuccess())
   {
      successfulAccountIds.add(sr.getId());
   }
}


// one set has all id's, other has id's that were successfully updated. So compare them both to get //the id's that failed to update
for(Id i : allAccountIds)
{
   if(!successfulAccountIds.contains(i))
       failedAccountIds.add(i);
}

system.debug('#### successful ids :: '+successfulAccountIds);
system.debug('#### failed ids :: '+failedAccountIds);

 

Hope this helps you :)

 

All Answers

vishal@forcevishal@force

Hi,

 

I couldn't come across any such standard method that returns Id's in case of failures. Even SaveResult's getId() method works only for successful DML.

 

You can refer this code and modify it as per your need:

 

Set<Id> allAccountIds = new Set<Id>();
Set<Id> successfulAccountIds = new Set<Id>();
Set<Id> failedAccountIds = new Set<Id>();
List<Account> lstAccounts = new List<Account>([Select Id, Name From Account Limit 2]); // any 2 random accounts
for(Account acc : lstAccounts)
{
    acc.Name = '';  // since it's a required field, update will fail
    allAccountIds.add(acc.Id); // store all id's that you're going to process
}
Database.SaveResult[] lsr = Database.update(lstAccounts, false);

// Iterate through the Save Results 
    for(Database.SaveResult sr:lsr)
{
   // fetch all success DML id's in this set
   if(sr.isSuccess())
   {
      successfulAccountIds.add(sr.getId());
   }
}


// one set has all id's, other has id's that were successfully updated. So compare them both to get //the id's that failed to update
for(Id i : allAccountIds)
{
   if(!successfulAccountIds.contains(i))
       failedAccountIds.add(i);
}

system.debug('#### successful ids :: '+successfulAccountIds);
system.debug('#### failed ids :: '+failedAccountIds);

 

Hope this helps you :)

 

This was selected as the best answer
Laxman RaoLaxman Rao

Hi Vishal,

 

I agree your statement, that only when dml operation is sucessfull we get the Id.

sai.sfsai.sf

Thanks Vishal.It works perfectly.