Salesforce Platform Developer I (legacy) — Question 88
Given the following Apex statement:
Account myAccount = [SELECT Id, Name FROM Account];
What occurs when more than one Account is returned by the SOQL query?
Answer options
- A. The variable, myAccount, is automatically cast to the List data type.
- B. The first Account returned is assigned to myAccount.
- C. The query fails and an error is written to the debug log.
- D. An unhandled exception is thrown and the code terminates.
Correct answer: D
Explanation
When a SOQL query retrieves more than one record but is assigned to a single variable, it results in an unhandled exception. This is because the code expects only one Account, and receiving multiple records violates this expectation, causing the execution to terminate.