1

My code below is giving me an error- System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.mfField__c I'm not clear on why I'm getting this error...

Help?

<apex:page standardController="Account" extensions="myObjectController">
    <apex:form >
        <apex:pageBlock title="Archived Records">
            <apex:pageBlockTable value="{!relatedRecords}" var="item">
                <apex:column value="{!item.name}"/> 
            </apex:pageBlockTable> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public with sharing class myObjectController {

    Account current;
    List<Account> relatedRecords{get;set;}

    public myObjectController(ApexPages.StandardController controller) {
       this.current = (Account )controller.getRecord();       
       this.relatedRecords = [select id, name, mfField__c from  Account  where mfField__c = :this.current.mfField__c];
    }

}

3 Answers 3

2

The Account being returned by the Standard Controller doesn't have the mfField__c set on it and you're using it in your where clause.

Use the addFields method on your controller constructor to set the field on the record.

http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardcontroller.htm

4
  • it does have the field set though.... there is a value for the field Aug 6, 2013 at 18:08
  • this.current.mfField__c ? Aug 6, 2013 at 18:17
  • Yea.... it has a value already Aug 6, 2013 at 18:18
  • Are you saying that on the basis of a System.debug somewhere ? The field may have a value, but since its not being referenced in VF, the field value will not get explicitly initialised until you reference it or invoke the addFields method. Aug 6, 2013 at 18:33
1

By default, a Visualforce page and its associated controller will only load the fields it sees referenced on the page. Your SOQL statement is referencing a field that is not used on the page. The easiest way to fix that is to reference the field on the page and set the rendered flag to false.

0

Try this

<apex:page standardController="Account" extensions="myObjectController">
<apex:form >
    <apex:inputHidden  value="{!Account.mfField__c}"/>
    <apex:pageBlock title="Archived Records">
        <apex:pageBlockTable value="{!relatedRecords}" var="item">
            <apex:column value="{!item.name}"/> 
        </apex:pageBlockTable> 
    </apex:pageBlock>
</apex:form>

and controller as

public with sharing class myObjectController {

Account current;
List<Account> relatedRecords{get;set;}

public myObjectController(ApexPages.StandardController controller) {
   this.current = (Account )controller.getRecord();       
   this.relatedRecords = [select id, name, mfField__c from  Account  where mfField__c = :current.mfField__c];
}

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .