You need to sign in to do that
Don't have an account?

Referencing related records using custom objects and a custom controller
OK, this is driving me nuts.
I have a series of custom objects related in various ways - some master/data some lookup. I'm trying to access these in a sites VF page and I can't seem to do it with the standard controller, so I have a custom extension ... but that doesn't seem to work either.
This query works in the Eclipse schema
SELECT name, (Select Box_Size__c, Content__c from Page_Element__r) FROM Page_Layout__c where name = 'capabilities'
and returns 'name' and page_element__r which if I click on gives me Box_Size__c and the ID of Content__c (a lookup detail of page_element)
So here's my controller get:-
public Page_Layout__c getElement() {
string pgl =ApexPages.currentPage().getParameters().get('pgl');
if (pgl == NULL) pgl = 'home'; // default to home page if no pgl not set
Page_Layout__c element = [select name, (select Box_Size__c, Content__c from Page_Element__r) from Page_Layout__c where name = :pgl];
Return element;
}
And here's the VF page snippet:-
<apex:repeat var="elements" value="{!element}" >
{!elements.Page_Element__r.Box_Size__c}<br/>
</apex:repeat>
... which fails with the "Save error: "Unknown property 'VisualforceArrayList.Box_Size__c' "
So what am I doing wrong? ... and is there a reference on relationships between custom objects and SOQL for them that someone can point me to so that I can try not to make the same mistakes again?
Many thanks.
If you want to iterate through the results of the inner query :
public Page_Element__c getElement() {
string pgl =ApexPages.currentPage().getParameters().get('pgl');
if (pgl == NULL) pgl = 'home'; // default to home page if no pgl not set
Page_Layout__c element = [select name, (select Box_Size__c, Content__c from Page_Element__r) from Page_Layout__c where name = :pgl LIMIT 1];
Return element.Page_Element__r;
}
And here's the VF page snippet:-
<apex:repeat var="elements" value="{!element}" >
{!elements.Box_Size__c}<br/>
</apex:repeat>
All Answers
I reckon its coz of the way your query result is being returned - its treating it as a List rather than a single row result.
Page_Layout__c[] element = [select name, (select Box_Size__c, Content__c from Page_Element__r) from Page_Layout__c where name = :pgl];
Return element[0];
OR
Page_Layout__c element = [select name, (select Box_Size__c, Content__c from Page_Element__r) from Page_Layout__c where name = :pgl LIMIT 1];
Return element;
Thanks - but no. Still the same error on the VF page.
Doh, the same applies to your inner query too - its returns a list
So it should be elements.Page_Element__r[0] in ur VF page
<apex:repeat var="elements" value="{!element}" >
{!elements.Page_Element__r[0].Box_Size__c}<br/>
</apex:repeat>
Nah, that doesn't work either.
The outer query is designed to only match one record - so I can fix that with the limit 1.
But I really want to return the list of the inner query of type Page_Element__c so that I can use the apex:repeat to render all the records on the page in turn.
How would I do that?
If you want to iterate through the results of the inner query :
public Page_Element__c getElement() {
string pgl =ApexPages.currentPage().getParameters().get('pgl');
if (pgl == NULL) pgl = 'home'; // default to home page if no pgl not set
Page_Layout__c element = [select name, (select Box_Size__c, Content__c from Page_Element__r) from Page_Layout__c where name = :pgl LIMIT 1];
Return element.Page_Element__r;
}
And here's the VF page snippet:-
<apex:repeat var="elements" value="{!element}" >
{!elements.Box_Size__c}<br/>
</apex:repeat>
Thank you!
Cheers !