+ Start a Discussion
chris_centrachris_centra 

result from test class: SObject row was retrieved via SOQL without querying the requested field

I have a VF controller that grabs the "current" record using the getRecord call:

 

        this.controller = stdController;
        this.theQuote = (SFDC_520_Quote__c)stdController.getRecord();

 

Now i'm writing the test class, but when I run it i'm getting an error:

 

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

 

However I am referencing this field in my VF page, (see below), so I should have access to it from Apex, right?  (That's how I read the documentation, at least.)  Also note that it works when walking through the app normally - it only fails in the test method.  I even added a hidden input to my VF page to try to resolve this (again, per the documentation) - so it's actually referenced twice.

 

What am I doing wrong?

 

                <apex:inputField value="{!SFDC_520_Quote__c.Discount_Type__c}" />   
 

 Thanks for your time

chris

 

lvivaninlvivanin

the requested field should be the part of select statement in the class-method/controller being called.

 

For example - object TestObject has fields: testField1, testField2,testField3

 

& we have a select statement like :-  TestObject[] to = [SELECT testField1, testField2 FROM TestObject];

 

we can't do  to[i].testField3 = 'something....' - because testField3 is not included in the selection list.

 

I think this error is related to this issue.

chris_centrachris_centra

Hello and thanks for the quick response.

 

Right - i understand this in the context of a standard query.  but i'm calling getRecord() from the controller - so I don't actually have access to the query.  According to the documentation, it should automatically query all fields which are referenced in the associated VF page...

 

Or am I missing something?

 

Thanks

chris

Luke@TWSLuke@TWS

You'll probably need to provide the standard controller with all of the fields it needs. e.g.

 

SFDC_520_Quote__c stdRecord = [select Id, Discount_Type__c from SFDC_520_Quote__c limit 1]; ApexPages.StandardController stdController = new ApexPages.StandardController(stdRecord);

myController testController = new myController(stdController);

 

 

Nick1746323Nick1746323
Try using a call to "Test.setCurrentPage(pageRef)" in your test method.