What is database insert (list, false) in apex?

2.8K    Asked by AbigailAbraham in Salesforce , Asked on Sep 22, 2022

 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.

Answered by Adam Lipscomb

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());

        }
    }
}

Your Answer

Answer (1)

In Apex, the Database.insert method allows you to insert records into the database, with the option to specify whether to perform a partial or full insert operation.

Database.insert(recordsToInsert, allOrNone);

Parameters:

  1. recordsToInsert: The list of records you want to insert.
  2. allOrNone: A boolean flag that determines the transaction behavior.

Behavior of allOrNone:

True: If set to true, the entire operation is rolled back if any record fails to insert. This is similar to using the insert DML statement.

False: If set to false, the method attempts to insert all records. If some records fail, the others will still be inserted. You can then handle the failed records separately.

Example:

  // Create a list of accountsList accountsToInsert = new List();accountsToInsert.add(new Account(Name='Account 1'));accountsToInsert.add(new Account(Name='Account 2'));// Attempt to insert the accounts with partial success allowedDatabase.SaveResult[] results = Database.insert(accountsToInsert, false);// Process the resultsfor (Database.SaveResult sr : results) {    if (sr.isSuccess()) {        System.debug('Successfully inserted account with ID: ' + sr.getId());    } else {        for (Database.Error err : sr.getErrors()) {            System.debug('Failed to insert account: ' + err.getMessage());        }    }}

In this example:

  • We create a list of Account records to insert.
  • We call Database.insert with the list and false for the allOrNone parameter, meaning partial inserts are allowed.

  • We then process the SaveResult array to handle successes and errors individually.

This method is useful when you want to ensure that valid records are inserted even if some records in the batch fail due to validation rules, required fields, or other issues.

6 Months

Interviews

Parent Categories