Newer Version Available

This content describes an older version of this product. View Latest

Example Using a Related List

Dynamic Visualforce components are best used when you don’t know the type of object you want to reference, as opposed to dynamic Visualforce bindings, which are best used when you don’t know the fields you want to access.

The following scenario for using dynamic Visualforce constructs a simple, reusable page with a known set of fields you want to access. The page and its custom object are placed into an unmanaged package and distributed throughout the same organization.

First, create a custom object called Classroom. Create two objects—one named Science 101 and another named Math 201, as this figure shows:
Image of Recently Added Classrooms
Next, create two more custom objects called Student and Teacher. After you finish creating each object:
  1. Click New under Custom Fields & Relationships.
  2. Select Master-Detail Relationship, then click Next.
  3. Select Classroom from the drop-down list, then click Next.
  4. Continue to click Next, leaving all the default values intact.
Create the following objects and matching relationships:
  • A new Student named Johnny Walker, and a new Teacher named Mister Pibb, both assigned to Science 101.
  • Another new Student named Boont Amber, and a new Teacher named Doctor Pepper, both assigned to Math 201.
Now, create a new Apex page called DynamicClassroomList and paste the following code:
After trying to save, you may be prompted about a missing Visualforce page. Click the link to create the page: the next blocks of code will populate it.
Create a Visualforce page called dynVFClassroom and paste the following code:
Finally, create a page called DynamicClassroomList. If you’ve been following this tutorial from the beginning, you should have already created this page when constructing your controller extension. Paste in the following code:
This is the page that presents the user with the option of selecting which object relationships to display. Notice that the “selected” and “unselected” lists are populated through dynamic means.

After assembling the controller extension and these pages, navigate to /apex/dynVFClassroom in your organization. You’ll see a sequence similar to the following:Screenshot of initial Classroom object selectionScreenshot of Classroom relationship selectionScreenshot of Classroom relationship selection