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:
1public with sharing class BookExtension { 2 3 private ApexPages.StandardController stdController; 4 5 public BookExtension (ApexPages.StandardController ct) { 6 this.stdController = ct; 7 if( ! Test.isRunningTest()) { 8 // You can't call addFields() in a test context, it's a bug 9 stdController.addFields(accessibleFields); 10 } 11 } 12 13 public List<String> accessibleFields { 14 get { 15 if (accessibleFields == null) { 16 // Get a list (map) of all fields on the object 17 Map<String, Schema.SobjectField> fields = 18 Schema.SobjectType.Book__c.fields.getMap(); 19 20 // Save only the fields accessible by the current user 21 Set<String> availableFieldsSet = new Set<String>(); 22 for (String s : fields.keySet()) { 23 if (fields.get(s).getDescribe().isAccessible() 24 // Comment out next line to show standard/system fields 25 && fields.get(s).getDescribe().isCustom() 26 ){ 27 availableFieldsSet.add(s.toLowerCase()); 28 if(Test.isRunningTest()) System.debug('Field: ' + s); 29 } 30 } 31 32 // Convert set to list, save to property 33 accessibleFields = new List<String>(availableFieldsSet); 34 } 35 return accessibleFields; 36 } 37 private set; 38 } 39} - Create a Visualforce page
called booksView that uses the controller
extension to show the values of the Book
object:
1<apex:page standardController="Book__c" extensions="BookExtension" > 2 3 <apex:pageBlock title="{!Book__c.Name}"> 4 <apex:pageBlockSection > 5 6 <apex:repeat value="{!accessibleFields}" var="f"> 7 <apex:pageBlockSectionItem > 8 9 <apex:outputLabel value="{!$ObjectType['Book__c'].Fields[f].Label}"/> 10 <apex:outputText value="{!Book__c[f]}"/> 11 12 </apex:pageBlockSectionItem> 13 </apex:repeat> 14 15 </apex:pageBlockSection> 16 </apex:pageBlock> 17 18</apex:page> - 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:
1@isTest 2public class BookExtensionTest { 3 4 public static testMethod void testBookExtension() { 5 6 // Create a book to test with 7 Book__c book = new Book__c(); 8 book.Author__c = 'Harry Lime'; 9 insert book; 10 11 Test.startTest(); 12 13 // Add the page to the test context 14 PageReference testPage = Page.booksView; 15 testPage.getParameters().put('id', String.valueOf(book.Id)); 16 Test.setCurrentPage(testPage); 17 18 // Create a controller for the book 19 ApexPages.StandardController sc = new ApexPages.StandardController(book); 20 21 // Real start of testing BookExtension 22 // BookExtension has only two methods; to get 100% code coverage, we need 23 // to call the constructor and get the accessibleFields property 24 25 // Create an extension with the controller 26 BookExtension bookExt = new BookExtension(sc); 27 28 // Get the list of accessible fields from the extension 29 Set<String> fields = new Set<String>(bookExt.accessibleFields); 30 31 // Test that accessibleFields is not empty 32 System.assert( ! fields.isEmpty()); 33 34 // Test that accessibleFields includes Author__c 35 // This is a bad test; you can't know that subscriber won't disable 36 System.assert(fields.contains('Author__c'.toLowerCase()), 37 'Expected accessibleFields to include Author__c'); 38 39 Test.stopTest(); 40 41 } 42 43} - 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.