Platform
List has no rows for assignment to SObject
Handle missing query results deliberately in Apex, distinguish optional records from required ones and avoid hiding duplicate matches.
A single-record query assignment assumes that the result contains exactly one record. That assumption breaks when the match disappears.
Salesforce documents the exception for zero or multiple rows assigned directly to one SObject. A LIMIT 1 prevents multiple returned rows, but it doesn’t create a record when the filter matches nothing.
Make the empty result explicit
This illustrative fragment assumes accountId is an input ID. It returns an optional Account value from a bounded query result:
List<Account> matches = [
SELECT Id, Name FROM Account WHERE Id = :accountId LIMIT 1
];
Account selected = matches.isEmpty() ? null : matches[0];
Decide what the caller should do with null. A search screen can show no match; a required parent for a write should produce a clear validation result. Continuing with a fabricated blank Account changes the meaning of the operation.
When matching a business identifier rather than a Salesforce ID, test duplicates separately. Choosing the first record can hide a data-quality problem. Use a uniqueness constraint where suitable, or explicitly reject ambiguous matches before a write.
Investigate why the record was absent
Check the exact filter values and the executing user. A record visible to an administrator may be excluded under the integration’s sharing or operation mode. A date filter, lifecycle status or stale external-ID mapping can also explain the empty result.
Don’t remove the filter or broaden permissions merely to make the query return something. Reproduce the failure using the intended user and verify which record the business operation was meant to select.
Current Apex documentation also supports the null-safe operator with single-row query expressions. Avoid the blanket claim that null-safe syntax can never help. An explicit list remains useful when reviewers need to see and test each cardinality outcome.
Test zero, one and ambiguous matches
Create test records inside the test rather than relying on a record already present in the org. Test the normal case and a valid-looking identifier that matches nothing. Where matching can be ambiguous, create two candidates and assert the defined response.
For an integration or agent action, return a stable result such as “record not found” without revealing inaccessible record details. Don’t describe a missing lookup as a temporary service outage unless there is evidence of one.
The important change is the query contract. Once the caller knows whether absence is acceptable, the implementation can handle it without a broad catch block that hides unrelated query failures.