[SalesForce] Database.DeleteResult.getErrors() question

I am trying to connect specific records that fail to delete in my batch job to their error messages. I thought my solution was working, but I checked the size of .getErrors() and it's size 1. My test class is set up so 50 records fail, so I would think the size of .getErrors() would be 50. How is the .getErrors() array populated?

Best Answer

When you are calling Database.delete it is returning a List of Database.DeleteResult records if you are doing it to a list.

There are several things that could cause only 1 record to be returned here.

1) If you are calling Database.delete inside a query on each individual record, you will only get 1 error returned.

for (Account acc: scope)
{
  Database.DeleteResult result = Database.delete(acc);
  if (!result.success)
  {
    //This is only asking for all of the errors on a single record here
    List<Database.Error> errors = result.getErrors();
  }
}

2) You are looking at a single result instead of all of the results.

List<Database.DeleteResult> results = Database.delete(scope);
for (Database.DeleteResult result: results)
{
  if (!result.isSuccess())
  {
    //Again, this is only for a single record
    Database.Error errors = result.getErrors();
  }
}

It's harder to tell for sure what is going on though unless you show us what you have done.