6

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.

11
  • 2
    I would assume this would work... but it could be because you have it wrapped inside of a <div> instead of another Salesforce component. Is there a reason you are using the <div> tags instead of a Salesforce tag? – dphil Jun 19 '15 at 18:44
  • 2
    If you can't get this to work any other way, you can try using StandardController.addFields in your controller to work around the problem. – Keith C Jun 19 '15 at 18:56
  • 1
    I don't know how particular you are about your apex testing, but if you strive for 100% test coverage, rather than use the StandardController.addFields method, you might want to add the field in the visualforce page (again) and set rendered to false. – martin Jun 19 '15 at 19:30
  • 1
    I hope my knowledge of this isn't out of date, but there was a point in time that addFields was an untestable line of code. – martin Jun 19 '15 at 20:04
  • 2
    I was able to track down a link that explains the issue quite well: raydehler.com/cloud/clod/… – martin Jun 19 '15 at 20:12
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.