Newer Version Available
Reset Data Stored by Custom Controllers
Visualforce automatically updates data in standard controllers. Use the action attribute with your own method to reset all local state. It ensures that the data contained in custom controllers and extensions refreshes.
Let's look at an example. Here, on the Visualforce page, live controller refreshes the value of the outputText component. When the expression {! fancyText} updates, the text "Hello" followed by the latest account name displays. The Visualforce page's standard controller tracks the account record. The custom extension LiveExtension tracks the account field acct on the account record across page refreshes.
For this example, we've created a controller extension for a standard controller to show how to manually reset data. Note that you do not need to reset data for standard controllers. They have built-in reset functionality.
1<apex:page standardController="Account" extensions="LiveExtension">
2 <apex:liveController action="{! resetMe}" />
3 <apex:outputText value="{! fancyText}" />
4</apex:page>1public class LiveExtension {
2 Private Account acct;
3 Private ApexPages.StandardController ctrl;
4
5 public LiveExtension(ApexPages.StandardController ctrl) {
6 ctrl.addFields(new List<String>{'Name'});
7 this.ctrl = ctrl;
8 this.acct = (Account)ctrl.getRecord();
9 }
10 public void resetMe() {
11 acct = (Account)ctrl.getRecord();
12 }
13 public String getFancyText() {
14 return 'Hello ' + acct.name + ' (' + acct.id + ')';
15 }LiveExtension has two private fields, the account record, acct and the standard controller, ctrl. The constructor instantiates both fields and callsgetRecord() so that when the Visualforce page loads, the acct field contains the most recent data. The resetMe() function calls getRecord() again. When live controller detects a change, the page updates with the newest data since we've set the action attribute to resetMe(). Finally,getFancyText() returns the name and ID fields on LiveExtension's acct field. The Visualforce page expression {! fancyText} calls getFancyText() every time the page loads.