Given a scenario, display content or modify Salesforce data using a Visualforce page and the appropriate controllers or extensions as needed.

After studying this topic, you should be able to:

  • Identify the different types of data that can be displayed in a Visualforce page
  • Describe how these data can be displayed on a Visualforce page
  • Determine what standard controllers, custom controllers and controller extensions are
  • Describe what lists set controllers and standard set controllers are
  • Identify use cases for the different Visualforce controller types to meet requirements

Table of Contents

  • Visualforce Content and Data
  • Visualforce Controllers
  • Standard Controllers
  • Standard List Controllers
  • Standard Set Controllers
  • Custom Controllers
  • Controller Extensions
  • When to Use Which Controller
  • Getter Methods
  • Setter Methods
  • Getter and Setter Properties
  • Action Methods
  • Other Considerations
  • Scenarios and Solutions

Introduction

  • When standard fucntionality is not sufficient to display data in a Salesforce instance, Visualforce pages can be used to create custom user interface elements that can retrieve data from objects and other resources in Salesforce
  • Visualforce is a user interface development framework that enables replacement and extension of Salesforce’s built-in features for accessing data, as well as creation of custom user interfaces with data from different sources
  • Types of Web Content in Visualforce
    • HTML: HTML tags are generated automatically by Visualforce, and generated HTML tags can be overridden. Inline CSS can be defined for HMTL tags
    • CSS: added using <apex:stylesheet>. Uploaded and referenced as static resources. Stylesheet is referenced using $Resource variable. styleClass attribute defines the name of the style class
    • JavaScript: can be added using <apex:includescript>. Uploaded as a static resource. <script> tags can be used to access functions. $Resource variable is used to access the file.
    • <apex:image> can display an image, either from static resource or external URL
    • <apex:map> can show a map
    • <apex:iframe> can display an external website
  • Types of Displayed Data in Visualforce
    • Object Data: displayed using expression syntax and components like <apex:outputField> and <apex:detail>
    • Related Data: merge field syntax is used to display related object records up to five levels of child-parent and one level of parent-child relationships away
    • Global Data: can be used to retrieve general info about the system and organization
    • User Data: global variables for the current user
    • Static Data: available from the global variable $Resource
  • Visualforce Controllers Overview
    • Standard Controllers: invoked using the standardController attribute of the <apex:page> component
    • Standard List Controllers: invoked using standardController and recordSetVar attributes of the <apex:page> component
    • Standard Set Controllers: used to create a custom list controller, invoked using the controller attribute of the <apex:page> component
    • Custom Controllers: used when completely new functionality is needed, invoked using the controller attribute of the <apex:page> component
    • Controller Extensions: used to extend or override functionality, invoked using the extensions attribute of the <apex:page> component

Visualforce Pages

  • Visualforce pages use tag-based markup language and provide standard and custom functionality for accessing, displaying and updating data in an org
  • Visualforce Expression Syntax: In Visualforce pages, all content encapsulated in {! and } characters are evaluated as expressions. Dot notation is used to access fields or traverse related objects.
    • <apex:inputFieldvalue="{! Opportunity.Name }" />
    • <apex:inputFieldvalue="{! Opportunity.Account.Name }" />
  • Visualforce and HTML markup can be used together in a Visualforce page. Only required tag in a Visualforce page is the <apex:page> tag.
<apex:page controller="MyPageController">
    <h3>{!account.Name}</h3>
    <apex:pageBlock>
        <!-- Operators and Functions can be used in VisualForce Expressions -->
        <apex:pageblockTable rendered="{!account.cases.size > 0}" value="{!account.cases}" var="case">
            <apex:column>
                <!-- Output Field should automatically display a Checkbox on the page -->
                <apex:outputField value="{!case.IsEscalated}"/>
            </apex:column>
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>
  • To access fields from a record, Visualforce pages need to be associated with a controller. A custom controller below identifies a specific record to access based on a URL parameter.
    • An account record on the Visualforce page is set based on an “acctId” parameter in the URL
public class MyPageController {
    public Account acct;
    public Account getAccount() {
        return acct;   
    }
    public MyPageController() {
        Id accountId = ApexPages.currentPage().getParameters().get('acctId');
        acct = [SELECT Name FROM Account WHERE Id = :accountId LIMIT 1];
   }
}

Visualforce Content and Data

  • Static Resources: Stylesheet, JavaScript, images, archive files etc can be uploaded as static resources and referenced in a Visualforce page using the $Resource global variable. To access files contained in an archive, use the URLFOR function
  • JavaScript Support: Visualforce provides support for including and using JavaScript in its pages
    • <apex:includeScript> and $Resource global variable are used to reference JavaScript files in static resources
  • CSS (Cascading Style Sheet): Visualforce provides support for creating and applying styles on its pages
    • When referenced on a page, the CSS below applies the styles on all h1 and paragraph tags as well as tags that use the class names in their respective class or styleClass attributes
/* customStyleSheet static resource */
h1 { color: #f00; }
p { background-color: #eec; }
.gold { color: rgb(248, 215, 70); font-weight: bold; }
.silver { color: rgb(221, 221, 221); font-weight: bold }
.bronze { color: rgb(228, 118, 28); font-weight: bold }
.loud { font-size: 2.5em; }
  • Visualforce Page Example: use <apex:image> tag
<apex:pagecontroller="VisualforceDemo" lightningStylesheets="true">
    <apex:stylesheetvalue="{!$Resource.customStyleSheet}"/>   
    <apex:imageurl="{! $Resource.LetterA }" width="150"/>
    <apex:imageurl="{! URLFOR($Resource.Letters, '/letter-B.png') }"width="150"/>
    <apex:imageurl="{! URLFOR($Resource.Letters, '/child/letter-C.png') }"width="150"/>
    <apex:form>
        <apex:pageBlocktitle="My Custom Account Editor" mode="edit">  
            <apex:pageBlockSection title="Account Details" columns="2">
            <apex:outputLabel>Enter Nickname</apex:outputLabel>
            <apex:inputTextvalue="{!title}"/>
            <apex:outputLabelid="name">Hello, my nickname is: {!title}</apex:outputLabel>
            <apex:commandButtonrerender="name"value="Submit"/>
        </apex:pageBlockSection>   
        </apex:pageBlock>
        </apex:form>   
    <apex:includeScriptvalue="{!$Resource.SuperAutoComplete}"/>   
    <script>
    // objects or functions from the referenced JavaScript file can be called.
    function runCoolFunction(el) {
        SuperAutoComplete.run(el); 
    }
    </script>
</apex:page>

  • iFrame component can be used to display an external website in Salesforce, use <apex:iframe>
  • Maps: JavaScript-based maps can be displayed using Visualforce
  • Charts: charts can be displayed on Visualforce pages by using Visual charting components
  • Global Data: Global variables can be used to reference general information about the current user and organization on a page
    • Ex: {!User.FirstName}, {!Organization.Country} or {!Page.some.VisualforcePageName}
    • The $Resource global variable and dot notation is used to reference static resources that are uploaded as standalone files. Ex: {!$Resource.<resourcename>}
    • Display global variables using the $ symbol. Ex:
      • <apex:outputText value="{!$Profile.Name}" />
      • <apex:outputText value="{!$UserRole.Name}" />
  • The standard detail page for a particular object record can be displayed using the <apex:detail> component
<apex:page standardController="Account">
    <apex:detailsubject="{!Account.Id}"/>
</apex:page>
  • Related Lists can be added to a Visualforce page via the <apex:detail> component or by using the <apex:relatedList> component
    • Example below uses a related list component to display opportunities related to the account:
<apex:page standardController="Account">
    <apex:detail relatedList="false"/>
    <apex:relatedList list="Opportunities"/>
</apex:page>
  • Iteration components can be used to display a table of data or list of records
<apex:page standardController="Account">
    <apex:pageBlock>
        <apex:pageBlockTablevalue="{!Account.Contacts}"var="contact">
            <apex:columnvalue="{!contact.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
  • Related Object Data: Visualforce pages can display field values from related records
    • Up to five levels of child-to-parent relationships can be accessed
    • Ex: {!contact.Account.Owner.FirstName}
<apex:page standardController="Contact">
    <apex:outputTextvalue="{!Contact.Account.Name}" />
</apex:page>

Visualforce Controllers

  • Visualforce pages use either a standard or custom controller, and an extension class can be added to extend its functionality
  • A controller is an Apex class used by a Visualforce page for setting and displaying data as well as performing other functionality or server-side processes
  • Controller Types:
    • Standard Controller: exists for every standard and custom object, contains the functionality and logic that are used for standard Salesforce pages
    • Custom Controller: custom controllers and controller extensions can be written to override existing functionality, customize navigation, use callouts and web services, or have finer control over information access
      • Built from scratch, implements logic without leveraging a standard controller
      • Uses the default, no argument constructor for the outer, top-level class
      • Custom controller constructor that includes parameters cannot be created
      • Custom controllers can contain getter, setter, and action methods

Standard Controllers

  • Visualforce page markup below shows how a custom object can be associated with the Visualforce page:
<apex:page standardController="MyCustomObject__c">
    <!-- ... -->
</apex:page>
  • Following Visualforce page markup uses the standard controller for the Account object and easily accesses field values of a current record
<apex:pagestandardController="Account"sidebar="false"lightningStylesheets="true">
    <apex:pageBlocktitle="Account Summary">
        <apex:pageBlockSectioncolumns="1">
            <apex:outputFieldvalue="{!Account.Id}"/>
            <apex:outputFieldvalue="{!Account.Name}"/>
            <apex:outputFieldvalue="{!Account.Type}"/>
            <apex:outputFieldvalue="{!Account.Industry}"/>
            <apex:outputFieldvalue="{!Account.BillingCity}"/>
            <apex:outputFieldvalue="{!Account.Owner.Name}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>
  • Visualforce page below uses a standard controller to display record details without the need to write custom Apex code:

Standard List Controllers

  • Standard list controller is used by setting the standardController attribute on the <apex:page> component and the recordSetVar attribute on the same component
    • recordSetVar not only indicates the page uses a list controller, it also indicates the variable name of the record collection, which can be used to access data in the record collection
    • <apex:selectlist> component can be used to include list views to filter records displayed on a page
<apex:page standardController="Opportunity" recordSetVar="opportunities">
  • Following Visualforce page uses a list controller to display a list of opportunities

Standard Set Controllers

  • Standard set controller allows developers to build functionality not available in the standard list controller
    • StandardSetController can be used to create a custom list controller or extend the built-in list controller
    • StandardSetController object is used in the constructor for a custom list controller
  • Apex code below shows how a StandardSetController can be used in the constructor for a custom list controller
// Example instantiation of the StandardSetController class using a list of sObjects
public with sharing classAccountListController {
    // Instantiate the StandardSetController
    public ApexPages.StandardSetController con {
        get {
            if (con == null) {
                List<Account> accs = [SELECT Id,Name,Type 
                                        FROM Account 
                                       WHERE Name like 'A%' 
                                       LIMIT 10];
                con = newApexPages.StandardSetController(accs);
            }
            return con;
        }
        set;
    }
    public list<Account> getAccounts() {
        return (List<Account>) con.getRecords();
    }
}
  • Visualforce markup below shows a custom list controller used to display a list of account records:
<apex:page controller="AccountListController">
    <apex:pageBlocktitle="sObject List Example">
        <apex:pageBlockTablevalue="{!accounts}"var="a">
            <apex:columnvalue="{!a.Id}" />
            <apex:columnvalue="{!a.Name}" />
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

  • Example AccountQueryLocator class below instantiates the standard set controller using the Database.getQueryLocator() method
// Example of instantiating the Standard Set Controller using a query locator
public with sharing class AccountQueryLocator {
    // Instantiate the StandardSetController
    public ApexPages.StandardSetController con {
        get {
            if (con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator([
                    SELECT Id, Name, Type 
                      FROM Account 
                     WHERE Name LIKE 'A%'
                     LIMIT 10
                ]));
            }
            return con;
        }
        set;
    }
    // Initialize con and return a list of records
    public List<Account> getAccounts() {
        return (List<Account>) con.getRecords();
    }
}
  • Following Visualforce page displays a list of accounts using built-in functionality provided by the standard list controller through the AccountQueryLocator class
<apex:page controller="AccountQueryLocator">
    <apex:pageBlock title="Query Locator Example">
        <apex:pageBlockTable value="{!accounts}" var="a">
            <apex:column value="{!a.Id}"/>
            <apex:column value="{!a.Name"/>
            <apex:column value="{!a.Type}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

  • StandardSetController can handle up to 10,000 records - how it responds to excess results depends on how the controller is instantiated
    • It exposes multiple methods to control pagination:
      • setPageSize(), next(), previous(), getRecords()
    • Important methods for performing actions on multiple records at once:
      • getSelected(), getRecord(), save()

Custom Controllers

  • A custom controller is an Apex class that implements all of the logic for a page without leveraging a standard controller
    • System mode: by default, custom controllers run in system mode, meaning they do not enforce user permissions and field-level security of the current user. Use with sharing keywords to have the controller respect users' org-wide defaults, role hierarchy, and sharing rules.
    • Referencing Methods: like a standard controller, custom controller methods can be referenced with the {! } notation in the Visualforce page markup
  • Binding Data based on URL Parameter: data displayed on a Visualforce page can be based on parameters passed to it vie the page URL
    • getParameters() method can be used to retrieve parameters passed to the URL. It returns a map of the query string parameters in the page URL.
public class CustomController {
    
    public Opportunity opp;

    public CustomController() {
        opp = [SELECT Id, Name, StageName
                 FROM Opportunity
                WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Opportunity getOpportunity() {
        return opp;
    }

    public PageReference save() {
        update opp;
        return null;
    }
}
  • Following shows using an <apex:inputField> for capturing user input and the <apex:commandButton> to update the name of the opportunity bound to the page.
<!-- This Visualforce page markup uses the custom controller 
to allow a user to update the name of an opportunity. -->
<apex:pagecontroller="CustomController">
    <apex:form>
        <apex:pageBlocktitle="Welcome {!$User.FirstName}!">
            Opportunity Name: <apex:inputFieldvalue="{!opportunity.name}"/>
            <apex:commandButtonaction="{!save}"value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
  • Following shows a custom controller that provides an option to sort a list of records
public class CustomAccountController {
    public Stringorder = 'Name'; // default field used for sorting the records
    
    public List<Account> getAccounts() {
        List<Account> accounts = Database.query(
            'SELECT Id,Name,BillingState,BillingCountry' + 
            ' FROM Account WHERE BillingCountry != \'\' ORDER BY' 
            + order 
            + ' LIMIT 10');
        returnaccounts;
    }
    
    public String getOrder() {
        returnorder;
    }
    public void setOrder(String s) { // set the field used for sorting
        order = s;
    }
    public PageReference save() {
        returnnull;
    }
}
  • Following Visualforce markup uses a custom controller and provides a text input to allow users to change the field used for the sorting:
<apex:page controller="CustomAccountController">
    <apex:pageBlock title="Ordered List of Accounts">
        <apex:form>
            <apex:outputLabel value="Enter the name of the field:" />
            <apex:inputText value="{!order}" />
            <apex:commandButton value="Order"action="{!save}" />
        </apex:form>
        <apex:pageBlockSection columns="1">
            <apex:pageBlockTable value="{!Accounts}"var="a">
                <apex:column value="{!a.Id}" />
                <apex:column value="{!a.Name}" />
                <apex:column value="{!a.BillingState}" />
                <apex:column value="{!a.BillingCountry}" />
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Controller Extensions

  • Controller Extensions: an Apex class which can be used to extend a standard or custom controller
    • Constructor method: takes a single argument of type ApexPages.StandardController or MyCustomController where MyCustomController is the name of the custom controller to extend
    • Extensions Attribute: extensions attribute of <apex:page> component is used to associate a controller extension with a Visualforce page
    • Multiple Extensions: multiple controller extensions can be used in a Visualforce page by defining a comma-separated list of the controller extension names in the extensions attribute
  • Following shows a Controller Extension using a constructor with a single argument
public class ControllerExtensions {
    public Account acc;

    public ControllerExtensions(ApexPages.StandardController stdController) {
        this.acc = (Account)stdController.getRecord();
    }

    public String getName() {
        String str = acc.name;
        return str;
    }
}
  • Visualforce page markup below uses a controller extension with a standard controller:
<apex:page standardController="Account"extensions="ControllerExtension">
    <h1>Details for: {!name}</h1>
    <p><apex:outputLabelvalue="Account ID: "/><apex:outputFieldvalue="{!Account.Id}"/></p>
    <p><apex:outputLabelvalue="Account Name: "/><apex:outputFieldvalue="{!Account.Name}"/></p>
    <p><apex:outputLabelvalue="Account Type: "/><apex:outputFieldvalue="{!Account.Type}"/></p>
    <p><apex:outputLabelvalue="Account City: "/><apex:outputFieldvalue="{!Account.BillingCity}"/></p>
</apex:page>
  • Controller extensions are Apex classes that extend functionality of a standard or custom controller - they can be used to override one or more actions:
    • Edit, View, Save, Delete, Add New
    • Associate them with the page using the extensions attribute of the <apex:page> component
    • Multiple Extensions: multiple controller extensions can be defined for a single page through a comma-separated list
public class MyControllerExtension {
    
    // controller extension constructor
    public MyControllerExtension(ApexPages.standardController controller) {}
    
    // override the standard edit action
    public PageReference edit() {
        PageReference pr;
        return pr;
    }
    
    // create a new custom action
    
    public PageReference myNewAction() {
        PageReference pr;
        return pr;
    }
}
  • Visualforce page markup below shows how multiple controller extensions can be defined on the extensions attribute. Also shows how the methods can be invoked from the page.
<apex:page standardController="Account" extensions="myExtension1,myExtension2,myExtension3">
    <apex:form>
        <!-- standard controller methods are accessible -->
        <apex:commandButtonaction="{!save}" value="Save" />
        <apex:commandButtonaction="{!edit}" value="Edit" />
        <apex:commandButtonaction="{!myNewAction}" value="New Action" />
    </apex:form>
</apex:page>

When to Use Which Controller

  • Use a ____ controller when:
    • Standard Controller: when a Visualforce page requires basic functionality, when standard actions do not need to be customized
    • Standard List Controller: display a list of records, use list view filters on a Visualforce page, create a Visualforce page with pagination features
    • Standard Set Controller: use to create a custom list controller or extend the pre-built Visualforce list controller, add features not supported by standard list controllers such as custom sorting
    • Controller Extension: use to extend or override a standard action, add a custom button to a page, build a page that respects user permissions
    • Custom Controller: used to implement total custom logic, use if the page needs to use web services or HTTP callouts, build a page that runs entirely in system mode, create a page with new actions, customize user navigation
  • A custom controller is required when data is not accessible using a standard controller
    • One level down: displaying opportunities related to an account is possible using a Visualforce page that is bound to the standard account controller
    • Two levels down: displaying opportunity line items related to the opportunities of the account is not possible on the same Visualforce page
  • Use a controller extension for two reasons:
    • Reuse functionality: when a necessary functionality already exists in the standard or custom controller used in the page
    • Require standard features: declarative features that depend on a standard controller like using custom buttons or if the Visualforce page needs to be embedded in the page layout

Getter Methods

  • Getter Methods: are used to return values from the controller
    • Display values: every value calculated by a controller and displayed in a page must have a corresponding getter method
    • Expression Syntax: expressions in Visualforce page markup using the general pattern {! expression_name } automatically connect to the getter method to get access to the data
    • Run queries: its possible for a getter method to contain a SOQL query that retrieves data to display on the Visualforce page
  • Using Getter methods: names of getter methods always start with the get prefex, so they are typically called getIdentifier
    • Method Structure: getter methods have no input parameters but return an object
    • Naming Convention: getScore() or getProjectManagers()
    • Access Modifier: getter methods need at least public access level so Visualforce can access them
    • Calling the method: results of a getter method can be displayed by using the name of the getter without the get, ex: {!Score} or {!ProjectManagers}
    • Accessing data: required to access data from the Visualforce page controller
  • Custom Controller Class using Getter Method: results of getAccounts getter method below can be accessed from a Visualforce page using {!accounts} expression
public class CustomController {
    public List<Account> getAccounts() {
        List<Account> result = [SELECT Id, Name, BillingCity
                                  FROM Account
                                 WHERE BillingCity != NULL
                                 LIMIT 10];
    }
}
  • Visualforce Page using the Getter Method: results of getAccounts getter method is displayed using <apex:pageBlockTable> to iterate through the record list
<apex:page controller="CustomController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{! accounts }" var="a">
            <apex:column value="{! a.Id }"/>
            <apex:column value="{! a.Name }"/>
            <apex:column value="{! a.BillingCity }"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
  • Following Visualforce page uses CustomController custom controller to display 10 accounts where its Billing City is not empty

Setter Methods

  • Setter methods: used to submit values from the Visualforce page back to the controller
    • Called before action methods: executed automatically before action methods
    • No setter methods required: setter methods may not always be required to pass values into a controller, ex 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 by a corresponding action method
      • Ex: <apex:inputField value="{!lead.company}" />
  • Using Setter methods: names of setter methods must always start with the set prefix
    • Naming Convention: setVariable in a Visualforce controller sets value of variable
    • Access modifier: should have at least public access level so the Visualforce framework can access them
    • Method Structure: setter method has one input parameter and does not return anything
    • Not always necessary: its not always necessary to include a setter method to pass values into a controller
  • setText setter method in the custom controller below is used to set the value of the text variable from the Visualforce page
public class CustomController {
    public String text;
    public String getText() {
        return text;
    }

    public void setText(String t) {
        text = t;
    }

    public PageReference save() {
        return null;
    }
}
  • The Visualforce page below allows a user to set the value of the text variable by entering the value in <apex:inputText> and clicking on the save button:
<apex:page controller="CustomController">
    <apex:form>
        <apex:outputlabel value="Enter text: "/>
        <apex:inputtext value={!text}"/>
        <apex:commandButton value="Save" action="{!Save}" /><p/>
        <apex:outputtext value="The text you entered is: {!text}"
    </apex:form>
</apex:page>

Getter and Setter Properties

  • Using Properties for Getter and Setter methods: Apex properties can be used instead of defining getter/setter methods to reduce total code
    • Property Definition: property definitions include one or two code blocks representing a get accessor and a set accessor:
      • Code in a get accessor executes when the property is read, set accessor when the property is assigned a new value
    • Access Level Definition
      • If property has get access or only, then read only
      • Set, then write-only
      • Both, then read-write
  • Using Automatic Properties: property with an empty code block is called an automatic property, typically with the following syntax: public dataType propertyName {get; set; }
    • Accessors expected: Visual force framework expects {get; set;} for a property used on a Visualforce page
    • If no property defined: framework looks for a corresponding getter method
    • If no property or method: an exception is thrown if the page tries to get a value from the controller for which no property was defined
  • Sample controller below shows three properties with different access levels based on defined accessors
// Example of an Apex controller containing different ways of using
// automatic properties in place of getter and setter methods
public class PropertyController {
    // Read-write property
    public String propertyName1 { get; set; }
    
    // Read-only property
    public Integer propertyName2 { get; }
    
    // Write-only property
    public Boolean propertyName3 { set; }
}

Action Methods

  • Action methods can be used in a custom controller to respond to user input on a Visualforce page such as the clicking of a button or link
    • Naming convention: they are named the same as the expression that references them
    • Multiple actions allowed: custom controller can contain as many custom action methods as needed
    • Action methods: components such as <apex:commandButton> can be used to execute action methods
    • Controller methods: when an action is performed on the Visualforce page, it executes the corresponding action method in the controller
  • search() Action Method in the Custom Controller - following custom controller defines an action method called search
    • The search() action method in this custom controller populates the accounts variable with a list of records based on a filter entered by the user on the Visualforce page.
public class CustomController {
    public String text;
    public List<Account> accounts;

    public String getText() {
        return text;
    }
    
    public void setText(String t) {
        text = t;
    }
    public List<Account> getAccounts() {
        return accounts;
    }
    public PageReference search() {
        accounts = [SELECT Id, Name, CreatedBy.FirstName 
                      FROM Account
                     WHERE CreatedBy.FirstName = :text];
        return null;
    }
}
<apex:page controller="CustomController">
    <apex:form>
        <apex:outputlabel value="Enter the first name of the user: "/>
        <apex:inputtext value="{!text}"/>
        <apex:commandButton value="Search"action="{!search}"/>
        <apex:outputtext value="The user you entered has the following accounts: "/>
            <apex:pageBlock>
            <apex:pageBlock Sectiontitle="Accounts">
                <apex:pageBlock Tablevalue="{!accounts}" var="a" rendered="{!NOT(ISNULL(accounts))}">
                    <apex:column value="{!a.Id}"/>
                    <apex:column value="{!a.Name}"/>
                    <apex:column value="{!a.CreatedBy.FirstName}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
  • Action-Aware Tags: action methods can be called using the action attribute that is supported in the following Visualforce tags:
    • <apex:commandButton>: action is called when user clicks the button
    • <apex:commandLink>: action is called when a user clicks the link
    • <apex:actionPoller>: action is called periodically without user input
    • <apex:actionSupport>: causes a JavaScript event on another component to call an action
    • <apex:actionFunction>: defines a JavaScript function that calls an action
    • <apex:page>: action is called when the page is loaded

Other Considerations

  • Some considerations when working with controllers or extensions for Visualforce pages:
    • Web Services: if a class includes a web service, it must be defined as global
    • Security: like most Apex classes, controllers and extensions run in system mode, use with sharing keyword if appropriate. Controllers and extensions are generally declared public
    • Apex Variables: primitives like Strings and Integers are passed by value, non-primitives like Lists or sObjects are passed by reference
    • Getter/Setter: DML statements can’t be used in getter methods and constructor methods, getter/setter methods cannot be annotated with @future
    • Process Order: order of methods and variables being processed is not guaranteed
    • Person/Business: name fields behave differently for person accounts and business accounts, use a custom formula field to ensure proper processing of both account types

Scenarios and Solutions

Reference FoF Slides