function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BrianWKBrianWK 

SObject row was retrieved via SOQL without querying the requested field

Hey everyone.

 

I'm running a controller extension of the Opportunity Standard Controller being used across 3 pages.

 

On Page 1, I have an outputfield of all fields that get used on Page 1 - 3 that are rendered false (to accomodate the SOQL Query method used by visualforce).

 

In my controller extension I have a method that uses one of these fields and returns a list of strings. If I load each page separately (via URL not using Page References) it works perfectly. However, if I use any of my page references to move from Page 1 to 2 I get:

 

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

 

Class.OpportunityPAWizardExtension.getInvoiceDates: line 80, column 43
Class.OpportunityPAWizardExtension.<init>: line 11, column 9
External entry point

 

I know there's values for the fields on using. It's the same Opportunity ID that used between the page references, although I do have the redirect set to true (one of the references is in the apex page and without redirect true I get an invalid root component error).  The method causing the error doesn't use any user input just calculates some difference between dates for a list of invoice dates.

 

Code giving me the errror it errors online 2 when I call the New Contract Start Date Field:

Opportunity Opp = (Opportunity)controller.getRecord(); date StartDate = date.valueof(Opp.New_Contract_Start_Date__c); date EndDate = date.valueof(Opp.New_Contract_End_Date__c);

 

So why does this work on all 3 pages called separate, but breaks when using a page reference? I thought it might have to do with the SoQL error, but I check and I have both the fields listed above on the pages. So the standard controller would query them right?

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae
What if you included the same hidden field on each page?  That might get them to pull through for you?  Not sure, but worth a try?

All Answers

BDArnzBDArnz

Opportunity Opp = (Opportunity)controller.getRecord(); Opp = [Select .... blah blah blah];

 

I had similar trouble and found the only way to correct it was to do a direct query of the record right after the .getRecord() method.  For some reason that statement alone doesn't seem to load entire record.  You need the getRecord to instatiate the standard controller but the query aftwards gave me full access to the data in the record...

 

There might be cleaner way but that helped me.

 

BrianWKBrianWK

but doesn't querying the object re-write what you did with getrecord?

 

Opp = getrecord()

Opp = select soql query

 

I would think that as long as the fields are listed on the page, the getRecord would load all the needed fields. Otherwise you just need it to get the ID of the record (unless you pull from the parmeters) and have SoQL all over the place.

BDArnzBDArnz

I would have thought the getrecord would load all the necessary fields too.  Again, maybe it should and there's maybe a way to make it do so but I haven't found it.  I understand the getrecord is necessary to initialize the standard controller so your page still has access to all the standard functions but I couldn't find a reference to what fields it does or doesn't load.

 

You should run the query right in the constuctor function right after the getrecord.  That way it loads the record when the controller loads which is before you make any changes to the record on your page.

 

I had exactly the same problem you're seeing and added the query to make sure all the fields get loaded.  That cleared it up except of course when I modify the controller to access other fields and forget to go add those fields to the query...

 

Good luck...

JimRaeJimRae
What if you included the same hidden field on each page?  That might get them to pull through for you?  Not sure, but worth a try?
This was selected as the best answer
BrianWKBrianWK

Jim,

 

Here's what worked. I had to include the hidden field at the TOP of my page. I had everything that I needed hidden on the bottom of the page. This was mainly to make coding easier on me (less scrolling). The moment I moved the outputfield to the top of the page it worked!

 

It also required the fields on both Page 1 and Page 2.

jkucerajkucera

Thanks Jim! You saved me from this error today as well.  

 

Looks like controllers only query form fields on the VF page, so including a hidden field resolves the error and makes those fields usable in the controller.

BravoEncoreBravoEncore

I know that this is an old thread, but as it was pretty high up in the Google results for this specific error message, it's probably getting quite a few hits from folks with this issue. I think that there is a better solution to this issue: the "addFields" method on the StandardController class. To borrow the fields from the original question...

 

public with sharing class OpportunityPAWizardExtension {
	private final Opportunity opp;

	public OpportunityPAWizardExtension(ApexPages.StandardController controller) {
		controller.addFields(new String[]{
			'New_Contract_Start_Date__c',
			'New_Contract_End_Date__c'
		});
		this.Opp = (Opportunity)controller.getRecord();
		...
	}
}

The key is to add all the fields before you do the "getRecord()" call. Also, to refer to those fields by "StartDate" and "EndDate," just add them as properties then give them values after the getRecord() call. Including the fields as hidden inputs will work, but this should achieve the same results without having extra, hidden HTML on each page.

 

Here's the link to the official docs: 

 

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

 

I hope that this helps.

ANUJIT DASANUJIT DAS
@bravoencore, you are right. It worked and helped me also.. Thanks!
Harry DhanoaHarry Dhanoa
@bravoencore you are a saviour.
Ben Rowley 4Ben Rowley 4
@bravoEncore, I was wondering why my debugs contained pretty much every field except the one I was interested in just after getRecord(), thanks again for the useful comment!