Using Relationships in Visualforce Pages
December 16th, 2009
The topic of relationships in Visualforce pages came up on the Salesforce developer discussion boards the other day so I thought I’d throw something together to expand on the topic.
Let’s say you want to print off a Sales Order or Purchase Order as a PDF. You can use the parent and child relationships inherent in Salesforce to display this information using the standard Apex Controller. (It’s amazing some of the things that you can create in Visualforce without the use of a custom controller or controller extension!)
You can run this example on my Developer Site. It will open a PDF so watch for any download.
Originally I was going to display an Opportunity and its LineItems but they are not accessible in Sites so I opted to display an Account and its Contacts. The renderAs attribute specifies that the page is to be generated as a PDF. The account names traverse the parent relationships to display the various names associated with them. We then display all off the associate contacts by iterating over the collection (account.contacts <– notice the “s”).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <apex:page standardController="Account" showHeader="false" renderAs="pdf"> <img src="{!URLFOR($Resource.AppirioLogo)}" border="0"/><p/> Name: {!account.name}<br/> Regional Parent: {!account.parent.name}<br/> Corp Parent: {!account.parent.parent.name}<p/> <table width="100%" cellpadding="2" cellspacing="2"> <tr> <td><b>First</b></td> <td><b>Last</b></td> <td><b>Email</b></td> </tr> <apex:repeat value="{!account.contacts}" var="contact"> <tr> <td>{!contact.firstname}</td> <td>{!contact.lastname}</td> <td>{!contact.email}</td> </tr> </apex:repeat> </table> </apex:page> |
Related posts:
- Redirecting Users to Different Visualforce Pages
- Overriding Standard Links with Visualforce Pages
- Using Related Lists in Visualforce Pages
- Use an Inline Visualforce Page with Standard Page Layouts
- Visualforce Export to Excel / IE Bug
Categories: Salesforce, Visualforce




Simple things should be easy, and it’s nice when they actually are easy. When you say “without a controller”, I think you mean without a custom controller or extension. The standard controller is doing the work in this case.
You are absolutely correct! I’ll make that change. Thanks!
Excellent! I was just getting ready to tackle something similar, and figured I would need a custom controller. Thanks for a useful post.