Newer Version Available
Using Dynamic References with Custom Objects and Packages
Package developers can use dynamic Visualforce binding to list only the
fields a user can access. This situation might occur when you’re developing a managed
package with a Visualforce page that
displays fields on an object. Since the package developer doesn’t know which fields a
subscriber can access, they can define a dynamic page that renders differently for each
subscriber.
The following example uses a custom object packaged with a page layout using a Visualforce page to demonstrate how different subscribing users view the same page.
- Create a custom object Book (API name Book__c) with the following fields and data types:
- Title: Text(255)
- Author: Text(255)
- ISBN: Text(20)
- Price: Currency(5, 2)
- Publisher: Text(255)
- Edit the Book page layout so it displays the custom fields first, and removes a few of the standard fields such as Created By, Last Modified By, Owner, and Name.
- Create a new custom object tab. Set the object to Book, and the tab style to Books.
- Switch to the Book tab and create a few Book objects. The values don’t matter, but you do need a few records to actually exist.
- Create a controller extension called BookExtension with the following
code:
- Create a Visualforce page
called booksView that uses the controller
extension to show the values of the Book
object:
- Since the controller extension is going to be packaged, you’ll need to create a test for the Apex class.
Create an Apex class called BookExtensionTest with this basic code to get you
started:
- Create a package called bookBundle, and add the custom object, the Visualforce page, and the bookExtensionTest Apex class. Other referenced elements, such as the page’s controller extension Apex class, are included automatically.
- Install the bookBundle package into a subscriber organization.
- After the package is installed, from the object management settings for books, add a new field called Rating.
- Create a new Book object. Again, the values for the record don’t actually matter.
- Navigate to the booksView page with the package namespace and book ID appended to the URL. For example, if GBOOK is the namespace, and a00D0000008e7t4 is the book ID, the resulting URL should be https://MyDomain_login_URL/apex/GBOOK__booksView?id=a00D0000008e7t4.
When the page is viewed from the subscribing organization, it should include all the packaged Book fields, plus the newly created Rating field. Different users and organizations can continue to add whatever fields they want, and the dynamic Visualforce page will adapt and show as appropriate.