+ Start a Discussion

VisualForce page relationship queries

I have an Apex class that does a  simple query

private List<Opportunity> opportunities; public List<Opportunity> getOpportunities() { opportunities= [Select o.Id, (Select Applicant_Type__c, First_Name__c, Last_Name__c From Members_Info__r) from Opportunity o where o.id='<opp_id>']; return opportunities; }



I'm using the results from this query in a VF page



<apex:pageBlockSection title="Info"> <apex:dataTable value="{!opportunities}" var="mbrinfo"> <apex:column headerValue="Opp Id"> <apex:outputText value="{!mbrinfo.Id}" /> </apex:column> <apex:column headerValue="First Name"> <apex:outputText value="{!mbrinfo.Members_Info__r.First_Name__c}" /> </apex:column> </apex:dataTable> </apex:pageBlockSection>



The display works fine for column Opp Id but always gives an error for mbrinfo.Members_Info__r.First_Name__c

First_Name__c is a custom field within the Members_Info__c object


The error I get is  - Error: Unknown property 'VisualforceArrayList.First_Name__c'


Does anyone know if I'm missing anything?


Any help is appreciated


Off the top of my head, I'd guess Members_Info_r is not a SObject, but rather a List of whatever objects Members_Info_r is pointing to. 


Perhaps you want: (Note, added "[0]")


<apex:outputText value="{!mbrinfo.Members_Info__r[0].First_Name__c}" 


But, I'm also tired and had a full day in the sun at the Maker's Faire in San Mateo, so I'm a little out of it.  :-)


Good luck.



From the syntax of the soql it's clear that the relationship is one to many with members_info__c on the many side and opportunity on the one side. So you need to recognize that there are multiple members_info per opportunity.


You need something like: 


 <apex:repeat value="{!mbrinfo.Members_Info__r}" var="m">
<apex:outputText value="{!m.First_Name__c}" />

And if you want only one members_info per opportunity you could use the rows attribute to only show the first members_info object. It seems you are perhaps trying to model the one-to-one case.






Hello Friends


         I am new to Salesforce ,I have one doubt about writing a visualforce page



                                    Can any one please tell me how to write the relationship fields in visualforce page by using custom controller


            For example,i have two visualforce pages(page1,page2),there are two fields (name,department)in page1 and in page two there are two fields(nameand department) but in page 2 the department value can populated from page1 department value,


so any one please help me