Sunday, July 19, 2015

DYNAMIC VISUALFORCE BINDINGS

Today in this post, I will explain about "Dynamic VisualForce Bindings". This was introduced in Spring'11 release and it allows developer to choose the list of fields to be displayed at run time, rather than at compile time. As a result, developer can write generic VisualForce pages which will display information about records without necessarily knowing the list of fields.

Dynamic VisualForce Binding is supported for both standard and custom objects. The general form is
reference[expression]
where -
  • reference - It can be either an sObject, or an Apex Class, or a global variable.
  • expression - It evaluates to a String that is the name of a field, or a related object. In case of related object, it can be used recursively to select another related object or field.
Important point - Dynamic VisualForce Page should always use a standard controller for the object and then implement any further customization through controller extensions.

The below example will show how dynamic visualforce binding can be implemented to display list of fields from Account object at run time.

Controller - ShowDynamicAccountFieldController

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public with sharing class ShowDynamicAccountFieldController {
 public ShowDynamicAccountFieldController(ApexPages.StandardController controller){
  controller.addFields(dynamicFields);
  Account acc = (Account) controller.getRecord();
 }
 
 public List<String> dynamicFields{
  get{
   if(dynamicFields == null){
    dynamicFields = new List<String>();
    dynamicFields.add('Name');
    dynamicFields.add('AccountSource');
    dynamicFields.add('NumberOfEmployees');
    dynamicFields.add('Rating');
   }
   return dynamicFields;
  }
  private set;
 }
 
 public void showFirstSet(){
  checkAndInitiate();
  dynamicFields.clear();
  dynamicFields.add('Name');
  dynamicFields.add('AccountSource');
 }
 
 public void showSecondSet(){
  checkAndInitiate();
  dynamicFields.clear();
  dynamicFields.add('Name');
  dynamicFields.add('Rating');
 }
 
 public void doReset(){
  checkAndInitiate();
  dynamicFields.clear();
  dynamicFields.add('Name');
  dynamicFields.add('AccountSource');
  dynamicFields.add('NumberOfEmployees');
  dynamicFields.add('Rating');
 }
 
 private void checkAndInitiate(){
  if(dynamicFields == null){
   dynamicFields = new List<String>();
  }
 }
}

VisualForce Page - ShowDynamicAccountFieldPage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<apex:page standardController="Account" extensions="ShowDynamicAccountFieldController">
 <apex:form >
  <apex:pageBlock title="Control Room">
   <apex:pageBlockSection columns="3">
    <apex:commandButton action="{!showFirstSet}" value="First Button" id="firstButton"/>
    <apex:commandButton action="{!showSecondSet}" value="Second Button" id="secondButton"/>
    <apex:commandButton action="{!doReset}" value="Reset" id="resetButton"/>
   </apex:pageBlockSection>
  </apex:pageBlock>
  <apex:pageBlock title="Display Account" mode="edit">
   <apex:pageBlockSection columns="2">
    <apex:repeat value="{!dynamicFields}" var="f">
     <apex:outputField value="{!Account[f]}"/>
    </apex:repeat>
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Let me explain what is happening here -

  • Controller "ShowDynamicAccountFieldController" creates a list of strings where each string maps to a field from the Account standard object.
  • The visualforce page uses an tag to loop through the strings.
  • Clicking on "First Button", method showFirstSet() is getting called which will select the list of account fields at run-time and the same set of fields are getting displayed in the visualforce page.
  • Clicking on "Second Button", method showSecondSet() is getting called which will select the list of account fields at run-time and the same set of fields are getting displayed in the visualforce page.
  • Clicking on "Reset", method doReset() is getting called which will select the list of account fields at run-time and the same set of fields are getting displayed in the visualforce page.
Below are screenshots of the Visualforce page -

Initial Load of the page -


After clicking on "First Button" -














After clicking on "Second Button" -

















After clicking on "Reset" button -














As you can see the list of fields are getting decided at run time, so this feature - Dynamic VisualForce Bindings is very powerful. 

I will write few more examples in my up coming posts to explain more about it's features. Till then enjoy learning. Please provide your comments below. Thanks.

0 comments:

Post a Comment