1

I could not understand whats the Difference between Database.insert(list) and Database.insert(list,false)

7

Just an example

If you are inserting 5 records.

3 having all the required fields value and remaining 2 records missing required fields value.

if you use

Database.insert(list) will thrown an exception and it will not insert any one of the records.

but

if you use Database.insert(list, false) It will insert 3 records successfully and rest 2 records(required fields missing) will not be inserted.

Database.insert(list, false) In this scenario you will not get any exception


Example with code. We know Account's Name field is required.

List<Account> lstAccount = new List<Account>();

lstAccount.add(new Account(Name = 'Test Account 001'));
lstAccount.add(new Account(Name = 'Test Account 002'));
lstAccount.add(new Account(Name = 'Test Account 003'));
lstAccount.add(new Account());
lstAccount.add(new Account());

Database.insert(lstAccount);

Try this code you will get error.

Line: 9, Column: 1 System.DmlException: Insert failed. First exception on row 3; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]

Now run below code

List<Account> lstAccount = new List<Account>();

lstAccount.add(new Account(Name = 'Test Account 001'));
lstAccount.add(new Account(Name = 'Test Account 002'));
lstAccount.add(new Account(Name = 'Test Account 003'));
lstAccount.add(new Account());
lstAccount.add(new Account());

Database.insert(lstAccount, false);

And check your org you can see 3 records created Test Account 001,Test Account 002 and Test Account 003

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.