2

I am creating List of Accounts, using database.inser(list,false) if i did give name for record in list,it will skip that particular and insert remaining records, is there any chance to get the details of skipped record.

1
  • You can get that using Database.saveresult which will have the failed records as well
    – RCS
    Commented Jan 5, 2017 at 7:35

3 Answers 3

-4

Please check the following link of how to access the error records: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_saveresult.htm

1
  • 4
    Posting links as answers doesn't help. Links fail or become dead all the time. Commented Jun 28, 2018 at 15:03
6

I had a similar problem. For me, it was important to know exactly which inserted object succeeded or failed. I wrote some methods to handle this:

/**
     * @description Method to insert objects to DataBase with improved audit
     * @param sObjects - sObjects to be insert
     * @param field - field that identifies Saveresult with inserted object
     * @return map where key is a value from field specified as param: field
     * and the value is Save result for sObject which is identified by key
     */
    public static Map<String,Database.SaveResult> improvedInsert(List<SObject> sObjects, Schema.sObjectField field) {
        Map<String,Database.SaveResult> saveResultByField = new Map<String,Database.SaveResult>();
        List<Database.SaveResult> results = Database.insert(sObjects, false);
        for (Integer i = 0;i<sObjects.size();i++) {
            saveResultByField.put((String)sObjects.get(i).get(field),results.get(i));
        }
        return  saveResultByField;
    }

Methods give you a map where key is specified as parameter field value and the value is referencing Save/Upsert Result object.

1
  • Post relevant content to your answer or delete it. Links are not valuable here especially when they go stale
    – Eric
    Commented Jan 31, 2017 at 12:33
3

Find the below code refer link

The Database methods return result objects containing success or failure information for each record. For example, insert and update operations each return an array of Database.SaveResult objects.

Database.SaveResult[] saveResultList = Database.insert(insertList, false);

// Iterate through saveResultList based on isSuccess capture the failed records
for (Database.SaveResult sr : saveResultList) {
    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 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());
        }
    }
}
1
  • "if (!sr.isSuccess()) { " should be without the !
    – Json
    Commented Apr 28, 2022 at 10:55

Not the answer you're looking for? Browse other questions tagged .