+ Start a Discussion
Tin013Tin013 

Retrieving data from the custom object

Hi all,

 

Hope you all are well.

 

I have a custom object called Members with master details relationship linked to Opportunity object. How could I create a query so that it displays all the Members of a current object on the visual force page?

 

Sorry, I know it is probably a lame question but quite like to know how it is done.

 

Thanks,

I know how to create a class and visual force page but don't know who to create a query for above propose.

pmozz01pmozz01

Not sure I can fully explain this here, but in your Apex code you need to create a list of those Members, something like:

 

public LIST<LineItem> myItems {
  GET {
  return newItems.values();

 

and then in your VF page you call the list: of Members, then go from there with the column headers if needed:

 

<apex:pageBlockTable value="{!myItems}" var="ni" columnClasses="left, center" >

 

 

Kevin SwiggumKevin Swiggum

If your using Opportunity as your standardController on your VF page, then no query needed. You have direct access to the child records via the relationship variable. See the example below.

<apex:page standardController="Opportunity">

<apex:pageBlock>
     <apex:pageBlockSection>
          <apex:outputField value="{!Opportunity.name}"/>
     </apex:pageBlockSection>

     <apex:pageBlockTable value="{!Opportunity.Members__r}" var="mbr">
          <apex:column value="{!mbr.name}"/>
     </apex:pageBlockTable>

</apex:pageBlock>

</apex:page>

The example gets to your member records (linked to the opportunity) via the Opportunity.members__r (I'm assuming the name of the relationship variable "members__r". You may need to look at your master-detail relationship definition to determine what the name really is...but it will end with __r.

 

The downside of doing it this way is you can't filter (e.g., show only the ACTIVE members vs ALL members on the opportunity) and you can't control sort order. But it's the easiest way to get at those child records. If you need more advaned access (filter or sort) then you'll need a Controller Extension and a SOQL query.

 

Hope that helps

--Kevin