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
Suraj GharatSuraj Gharat 

Despite calling addFields method, I get "SObject row was retrieved via SOQL without querying the requested field" error

Hi everyone,

I've a VF page with standard controller and an extension. In an action invoked by a postback request, I add one new field into the standard controller (Using reset() & addField() methods ) and then refer that new field dynamically onto the page as it rerenders. When the page rerenders after this postback, I get follwoing error:

SObject row was retrieved via SOQL without querying the requested field: Account.Name 

Here "Name" is the new field that I add into the action method. The error suggest that I didnt understand the concept of reset() and addFields() correctly and may be using in wrong manner.

Please help me resolve this error and explain the use/purpose & right sequence of these two methods.
 
Best Answer chosen by Suraj Gharat
William TranWilliam Tran
Unless you are using metadata API to create new fields, you do KNOW ALL the fields ahead of time that the user could be using, so you can add them all in the controller or VF page.

It's not ideal but definitely a viable work around.

Thx

All Answers

Anirudh SinghAnirudh Singh
Hi Suraj,

Have you used SOQL Query anywhere in your code, check if it is having Name field queried. If this also doesn't solves your issue please paste your code, else it will be difficult to resolve your issue.

Please let me know if this helps.

Thanks and Regards,
Anirudh Singh
Suraj GharatSuraj Gharat
Thank you Anirudh for your reply.

Here I do not access any queried record, rather I access the record supplied by standard controller.

Ok, I'm posting my page & controlle, this will assit in help.
 
<apex:page standardController="Account" extensions="AddFieldsDemoCtrl">
    {!Account[f]}
    <br/>    
    <apex:form >
        <apex:commandButton value="Press to invoke action" action="{!action1}" />
    </apex:form>
</apex:page>
 
public class AddFieldsDemoCtrl {
    ApexPages.StandardController std;
    public String f{get;set;}
    public AddFieldsDemoCtrl(ApexPages.StandardController std){
        this.std=std;
        f='CreatedBy.Name';
        std.addFields(new List<String>{f});
    }  
    public void action1(){
        f='Name';
        std.reset();
        std.addFields(new List<String>{f});
	}
}

 
William TranWilliam Tran
StandardController.reset() error can throw error: SObject row was retrieved via SOQL without querying the requested field

Apex , VisualForce

Last updated 2015-06-30 ·Reference W-1571056 ·Reported By 16 users

No Fix

Summary
When using StandardController.reset() with dynamic visualforce, the error "SObject row was retrieved via SOQL without querying the requested field" can be thrown

more info:
https://success.salesforce.com/issues_view?id=a1p30000000Sw8tAAC

Workaround
A workaround for now could be to statically declare fields that might be included, but don't render them:

<apex:outputText value="{!myObject__c.field__c}" rendered="false"/>
OR
<apex:inputhidden value="{!myObject__c.field__c}" />


As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.

Thanks
 
Suraj GharatSuraj Gharat
Thank you William for sharing this.

After debugging this error, I found the methods reset & addFields dont thorw any exception. The error comes when we try to access the field which was not added from the beginning, may be because reset & addFields are not doing thier work as expected.

As suggested, referencing fields in the markup could be a good workaround for this, but I fear, it will not work in case we dont know the fields beforehand or if it depends on user input.
William TranWilliam Tran
Unless you are using metadata API to create new fields, you do KNOW ALL the fields ahead of time that the user could be using, so you can add them all in the controller or VF page.

It's not ideal but definitely a viable work around.

Thx
This was selected as the best answer