---
schema_version: "1"
id: d1f31b3bd503d19d6e405d9c11af6c835024f454fceb83ac0ab7f598421921f1
title: Using Dynamic References with Custom Objects and Packages
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/dynamic-vf/pages-dynamic-vf-sample-custom.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-dynamic-vf-sample-custom.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 36c6c9f455b482177afb73eeba068bec91a3424c972d31b76ba056ceef690942
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# 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.

1.  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)
2.  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.
3.  Create a new custom object tab. Set the object to Book, and the tab style to Books.
4.  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.
5.  Create a controller extension called `BookExtension` with the following code:

    <!-- otherprops: set_clipboard controller extension=bookExtension -->
    ```apex_code
    public with sharing class BookExtension {
    
        private ApexPages.StandardController stdController;
        
        public BookExtension (ApexPages.StandardController ct) {
            this.stdController = ct;
            if( ! Test.isRunningTest()) {
                // You can't call addFields() in a test context, it's a bug
                stdController.addFields(accessibleFields);
            }
        }
        
        public List<String> accessibleFields {
            get {
                if (accessibleFields == null) {
                    // Get a list (map) of all fields on the object
                    Map<String, Schema.SobjectField> fields = 
                        Schema.SobjectType.Book__c.fields.getMap();
            
                    // Save only the fields accessible by the current user 
                    Set<String> availableFieldsSet = new Set<String>();
                    for (String s : fields.keySet()) {
                        if (fields.get(s).getDescribe().isAccessible() 
                            // Comment out next line to show standard/system fields
                            && fields.get(s).getDescribe().isCustom()
                        ){
                                availableFieldsSet.add(s.toLowerCase());
                                if(Test.isRunningTest()) System.debug('Field: ' + s);
                        }
                    }
                    
                    // Convert set to list, save to property
                    accessibleFields = new List<String>(availableFieldsSet);
                }
                return accessibleFields;
            }
            private set;
        }
    }
    ```

6.  Create a Visualforce page called `booksView` that uses the controller extension to show the values of the Book object:

    <!-- otherprops: set_clipboard markup -->
    ```apex_page
    <apex:page standardController="Book__c" extensions="BookExtension" > 
    
        <apex:pageBlock title="{!Book__c.Name}">
            <apex:pageBlockSection >
        
            <apex:repeat value="{!accessibleFields}" var="f">
                <apex:pageBlockSectionItem >
                
                    <apex:outputLabel value="{!$ObjectType['Book__c'].Fields[f].Label}"/>
                    <apex:outputText value="{!Book__c[f]}"/>
                        
                </apex:pageBlockSectionItem>
            </apex:repeat>
            
            </apex:pageBlockSection>
        </apex:pageBlock>
    
    </apex:page>
    ```

7.  Since the controller extension is going to be packaged, you’ll need to [create a test for the Apex class](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm). Create an Apex class called `BookExtensionTest` with this basic code to get you started:

    <!-- otherprops: set_clipboard -->
    ```apex_code
    @isTest
    public class BookExtensionTest {
        
        public static testMethod void testBookExtension() {
    
            // Create a book to test with
            Book__c book = new Book__c();
            book.Author__c = 'Harry Lime';
            insert book;
            
            Test.startTest();
            
            // Add the page to the test context
            PageReference testPage = Page.booksView;
            testPage.getParameters().put('id', String.valueOf(book.Id));
            Test.setCurrentPage(testPage);
            
            // Create a controller for the book
            ApexPages.StandardController sc = new ApexPages.StandardController(book);
            
            // Real start of testing BookExtension
            // BookExtension has only two methods; to get 100% code coverage, we need
            // to call the constructor and get the accessibleFields property
            
            // Create an extension with the controller
            BookExtension bookExt = new BookExtension(sc);
            
            // Get the list of accessible fields from the extension
            Set<String> fields = new Set<String>(bookExt.accessibleFields);
            
            // Test that accessibleFields is not empty
            System.assert( ! fields.isEmpty());
            
            // Test that accessibleFields includes Author__c
            // This is a bad test; you can't know that subscriber won't disable
            System.assert(fields.contains('Author__c'.toLowerCase()), 
                'Expected accessibleFields to include Author__c');
            
            Test.stopTest();
            
        }
        
    }
    ```

    **note:** This Apex test is only meant to be a sample. When creating tests that are included into packages, validate all behavior, including positive and negative results.

8.  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.
9.  Install the `bookBundle` package into a subscriber organization.
10. After the package is installed, from the object management settings for books, add a new field called `Rating`.
11. Create a new Book object. Again, the values for the record don’t actually matter.
12. 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.

**See Also**  


-   [*Salesforce Help*: Find Object Management Settings](https://help.salesforce.com/HTViewHelpDoc?id=extend_click_find_objectmgmt_parent.htm)

