標準コントローラーとカスタムコントローラー拡張を使用して Visualforce ページにアクセスすると、「SObject-row-was-retrieved-via-SOQL-without-querying-the-requested-field (要求された項目をクエリせずに SOQL で SObject 行が取得された)」というエラーが発生することがあります。
標準コントローラーエラーが Visualforce ページのオブジェクトをクエリすると、ページで参照されているオブジェクトの項目のみを取得します。
これにより、Visualforce ページで不要な項目を取得しないため、効率が向上します。
カスタムコントローラーがページに含まれていない項目を参照すると、項目データが使用できず、エラーが発生します。
このシナリオを再現するには、以下のステップに従います。
ステップ 1: Apex コントローラーを作成します。
Apex クラスコード:
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;
}
}
ステップ 2: SOQL でクエリを実行し、recordTypeId とステータスを表示する Visualforce ページを作成します。
<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>
ステップ 3:
ここで VF ページをプレビューし、コマンドボタン [Get Record Type ID (レコードタイプ ID を取得)] をクリックします。recordTypeId は Visualforce ページに表示されます。
ここで、[Get Status (ステータスを取得)] をクリックすると、次のエラーが表示されます。
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Case.Status. (System.SObjectException: 要求された項目 Case.Status をクエリせずに、SOQL 経由で SObject 行が取得されました)
つまり、recordTypeId 項目を明示的にクエリしなくても、Apex クラスと VF ページの recordTypeId にアクセスできるということです。ただし、これは他の項目には適用されません。
注意: このシナリオをテストするには、組織内のケースオブジェクトに少なくとも 1 つのレコードタイプが必要です。
この問題を解決するための回避策は次の 2 通りです。
<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.