+ Start a Discussion
maxout2maxout2 

can std getrecord() be used in extension's constructor

 

the getrecord() of the standard controller is used within a constructor method of custom object's extension controller.

 

when accessing a field of the custom object afterwords,  error comes:

 System.SObjectException: SObject row was retrived via SOQL without querying the requested field: Status__c

Class.xxxControllerExtension.<init>: line ? colum ?

 

is this banned to use getrecord in constructor?

Tryed moving doConfirm() out of constructor, say a public void test method, got the same error?

Tryed accessing standard field [Id], ok, return the right id passed in through a custom button click;

Tryed accessing standard field [Name], same error;

the code seems normal after looking around. but what's wrong or missing? idea? any doc?

 

[code partially]

 public RequestControllerExtension(ApexPages.StandardController stdController)
 {
  controller=stdController;

  doConfirm();
 }
 
 private void doConfirm()
 {
  this.req=(Request__c)controller.getRecord();
  if(req.Status__c=='New')
  {
   req.Status__c='Dispatched';
   controller.save();
   Msg='Change status:' + req.Status__c + '.';
  }
  else
  {
   Msg='Invalid status:' + req.Status__c + '.';
  }
 }

 

 

I found a similar Q:

 

http://boards.developerforce.com/t5/Apex-Code-Development/Trouble-adding-a-getRecord-to-an-existing-Extension/m-p/128543

Best Answer chosen by Admin (Salesforce Developers) 
sebcossebcos

Hi maxout2,

the getrecord method has only got access to what is on the visualforce page.

Please find this on the visualforce guide:

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

In particular:

"Note that only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression."

There is a workaround given in the documentation in the Tip box in case you don't want to use SOQL.

All Answers

sebcossebcos

Hi maxout2,

the getrecord method has only got access to what is on the visualforce page.

Please find this on the visualforce guide:

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

In particular:

"Note that only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression."

There is a workaround given in the documentation in the Tip box in case you don't want to use SOQL.

This was selected as the best answer
maxout2maxout2

Thanks Sebcos.

 

 

Have another after testing:

controller.save();  didn't save the change of the field.

Checking...