function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rahul Garg SFDRahul Garg SFD 

What happen when we get error in insert DML , which is used after database.insert()

An Apex transaction insert 100 Account records and 2000 Contact records  before encountering a DML exception when attempting to insert 500 Opportunity records. The Account record are inserted by calling the database.insert() method with the allOrNone argument set to false. The Contact and Opportunity record are inserted using the stand-alone insert statement.
How many total records will be committed to the database in this transaction?
A. 100
B. 2,100
C. 2,000
D. 0

Considering the salesforce documentation of Database.insert() when allOrNone is false, the answer should be A i.e 100. But, when I checked this on Anonymous window in developer console I get answer as D i.e 0.

Please help me with this solution.
Best Answer chosen by Rahul Garg SFD
Rahul Garg SFDRahul Garg SFD
Answer of this Question is D.

All insertions took place in one transaction and the transaction fails, so all records are rolledback even the one inserted form Database.insert().

All Answers

Rahul Garg SFDRahul Garg SFD
Code written in Anonymous Window. This throws DML exception on inserting Contact, but why Accounts are not inserted in database? 

List<Account> accObjLst = new List<Account>();
for(Integer i =0;i<10;i++){
    Account accObj = new Account();
    accObj.name = 'Account12345 '+ i;
    accObjLst.add(accObj);
}
Database.insert(accObjLst, false);
System.debug(accObjLst);

List<Contact> conObjLst = new List<Contact>();
for(Integer i =0;i<9;i++){
    Contact conObj = new Contact();
    conObj.lastName = 'Contact12345 '+ i;        
    conObjLst.add(conObj);
}
Contact conObj = new Contact(Firstname = 'Rahul');
conObjLst.add(conObj);
insert ConObjLst;
Rahul Garg SFDRahul Garg SFD
Answer of this Question is D.

All insertions took place in one transaction and the transaction fails, so all records are rolledback even the one inserted form Database.insert().
This was selected as the best answer
viktor iliukhin 6viktor iliukhin 6
Rahul

You make a mistake. You wrote
"Contact conObj = new Contact(Firstname = 'Rahul');" But, you should to write "Contact conObj = new Contact(LASTNAME = 'Rahul');", because "lastname" is required

 
Rahul Garg SFDRahul Garg SFD
Hi Viktor, 

The mistake you pointed out was made intentionally. I request you to re-read the question. 
sreelekha pateel 2sreelekha pateel 2
DML operations execute within a transaction. All DML operations in a transaction either complete successfully, or if an error occurs in one operation, the entire transaction is rolled back and no data is committed to the database. The boundary of a transaction can be a trigger, a class method, an anonymous block of code, an Apex page, or a custom Web service method.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_transactions.html
The above statement is from the below link which says the boundary could be anonymous block of code. So, the entire transction will get rolled back here.