You need to sign in to do that
Don't have an account?
Rahul 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.
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.
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
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;
All insertions took place in one transaction and the transaction fails, so all records are rolledback even the one inserted form Database.insert().
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
The mistake you pointed out was made intentionally. I request you to re-read the question.
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.