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.
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.
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.
There are two ways to resolve this error:
SELECT id FROM Case to SELECT id, Status FROM Case).apex:outputText component with rendered="false"<apex:outputText value="{!Condition__c.Criterion__c}" rendered="false"/>
000385722

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.