+ Start a Discussion
kiran punurukiran punuru 

How to get the failed to update record ids

Hi ,
I Have list of 10 accounts which i need to update out of that 10 records 5 records get updated and 5 records get failed to update due to validation rules ,Is there a way to get the failed record ids .any ideas ??

Thanks 
Kiran
Nayana KNayana K
// replace lstAccountToUpdate with list of accounts to be updated.
Database.SaveResult[] srList = Database.update(lstAccountToUpdate, false);

// Iterate through each returned result

for(Integer i=0; i<srList.size(); i++) 
{
	sr = srList[i];
    if (sr.isSuccess()) 
	{
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + sr.getId());
    }
    else 
	{
		// Operation was unsuccessful, so get the ID of the record that was processed
        System.debug('Error Account ID: ' + lstAccountToUpdate[i].Id);
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) 
		{
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}

 
Abu HashimAbu Hashim
As Nayana said, u should Database.Update and use the return type Database.SaveResult and iterate through failed records...!!
Tad Aalgaard 3Tad Aalgaard 3

One edit to the code provided by Nayana.

Add Database.SaveResult to beginning of the followng line.

sr = srList[i];
 
Database.SaveResult sr = srList[i];