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.

Dynamic Visualforce binding is supported for standard and custom objects. Dynamic bindings take the following general form:
reference[expression]
where
  • 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.
Dynamic bindings can be used anywhere formula expressions are valid. Use them on a page like this:
{!reference[expression]}
Optionally, you can add a fieldname to the end of the whole dynamic expression. If the dynamic expression resolves to an sObject, the fieldname refers to a specific field on that object. If your reference is an Apex class, the field must be public or global. For example:
{!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.

Important

Important

Static references are checked for validity when you save a page, and an invalid reference will prevent you from saving it. Dynamic references, by their nature, can only be checked at run time, and if your page contains a dynamic reference that is invalid when the page is viewed, the page fails. It’s possible to create references to custom fields or global variables which are valid, but if that field or global value is later deleted, the page will fail when it is next viewed.

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.

If Object2__c has a field called myField, then the following dynamically-cast lookups all return a reference to the same field:
  • 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]
Previous
Next