Newer Version Available
Dynamic Visualforce Bindings
Dynamic Visualforce bindings are a way of writing generic Visualforce pages that display information about records without necessarily knowing which fields to show. In other words, fields on the page are determined at run time, rather than compile time. This allows a developer to design a single page that renders differently for various audiences, based on their permissions or preferences. Dynamic bindings are useful for Visualforce pages included in managed packages since they allow for the presentation of data specific to each subscriber with very little coding.
1reference[expression]- reference evaluates to either an sObject, an Apex class, or a global variable
- expression evaluates to a string that is the name of a field, or a related object. If a related object is returned, it can be used to recursively select fields or further related objects.
1{!reference[expression]}1{!myContact['Account'][fieldname]}Your dynamic Visualforce pages should be designed to use a standard controller for the object on your page, and implement any further customization through a controller extension.
You can use the Apex Schema.SobjectType methods to get information for your dynamic references, in particular those that access the fields of an object. For example, Schema.SobjectType.Account.fields.getMap() returns a Map of the names of the Account fields in a format that your Apex controllers and extensions can understand.
Defining Relationships
Both reference and expression can be complex expressions, such as those that evaluate to object relationships. For example, suppose that an object called Object1__c has a relationship to another object called Object2__c. The name of the relationship between these two objects is called Relationship__r.
- Object1__c.Object2__c['myField']
- Object1__c['Object2__c.myField']
- Object1__c['Object2__c']['myField']
- Object1__c.Relationship__r[myField]
- Object1__c[Relationship__r.myField]
- Object1__c[Relationship__r][myField]