Using Relationships in Visualforce Pages

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").

<apex:page standardController="Account" showHeader="false" renderAs="pdf">

 <img src="{!URLFOR($Resource.AppirioLogo)}" border="0"/><p/>

 Name: {!}<br/>
 Regional Parent: {!}<br/>
 Corp Parent: {!}<p/>

  <table width="100%" cellpadding="2" cellspacing="2">
  <apex:repeat value="{!account.contacts}" var="contact">