[SalesForce] SObject row was retrieved via SOQL without querying the requested field. Transaction_Survey__c.Transaction_Score_Other_Reason__c

I'm getting this error in a Visualforce page I'm working on.

I understand that this error comes up when you try to reference a field you haven't queried, but the problem is, I have it referenced on in my VF Page, and I don't use it specifically in my controller.

In my controller I'm using the standardController.getRecord() method to retreive the record and store it in a variable called "survey".

public with sharing class SurveyPageController {

    public Transaction_Survey__c survey {get; set;}

    public List<SelectOption> transScoreReason {get; private set;}
    public List<String> selectedReasons {get; set;}

    public SurveyPageController(ApexPages.StandardController stdController)
    {
        //stdController.addFields(new List<String>{'Transaction_Score_Other_Reason__c'});
        this.survey = (Transaction_Survey__c)stdController.getRecord();

        transScoreReason = new List<SelectOption>();
        for ( PicklistEntry e : SObjectType.Transaction_Survey__c.fields.Transaction_Score_Reason__c.getPicklistValues() )
        {
            transScoreReason.add( new SelectOption(e.getValue(), e.getLabel()) );
        }
    }
}

In my VF Page I use an apex:inputField value="{!survey.Transaction_Score_Other_Reason__c}" to get user input.

<apex:page standardController="Transaction_Survey__c" showHeader="false" sidebar="false"
                extensions="SurveyPageController"
                standardStylesheets="false">
    <apex:form>
    <div id="container" class="container">

        <apex:pageMessages id="messages" />

        <p class="question">Why did you give this rating?</p>

        <div class="response_container">
            <div class="response">
                <apex:selectCheckboxes id="response" layout="pageDirection" value="{!selectedReasons}">
                    <apex:selectOptions value="{!transScoreReason}" />
                </apex:selectCheckboxes>
                <apex:inputField value="{!survey.Transaction_Score_Other_Reason__c}" />
            </div>
        </div>
    </div>
    </apex:form>
</apex:page>

What I don't understand is why I'm getting this error. Of course if I uncomment the addFields() method in my conroller everything works fine, but I'd like to know why I'm getting this error to begin with.

Best Answer

You need to use this in the VF page to have it work:

Transaction_Survey__c.Transaction_Score_Other_Reason__c

instead of this:

survey.Transaction_Score_Other_Reason__c

As it stands, you are only referencing the field in a controller property not the actual object in the StandardController which is why you get the error.

Using what I stated will still populate the survey property correctly when updated and output the value appropriately