What is database insert (list, false) in apex?
I am creating a List of Accounts, using Database.Insert (list,false) if I did give a name for a record in the list, it would skip that particular and insert the remaining records, is there any chance to get the details of the skipped record.
Salesforce SPONSORED BY Sponsored logo Database.insert (list,false) in apex
Asked 5 years, 7 months ago Modified 3 months ago Viewed 32k times 2 1
I am creating a List of Accounts, using database.insert (list,false) . If I did give a name of the record in the list, it would skip that particular and insert the remaining records. Is there any chance to get the details of the skipped record? apex
database.insert Share
Improve this question
Follow edited Jan 5, 2017 at 7:57 user avatar
RCS 2,07033 gold badges1111 silver badges2323 bronze badges
asked Jan 5, 2017 at 7:31
user avatar Satya 70633 gold badges1919 silver badges4040 bronze badges
You can get that using Database.saveresult which will have the failed records as well –
RCS
Jan 5, 2017 at 7:35
Add a comment
3 Answers
Sorted by:
Highest score (default)
-2
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
Share
Improve this answer
Follow
answered Jan 5, 2017 at 7:36
user avatar
Shikha Jain
11222 bronze badges
2
Posting links as answers doesn't help. Links fail or become dead all the time. –
Drew Kennedy
Jun 28, 2018 at 15:03
Add a comment 3 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 improvedInsert(List sObjects, Schema.sObjectField field) {
Map saveResultByField = new Map();
List results = Database.insert(sObjects, false);
for (Integer i = 0;i
Methods give you a map where the key is specified as a parameter field value and the value is referencing the Save/Upset Result object.
Share Improve this answer Follow edited Apr 23, 2019 at 17:39
user avatar apz
13011 silver badge1111 bronze badges
answered Jan 31, 2017 at 12:32
user avatar pantry
60444 silver badges1919 bronze badges
Post relevant content to your answer or delete it. Links are not valuable here especially when they go stale –
Eric Jan 31, 2017 at 12:33
Add a comment 2
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.Save Result 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());
}
}
}