Platform
SObject field not queried: fix the Apex data contract
Fix SObject field-access exceptions by aligning selectors, relationship fields and downstream method requirements.
An Account can arrive with its ID and still lack the Name field your method expects. The record exists; the retrieved representation is incomplete for that operation.
A SOQL query selects fields for the result. Salesforce’s query documentation describes this field-based retrieval. Reading a field that wasn’t retrieved can raise an SObject exception rather than returning a useful business value.
Add the field at the correct selector
If the operation needs Account.Name, include Name in the query that supplies it. If it needs Owner.Name, selecting only OwnerId doesn’t establish that relationship field’s value.
List<Account> accounts = [
SELECT Id, Name, OwnerId, Owner.Name
FROM Account
WHERE Id IN :accountIds
];
This illustrative fragment assumes accountIds is a bounded input set. It demonstrates the field contract; production code still needs appropriate permissions, sharing and handling of absent results.
Don’t add every field to every query as a precaution. That expands data exposure and memory usage while hiding what a method depends on. Prefer a named selector for a specific operation with an explicit list of required fields.
Trace who constructed the SObject
The same method may receive a trigger record, a partial query result or an SObject assembled from an API payload. Those inputs aren’t interchangeable. A method signature accepting Account doesn’t declare which fields are available.
Document that contract or pass a purpose-built input type containing the values the operation needs. If the method must query its own authoritative data, do so in bulk at a clear boundary rather than once per record.
Also inspect sanitization steps. Field-access enforcement can remove or reject data. Follow the secure Apex guidance for the class API version and operation mode. Broadening a query doesn’t grant a user access to a protected field.
Separate missing data from missing permission
A selected field whose stored value is null is different from an unselected field. A relationship that doesn’t exist is another case. Tests should distinguish all three rather than using one generic null check.
Test the consumer with a fully populated result and a deliberately partial result. If partial inputs are invalid, assert a clear contract failure. If the method supports them, assert the specific fallback and ensure it doesn’t make an unauthorized write.
The durable fix is agreement between the selector and its consumer. A catch block around the final field read makes the symptom less visible while leaving that disagreement in place.