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
Tracy Oden 31Tracy Oden 31 

Visualforce: Help displaying field data from two objects using a single standard controller where repeat data is used from one of the objects

Hopefully someone can help. I am new to VF.  I desperately need someone to write a code example for me.

I am creating a resume page that renders as a PDF. I am running itnot a few problems,

I have two custom objects for which the VF page will need to display data.
  • Object A contains the name of a single practitioner (with some additional personal details).
  • Object B contains all the technical details of a single practitioner on a single engagement.
  • A lookkup field is on Object B relating it to Object A.
  • There are many Object Bs but only one Object A.
I want to use the stanard controller on the VF page for custom Object A.

1) I need to know how to reference fields from Object B into the page. An example would be great.
2) Since there are many Object B records containing skills for a praticiular practitioner;  I also need to know how to refenerence each skill from each Object B into the single VF page. (an example would be great)!

Thanks in advance for anyone who can help!
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Let us say you have queried on Object B and fetched records related to Object A in a list (lstObjectBs). Now, to display these records on your page you can use <apex:pageBlockTable> and to access fields from ObjectA you can write ObjectA__r.Name. This will give you the Name of the Object A record.
<apex:page standardController="ObjectA" extensions="YourController">

    <apex:pageBlock title="My Content">

        <apex:pageBlockTable value="{!lstObjectBs}" var="item">

            <apex:column value="{!item.ObjectA__r.Name}"/> 

        </apex:pageBlockTable> 

    </apex:pageBlock> 

</apex:page>

 
Tracy Oden 31Tracy Oden 31
Thank you. I will try this. I’ll let you know how it goes.
Tracy Oden 31Tracy Oden 31
Thank you so much for your help. I am very new at this. I have customized the code you sent for the VF page; now I need help with sample code for the Controller Extension. Please help again. This should do it.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
I am assuming you are passing Object A;s Id in the url
public class MyController{

    public List<ObjectB> lstObjectBs{get;set;}
    public Id ObjectAId;
    public MyController(ApexPages.StandardController con){
		ObjectAId = System.currentPageReference().getParameters().get('Id');
        lstObjectBs = new List<ObjectB>([Select Id, ObjectA__r.Name from ObjectB
                    Where ObjectA__C=:ObjectAId]);    
    }
}

Please mark it Best Answer if it works for you.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Also, if it is a complete vf page then use this to get the Id from the url:
​ApexPages.currentPage().getParameters().get('Id');
Tracy Oden 31Tracy Oden 31
Thanks a million. I will try this. I truly appreciate your help!!!!
Tracy Oden 31Tracy Oden 31
I have one more issue with this. Your code is superb. The issue is I don't want to display the data in a List. I want to use the data returned from Object B as single fields in the VF page. I want to literally design a resume using these fields. The code above renders a list.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Then create a html table inside <apex:pageBlockTable>. Something like this
<apex:page standardController="ObjectA" extensions="YourController">

    <apex:pageBlock title="My Content">

        <apex:pageBlockTable value="{!lstObjectBs}" var="item">

            <tr>
              <td value = "Field Name"/>
              <td value = "{!item.ObjectA__r.Name}"/>
            </tr>

        </apex:pageBlockTable> 

    </apex:pageBlock> 

</apex:page>

If you don't want html then you can try using <apex:pageBlockSectionItem>