Using SOQL Queries That Return One Record
SOQL queries can be used to assign a single sObject value when the result list contains only one element.
When the L-value of an expression is a single sObject type, Apex automatically assigns the single sObject record in the query result list to the L-value. A runtime exception results if zero sObjects or more than one sObject is found in the list. For example:
This usage is supported with the following Apex types, methods, or operators:
-
Database.querymethod. -
Safe Navigation Operator. See Safe Navigation Operator.
-
Null Coalescing Operator. See Null Coalescing Operator.
-
Map.values.Although currently supported, Salesforce recommends against using this feature with
Map.values.