Variable assignment
If you select data from a data table, you assign the results to a variable. This can be one record or a list of records. But what if the result doesn't contain any records? Maybe you have observed that I always use an assignment of a new list, and add the records to that list. When you assign a variable to a simple query (without the definition of a list), you receive QueryException (more about that in the exception handling, later in this book).
You can avoid this to assign your query results to a List<SObject> variable, instead of an sObject variable. Here is an example to illustrate this. The following is a simple query, assigned to a variable from the Account type. If the query doesn't have any results, the execution ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access