Reply
Trusted Contributor
sparky
Posts: 259
Accepted Solution

error referencing field, using standard controller extension

I've got a VF page & controller extension for a custom obj., and I've set it to be the override for the Edit action for the obj.

Here's simplified versions of the code.

Page:

Code:
<apex:page standardController="Condition__c" extensions="NNRG_CNT_Condition" tabStyle="Assessment__c">

<apex:form >
 <apex:pageblock title="Condition Edit">
  <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!save}" />
  </apex:pageBlockButtons>

  <apex:pageblockSection columns="1" title="Condition" collapsible="false">
   <apex:pageblockSectionItem >
    <apex:outputLabel value="Assessment" /> 
    <apex:outputText value="{!asName}" />
   </apex:pageblockSectionItem>
   <apex:pageblockSectionItem >
    <apex:outputLabel value="Criterion" /> 
    <apex:outputText value="{!critNbr} ({!critTxt})" />
   </apex:pageblockSectionItem>

   <apex:inputField value="{!Condition__c.Condition_Text__c}" />
   <apex:inputField value="{!Condition__c.Pre_Condition__c}" />

  </apex:pageblockSection>
 </apex:pageblock>
 </apex:form>
 
</apex:page>

 and controller:

Code:
public class NNRG_CNT_Condition {

 private final Condition__c cond;
 public string asName {get;set;}
 public string critNbr {get;set;}
 public string critTxt {get;set;}
 
 // constructor
 public NNRG_CNT_Condition (ApexPages.StandardController stdController) {
  this.cond = (Condition__c)stdController.getRecord();
  
  if ( this.cond.Id == null ) {
   this.cond.Criterion__c = System.currentPageReference().getParameters().get('cr');
  }
  
  string critID = this.cond.Criterion__c;
  Criterion__c crit = [SELECT Id, Name, Short_Text__c, Assessment__r.Name, Criterion_Number__c FROM Criterion__c 
   WHERE Id=:critID LIMIT 1];
     
  asName = crit.Assessment__r.Name;  
  critNbr = crit.Criterion_Number__c;  
  critTxt = crit.Short_Text__c;   
 }
}

 The problem is that when I attempt to edit a Condition record, VF gives me the following error:

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Criterion__c

The line cited is:
     string critID = this.cond.Criterion__c;


I don't understand this error.  Since I'm starting from an existing record, doesn't this line:
     this.cond = (Condition__c)stdController.getRecord();
load in the record in its entirety?  Why would one or more fields be left out?

BTW, Condition__c is a master-detail relationship field.  So I know that there's data in the field for the record I want to edit.

Thanks for any help!


PS This code works fine when I use the same page to add a new Condition record, instead of editing an existing one.  That's where the 'cr' querystring param comes in.  I pass that in to indicate the id that should be put into the Criterion__c field.  That works fine, but not when attempting to edit an existing record.




Matthew Scholtz
Consultant
Groundwire
Moderator
jwetzler
Posts: 851

Re: error referencing field, using standard controller extension


sparky wrote:
I don't understand this error.  Since I'm starting from an existing record, doesn't this line:
     this.cond = (Condition__c)stdController.getRecord();
load in the record in its entirety?  Why would one or more fields be left out?

No.  You are getting the record from the standard controller, and the standard controller automatically constructs your SOQL query for you based on the fields that are referenced in your page.  It would not make sense performance-wise for us to load up every single field on your object if all you want to reference is a couple of fields.

So to get around this you can either do another query for your field in your controller (not recommended), or better yet you can simply include a hidden reference to it on your page somewhere, something like:
Code:
<apex:outputText value="{!Condition__c.Criterion__c}" rendered="false"/>

 The standard controller will pick it up when constructing your query and you will be free to reference it in your extension.
Jill Wetzler
Developer - salesforce.com
Check out the developer documentation
Got an idea?
Trusted Contributor
sparky
Posts: 259

Re: error referencing field, using standard controller extension

Thanks, Jill!  That seems like a very important concept, that I somehow completely missed.  It certainly makes sense from a resource-optimization standpoint, but it's not exactly intuitive. 

Just out of curiosity, is this noted in the docs anywhere?  I never saw it, but could easily have missed it.
Matthew Scholtz
Consultant
Groundwire
Moderator
jwetzler
Posts: 851

Re: error referencing field, using standard controller extension

I think it's something you could infer from the standard controller doc but it's not completely clear.  My doc writer just asked me the exact same question a couple days ago :)  I'll see if we can add something.
Jill Wetzler
Developer - salesforce.com
Check out the developer documentation
Got an idea?