Saturday, 4 June 2011

Dynamic Binding : Using Field Sets


Dynamic binding has been an amazing feture released in Spring 11. Earlier in visualforce page means that the apex controls that used to be binded like {!sObjectAPIName.FieldAPIName} , where you have to provide object API Name and field API Name at compile time ( at the time you develope a page and save it), it was static binding. Now we can provide both of these at run time thats why it is dynamic binding.

Variations of Dynamic Binding :

1) Dynamic Field API Name : It can be sub divided in two parts
    A) Using Field Sets
    B) Without Using Field Sets where Field to be displayed are maintained in any List in apex class, we can save these list using any custom object in which we will save both object API Name and Field API Name.

2) Dynamic Object API Name and Field API Name both

We will start here by first

1) Dynamic Field API Name Using Field Set :

First we will create a field set on Account Object : Go to -> Customize -> Account -> Field Sets and create a new field set.

Here you will see three boxes for field selection
1)Account(This is Object Name) Object Fields : This box is only available on developer org. This is not visible on production org after managed packaged is installed there.
2)Available For the Field Set : These are the fields that are available to move to the "In the Field Set". This is available on production org after managed packaged is installed there.
3) In the Field Set : These are the fields that are actually rendered on Visualforce Page when Field Set is used.

Now I will add some fields see the below image

Now Save this field set.

Note : In summer 11 release layout for field set editor has been changed and made like page layout in the object. See this for more

Using field Set on Visulaforce Page

<apex:page standardController="Account" title="Field Set Use">
       <apex:pageBlock title="Field Set Use">
       <apex:pageBlockSection title="Field Set Use">
           <apex:repeat value="{!$ObjectType.Account.FieldSets.DemoFieldSet}" var="fieldAPIName">
               <apex:inputField value="{!Account[fieldAPIName]}"/>
In above example we used Account as standard controller, and used it for Field binding in Input Field. We can also use any account instance of account object from controller class also if we want. All fields of field set renders in order you save them in "In the Field Set" column. If any field is Hidden for any profile then field set will not render that field for that user.

You can also show field set of any related object which has a reference to parent object using relationship name.

Often it is asked whether we can create new fields and fields in the field set on production orgs. Answer to this is "Yes". As soon you create any field on the object on production org it comes in "Available Fileds" box and you can add them in the field set and render them on your VFP.

Related Post :


  1. sharma......thanks for giving good examples...


  2. This is good, but how about you have input and read only fields, and you want admins to have control of both this fields on VF page. Is there a way without creating multiple field sets?

  3. This is good,Actually I created objects and these objects related fields using metadata api in an visualforce page.But my requirement is how to retrieve  created fields(Using visualforce page ) in object Dynamically using visualforce page and apex class.Please help me

  4. Hi Sreekanth,
    Your displayed all fields dynamically in an visualforce page.I have one field in that fields as multiselect picklist.How to display multiselect field vales as Checkboxes Dynamically.

    please help me.............