PDI · Question #169
PDI Question #169: Real Exam Question with Answer & Explanation
The correct answer is D: Collect the insert method return value a Saveresult variable. When using Database.insert() with allOrNone set to false, exceptions are suppressed, and the method returns a SaveResult object that must be inspected to determine success or failure and retrieve error details.
Question
A developer is debugging the following code to determinate why Accounts are not being created Account a = new Account(Name = 'A'); Database.insert(a, false); How should the code be altered to help debug the issue?
Options
- AAdd a System.debug() statement before the insert method
- BAdd a try/catch around the insert method
- CSet the second insert method parameter to TRUE
- DCollect the insert method return value a Saveresult variable
Explanation
When using Database.insert() with allOrNone set to false, exceptions are suppressed, and the method returns a SaveResult object that must be inspected to determine success or failure and retrieve error details.
Common mistakes.
- A. Adding a
System.debug()before theinsertmethod would only display the record's state prior to the operation, not the actual outcome or any errors that occurred during the insert. - B. A
try/catchblock will not catch exceptions whenallOrNoneisfalsebecauseDatabase.insert()suppresses exceptions and returns error information in theSaveResultobject instead. - C. Changing the
allOrNoneparameter totruewould alter the method's behavior to throw an exception on failure, which changes the original debugging scenario where exceptions are suppressed.
Concept tested. Database DML Error Handling
Topics
Community Discussion
No community discussion yet for this question.