DYNAMIC VISUALFORCE BINDINGS

by Sudipta Deb | Jul 19, 2015 | Apex, dev 501, dynamic, generic, visualforce | 1 comment

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.

Written by

Related Posts

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

For years, Salesforce developers have faced a common challenge: programmatically retrieving picklist values that are specific to a certain Record Type directly within Apex. The “solutions” often involved either complex SOQL queries against metadata, relying on the UI API (with its associated callout limits and serialization overhead), or maintaining clunky custom metadata/settings.

Good news, Technical Potpourri readers! The Salesforce Spring ’26 release is bringing a game-changer that will significantly simplify your Apex code and improve performance: native Apex methods to filter picklist values by Record Type!

read more...
Apex Cursor and Its Advantages Over Batch Processing

Apex Cursor and Its Advantages Over Batch Processing

Struggling with large datasets and SOQL governor limits in Salesforce? In this video, I will dive deep into Apex Cursors, the powerful new feature that revolutionizes how we process millions of records.

Apex Cursors allow developers to navigate through query results in manageable chunks within a single transaction or across Queueable jobs. In this video, I will show you the correct way to implement Apex Cursor and also when this is an exception, how to handle that and perform retry.

read more...
DeepLearning.AI’s 16 AI-Agent Courses — What You Should Know & How to Navigate Them

DeepLearning.AI’s 16 AI-Agent Courses — What You Should Know & How to Navigate Them

AI agents — software entities that can perceive environments, plan actions, and autonomously execute tasks — are a rising frontier in artificial intelligence. DeepLearning.AI has apparently launched (or announced) a series of 16 AI-agent courses, likely aiming to train practitioners in building, deploying, and scaling agents that can learn, reason, and interact.

read more...

1 Comment

  1. Unknown

    Nice blog however, please advise what if list of account records need to be displayed in a data table, what would be the optimal code that you would follow using dynamic variable binding in VF page?

    Reply

Leave a Reply