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);
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);
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:
Hope this helps you :)
All Answers
Refer this :
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_insert.htm
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:
Hope this helps you :)
Hi Vishal,
I agree your statement, that only when dml operation is sucessfull we get the Id.
Thanks Vishal.It works perfectly.