Newer Version Available
Controller Methods
- Action
- Getter
- Setter
Action Methods
- <apex:commandButton> creates a button that calls an action
- <apex:commandLink> creates a link that calls an action
- <apex:actionPoller> periodically calls an action
- <apex:actionSupport> makes an event (such as “onclick”, “onmouseover”, and so on) on another, named component, call an action
- <apex:actionFunction> defines a new JavaScript function that calls an action
- <apex:page> calls an action when the page is loaded
For example, in the sample page in Building a Custom Controller, the controller's save method is called by the action parameter of the <apex:commandButton> tag. Other examples of action methods are discussed in Defining Action Methods.
Getter Methods
Getter methods return values from a controller. Every value that is calculated by a controller and displayed in a page must have a corresponding getter method, including any Boolean variables. For example, in the sample page in Building a Custom Controller, the controller includes a getAccount method. This method allows the page markup to reference the account member variable in the controller class with {! } notation. The value parameter of the <apex:inputField> tag uses this notation to access the account, and dot notation to display the account's name. Getter methods must always be named getVariable.
Setter Methods
Setter methods pass user-specified values from page markup to a controller. Any setter methods in a controller are automatically executed before any action methods.
For example, the following markup displays a page that implements basic search functionality for Leads. The associated controller includes getter and setter methods for the search box input, and then uses the search text to issue a SOSL query when the user clicks Go!. Although the markup doesn’t explicitly call the search text setter method, it executes before the doSearch action method when a user clicks the command button:
The following class is the controller for the page markup above:
While a getter method is always required to access values from a controller, it’s not always necessary to include a setter method to pass values into a controller. If a Visualforce component is bound to an sObject that is stored in a controller, the sObject's fields are automatically set if changed by the user, as long as the sObject is saved or updated by a corresponding action method. An example of this behavior is shown in the sample page in Building a Custom Controller.
Setter methods must always be named setVariable.
Getting and Setting Data with a Custom Extension or Controller
There is no guaranteed order in which Apex methods and variables are processed by a controller extension or custom controller. Therefore, do not allow controller and extension classes to rely on another method being run, call that method directly. This applies specifically to setting variables and accessing data from the database.
For example, in the following custom controller, the first method, getContactMethod1, always returns the correct value because it doesn’t assume that the contact variable c already exists. The second method, getContactMethod2, however, sometimes returns the correct value, but not every time if c hasn’t yet been set.
The following custom controller has the exact same methods. However, getContactMethod2 calls contactMethod1, so the variable c is always set, and always contains the correct value when returned.
The following markup shows two pages that call these controllers. The Visualforce markup is identical, only the controller name is changed: