+ Start a Discussion
tobibeertobibeer 

Template composition and SOQL exception

Can someone explain, why this VisualForce page...

 

<apex:page showHeader="false" standardStylesheets="false" standardController="SitePage__c" extensions="pageController">
    <apex:composition template="{!myTemplate}">
        <apex:define name="content">
            <apex:outputText escape="false" value="{!SitePage__c.Content__c}"/>
        </apex:define>
    </apex:composition>
</apex:page>

produces the following exception...

 

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

 

...yet the following, modified version of the page - introducing a quite redundant variable - does work...

 

<apex:page showHeader="false" standardStylesheets="false" standardController="SitePage__c" extensions="pageController">
    <apex:variable var="c" value="{!SitePage__c.Content__c}"/>
    <apex:composition template="{!myTemplate}">
        <apex:define name="content">
            <apex:outputText escape="false" value="{!c}"/>
        </apex:define>
    </apex:composition>
</apex:page>

?


The controller should be of no concern but looks like this...

 

 

public class pageController {

    private final SitePage__c sitepage;
    public pageController(ApexPages.StandardController stdController) {
        this.sitepage = (SitePage__c)stdController.getRecord();
    }
    
    public  PageReference getmyTemplate()  {
        return  Page.tplDefault;
    }
}

 

Rahul S.ax961Rahul S.ax961

Hi tobibeer,

 

Your code is correct, dunno why sometimes we face this weird behaviour.

You can get rid of it by querying in controller, but that too will be using redundant variable.

Eg:

 

public class pageController 
{
	private final SitePage__c sitepage;

	public pageController(ApexPages.StandardController stdController) 
	{
		//this.sitepage = (SitePage__c)stdController.getRecord();
		this.sitepage = [Select Name, Id, Content__c from SitePage__c where id =: stdController.getId()];
	}

	public  PageReference getmyTemplate()  
	{
		return  Page.tplDefault;
	}
}

 

 

Was just an Idea, let me know if you find some better solution.

tobibeertobibeer

Hi Rahut,

 

Thanks for your suggestion, although it wasn't quite sufficient to actually work. What I had to do was to provide a getter which I called getsitepage(), to eventually retrieve the custom field content in the VisualForce page via {!sitepage.Content__c}.

 

The controller now looks like this:

 

public class pageController {

    private final SitePage__c sitepage;
    public pageController(ApexPages.StandardController stdController) {
        this.sitepage = [Select Name, Id, Content__c from SitePage__c where id =: stdController.getId()];
    }
    
    public SitePage__c getsitepage() {
        return this.sitepage;
    }
    
    public PageReference getmyTemplate()  {
        return  Page.tplDefault;
    }
}

and my page now looks like this...

 

<apex:page showHeader="false" standardStylesheets="false" standardController="SitePage__c" extensions="pageController">
    <apex:composition template="{!myTemplate}">
        <apex:define name="content">
            <apex:outputText escape="false" value="{!sitepage.Content__c}"/>
        </apex:define>
    </apex:composition>
</apex:page>

I have no idea as to whether or not such a proceeding would introduce code vulnerabilities... nontheless, I have tried to declare the variable sitepage as public without using a dedicated getter, like this...

 

public final SitePage__c sitepage;

That , however, in combination with the following changes...

 

<apex:outputText escape="false" value="{!SitePage__c.Content__c}"/>

...to my VisualForce page, nothing at all was retrieved, just like your code.

 

Any other ideas outthere on how to clear the code from such (seemingly) redundant accessors, please let me know.

 

Cheers, Tobias.