Print this page

SObject row can be retrieved via SOQL without querying the requested field on VF Page

Knowledge Article Number 000230405
Description RecordTypeId field can be accessed in Apex class and in VF page without explicitly querying the recordTypeId field.  the following error occurs for all other fields but NOT for RecordTypeID field in the VF context: "SObject row was retrieved via SOQL without querying the requested field"
Resolution This is working as expected. When executing the following SOQL from VF(and Apex), RecordTypeId is added to SOQL implicitly.

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. 
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.




promote demote