I am working through the Visualforce Developers Guide and during the section on Custom Controllers, an example performs a search stating that it also executes the setter but does not explain why. Would appreciate if someone can help me understand the rule behind this. Here is the section describing and the VF & Controller code from the VF Developers Guide:

"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:

<apex:page controller="theController">
    <apex:pageBlock mode="edit" id="block">
          <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
              <apex:inputText id="searchText" value="{!searchText}"/>
                <apex:commandButton value="Go!" action="{!doSearch}"  <!-- search here -->
                   rerender="block" status="status"/>
      <apex:actionStatus id="status" startText="requesting..."/>
      <apex:pageBlockSection title="Results" id="results" columns="1">
        <apex:pageBlockTable value="{!results}" var="l" rendered="
          <apex:column value="{!l.name}"/>
          <apex:column value="{!l.email}"/>
          <apex:column value="{!l.phone}"/>

The following class is the controller for the page markup above:

public class theController {
  String searchText;
  List<Lead> results;

public String getSearchText() {
  return searchText;

public void setSearchText(String s) {  // dosearch calls setter here
  searchText = s;

public List<Lead> getResults() {
  return results;

public PageReference doSearch() {
  results = (List<Lead>)[FIND :searchText RETURNING Lead(Name, Email, Phone)][0];
  return null;
  • sorry that stackexchange removed nice formatting of my code .... (think I fixed the formatting) Commented May 11, 2014 at 1:20

1 Answer 1


It's doing exactly what it says: it's setting the value through the use of the setter and getter methods. See the documentation on the Visualforce life cycle, but in summary, Visualforce basically does this:

  • Deserializes any postback information (the view state)
  • Calls and constructors and static methods
  • Calls any setters that are bound to rendered elements (which may be hidden, but still present)
  • Performs the action for the transaction
  • Calls any getters that are bound to rendered elements
  • Generates the view state
  • Outputs the page

In your example code, that means that clicking the search button does this:

  • Deserializes the view state
  • Calls setSearchText
  • Calls doSearch
  • Calls getSearchText and getResults (in an indeterminate order)
  • Generates the view state
  • Renders the search results

The reason why setters are called with each transaction is so that any business logic associated with the setter can be performed (e.g. normalizing or validating data, etc). While this page could have been written with so-called "automatic getters and setters", this code is meant to illustrate that more complex logic can also be incorporated, as well as the fact that "setSearchText" and "getSearchText" are called without the programmer explicitly calling those functions-- nowhere in your page markup do you have {!setSearchText}, for example, because it is automatically bound by the Visualforce runtime engine.

  • Excellent explanation and also helped clarify the section on Visualforce lifecycle which came later. Commented May 11, 2014 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .