CRT-450 practice question 6 of 30
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?
Full set: 201 questionsThe Apex syntax sObject variable = [SOQLquery]; is specifically designed to assign a single record to an sObject variable. If the SOQL query returns more than one record, the Apex runtime environment throws a System.QueryException with the error message "List has more than 1 row for assignment". If this exception is not handled within a try-catch block, it is considered an unhandled exception, which immediately terminates the transaction and prevents further code execution.
1. Salesforce Apex Developer Guide
"SOQL and SOSL Queries": The guide explains that assigning a query result to a single sObject variable is only valid if the query returns exactly one record. It states
"If the query returns no records
a System.QueryException is thrown... Likewise
if the query returns more than one record
a System.QueryException is thrown."
Reference: Salesforce Apex Developer Guide
Chapter 3: "Database & DML"
Section: "Writing SOQL Queries".
2. Salesforce Apex Developer Guide
"Exception Class and Built-In Exceptions": This section documents the System.QueryException. It is described as an exception that is thrown for errors that occur during SOQL or SOSL query execution
including when a query that is expected to return a single row returns multiple rows.
Reference: Salesforce Apex Developer Guide
Appendix: "Apex Language Reference"
Section: "Exception Class and Built-In Exceptions".
Keep practicing
Take the free CRT-450 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CRT-450 practice questions
- Question 2A developer created a child Lightning web component nested inside a parent Lightning web …
- Question 4A developer needs to make a custom Lightning Web Component available in the Salesforce Cl…
- Question 8Which action causes a before trigger to fire by default for Accounts?
- Question 9Universal Containers (UC) wants to lower its shipping cost while making the shipping proc…
- Question 10A custom picklist field, Pool Preference ¢, exists on a custom object. The picklist conta…
- Question 11Universal Containers decides to use exclusively declarative development to build out a ne…
