Dynamic Visualforce Bindings – ooOOOoo

By Vincent on February 18, 2011

This blog post is mainly a summary of what i just read in the SFDC Dynamic VF Bindings section of the documentation for version 21.0. Pretty cool stuff. This will allow programmers to make VF pages even more Administrator friendly (as opposed to be Developer only territory). So Dynamic Visualforce Bindings allow VF pages to be more flexible when it comes to displaying fields of sObjects and even public properties of Custom Apex Classes. Previously when working with <apex:outputField> tags and <apex:column>, you would always have to define the field name somewhere in the VF code to present it on screen. Now there’s syntax that allows you to dynamically get fields at runtime. It’s about time! So the following are example code snippets (borrowed from the documentation). which uses the new features:

Using a List of field names on an Account object

Here “fieldList” is a List<String> which contains ‘Industry’, ‘BillingCity’, ‘AnnualRevenue’.

<apex:page standardController="Account" extensions="DynRefAccFieldLister" >
 <apex:form>
  <apex:pageBlock title="Account Fields" mode="edit">
   <apex:pageBlockSection columns="1">
    <apex:inputField value="{!Account.Name}" />
    <apex:repeat value="{!fieldList}" var="fld" >

     <!-- access the account fields that are a part of the field set! -->
     <apex:inputField value="{!Account[fld]}" />

    </apex:repeat>
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Extending this example a little, we can retrieve the label and value separately

<apex:page standardController="Account" extensions="DynRefAccFieldLister" >
 <apex:form>
  <apex:pageBlock title="Account Fields" mode="edit">
   <apex:pageBlockSection columns="1">
    <apex:inputField value="{!Account.Name}" />
    <apex:repeat value="{!fieldList}" var="fld" >

     <!-- get the label for the field set fields this way -->
     <apex:outputText 
      value="{!$ObjectType.Account.Fields[fld].label}" />
     <apex:inputField value="{!Account[fld]}" />

    </apex:repeat>
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Using a List of indexes against a List of Strings

This example is mostly a copy from the documentation. Controller class contains the following properties:

public List<String> people {
 get { 
  return new List<String>{'Winston', 'Julia', 'Brien'};
 }
 set;
}
public List<Integer> iter{
 get { 
  return new List<Integer>{0, 2, 1};
 }
 set;
}

The following VF code can display the list in the 0, 2, 1 order.

<apex:repeat value="{!iter}" var="pos">
 <apex:outputText value="{!people[pos]}" /><br/>
</apex:repeat>

Using a List of keys against a Map

Similar to the one above, but using a map. Here the map is of type <string, string>, but something like a <string, Account> can also work and you would use it just like you would expect! Sample Controller code:

public Map<String,String> directorsToCountry {
 get { 
  return new Map<String, String>{'Kieslowski' => 'Poland', 
   'del Toro' => 'Mexico', 'Gondry' => 'France'};
  }
  set;
}
public List<String> keys {
 get { 
  return new List<String>{'Kieslowski', 'del Toro', 'Gondry'};
 }
 set;
}

The following VF code can display the map.

<apex:repeat value="{!keys}" var="director">
 <apex:outputText value="{!directorsToCountry [director]}" /><br/>
</apex:repeat>

Using a FieldSet against an Account object

Finally, you can use a FieldSet to define… well… a set of Fields. This FieldSet can then be iterated through in a VF page and then applied to an sObject. Here’s the example from the documentation. Here properNames is a Contact FieldSet which contains fields holding First Name, Middle, Name, Last Name ,and a Job Title field. The following VF could be used to view those fields for a specific record

<apex:page standardController="Contact">
 <apex:repeat value="{!$ObjectType.Contact.FieldSets.properNames}" 
  var="f">
  
  {!$ObjectType.Contact.Fields[f].label} 
  ({!$ObjectType.Contact.Fields[f].type}): 
  <apex:outputField value="{!Contact[f]}"/>
  <br/>
 </apex:repeat>
</apex:page>

Cool! This makes me want to create FieldSets for every ding-dang custom list i’ve ever had to write.

Force.com Development