0

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.now i want to make a note of skipped record.and save it in csv and insert in static resource.is it possible

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 {
//so failed records comes here,now i want to create a csv and store below store data.

        // 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());
        }
    }
}
2
  • is there any way,to create .csv file in static resource whenever record got skipped
    – Satya
    Commented Jan 5, 2017 at 12:34
  • Hi need help,,can i create csv file in static resource using apex coding.In my project,i have written some batch class to insert data into Account Replica object.And i used database.insert.it will skip the failed records.but now i want to make the customers to see the failed records.how can i achieve this...?!urgent requirement,
    – Satya
    Commented Jan 5, 2017 at 12:44

1 Answer 1

0

Since you are inserting a list, the order is guaranteed. You can get the information about failed records by iterating using for loop.

Database.SaveResult[] saveResultList = Database.insert(insertList, false);
List<String> allErrors = new List<String>();
// Iterate through saveResultList based on isSuccess capture the failed records
for (integer i = 0 ; i< saveResultList.size() ;i++) {
    Database.SaveResult sr = saveResultList[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 {
//so failed records comes here,now i want to create a csv and store below store data.

        // Operation failed, so get all errors   
        String errors = '';             
        for(Database.Error err : sr.getErrors()) {
            errors = err.getStatusCode() + ': ' + err.getMessage()) +'Account fields that affected this error: ' + err.getFields());
            errors=+ insertList[i].SOMEFIELDSVALUE__c;
            allErrors.add(errors);
        }
    }
}

You must log in to answer this question.

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