Loading

SObject row retrieved via SOQL without querying the requested field

Publiceringsdatum: May 8, 2026
Beskrivning

The error "System.SObjectException: SObject row was retrieved via SOQL without querying the requested field" can occur when accessing a Visualforce page that uses both a standard controller and a custom controller extension.

When a standard controller queries an object for a Visualforce page, it only retrieves the fields that are explicitly referenced in the page markup. This improves efficiency by avoiding retrieval of unused fields. When a custom controller extension then attempts to access a field on that object that was not referenced in the Visualforce page, that field's data is not available in the queried record, and Salesforce throws this error.

Note:
RecordTypeId is a special system field that the standard controller always includes in its query, regardless of whether it appears on the Visualforce page. This means accessing RecordTypeId in a controller extension will succeed, but accessing other standard or custom fields (such as Status) will fail unless those fields are present in the page markup.

Lösning

Root Cause:

When a Visualforce standard controller queries an object, it uses a SOQL statement that only includes fields referenced in the page markup. If the custom extension references additional fields not on the page, those fields are absent from the queried record, causing the SObjectException.

Reproduction Steps:

To reproduce this scenario you can follow the steps mentioned below:

Step 1:   Create a Apex controller:

Apex class code: 

public class recTypeController {
public String recordTypeId{get;set;}
public String Status{get;set;}
public PageReference testCode() {
    List<case> cases = [SELECT id FROM case LIMIT 1]; 
    case  x  =cases[0]; 
    recordTypeId = 'RecordType ID for Case is ' + x.recordTypeID;
    return null; 
   }
public PageReference testStatus() {
    List<case> cases = [SELECT id FROM case LIMIT 1]; 
    case  x  =cases[0]; 
    Status= 'Status of this Case is ' + x.status;
    return null; 
   }

}



Step 2: Create a visual force page to display recordTypeId and status with querying in the SOQL:
 

<apex:page controller="recTypeController " > 
<apex:form id="caseForm"> 
<apex:commandButton value="Get Record Type ID " action="{!testCode}" rerender="recId"/> 
<apex:outputPanel id="recId">
<apex:outputText > {!recordtypeid}</apex:outputText> 
</apex:outputPanel>
<apex:commandButton value="Get Status " action="{!testStatus}" rerender="cStatus"/> 
<apex:outputPanel id="cStatus">
<apex:outputText > {!status}</apex:outputText> 
</apex:outputPanel>
</apex:form> 
</apex:page>


Step 3: Now preview the VF page and click on the command button "Get Record Type ID". The recordTypeId is displayed on the Visualforce page. 

Step 4: Now, click "Get Status" and you see an error: 
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Case.Status.

So, this implies that we can access recordTypeId in Apex class and in VF page without explicitly querying the recordTypeId field. However, this doesn't apply for other fields.

Note: For this scenario to work, the org must have at least one Record Type on the Case object.

Workarounds:

There are two ways to resolve this error:

  • Add the field to the SOQL query in the custom controller: Update the controller's SOQL query to explicitly include the field that is being accessed (for example, change SELECT id FROM Case to SELECT id, Status FROM Case).
  • Add a hidden reference to the field on the Visualforce page. Add the field to the Visualforce page as a hidden apex:outputText component with rendered="false"

    For example: 
<apex:outputText value="{!Condition__c.Criterion__c}" rendered="false"/>


 

Knowledge-artikelnummer

000385722

 
Laddar
Salesforce Help | Article