+ Start a Discussion

How to send a list of failed record inserts in an email

P.S. No need to send entire record, just a few specific field on each failed records. Code so far is below:
Database.SaveResult[] srList = Database.insert(recsToInsert, false);

Integer successCtr = 0;
Integer failedCtr = 0;

for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        successCtr ++;
    } else {
        // Operation failed, get all errors
        failedCtr ++;
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Fields that affected this error: ' + err.getFields());
System.debug('Total successful record inserts: ' + successCtr);
System.debug('Total failed record inserts: ' + failedCtr);

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Colbridge,

Please find the below link which shows how to send email of the failed record using SingleEmailMessage.


If this solution helps, Please mark it as best solution.