This document discusses standard controllers in Visualforce. Standard controllers provide the same functionality as standard Salesforce pages for objects like Accounts. They allow accessing object data and performing standard actions like save, edit, delete. Validation rules can display errors on pages using standard controllers. Pages can override the default styling of the associated object tab. It's important to check user access before rendering pages for an object.
2. Introduction
• A Visualforce controller is a set of instructions that specify what happens
when a user interacts with the components specified in associated
Visualforce markup, such as when a user clicks a button or link. Controllers
also provide access to the data that should be displayed in a page, and can
modify component behavior.
• The Force.com platform provides a number of standard controllers that
contain the same functionality and logic that are used for standard
Salesforce pages. For example, if you use the standard Accounts controller,
clicking a Save button in a Visualforce page results in the same behavior as
clicking Save on a standard Account edit page.
• A standard controller exists for every Salesforce object that can be queried
using the Force.com API.
3. Introduction
• The following topics include additional information about using
standard controllers:
• Associating a Standard Controller with a Visualforce Page
• Accessing Data with a Standard Controller
• Using Standard Controller Actions
• Validation Rules and Standard Controllers
• Styling Pages that Use Standard Controllers
• Checking for Object Accessibility
• Custom Controllers and Controller Extensions
4. Associating a Standard Controller with a
Visualforce Page
• Note: When you use the standardController attribute on the
<apex:page> tag, you cannot use the controller attribute at the same
time.
5. Accessing Data with a Standard Controller
• Every standard controller includes a getter method that returns the
record specified by the id query string parameter in the page URL.
This method allows the associated page markup to reference fields on
the context record by using {!object} syntax, where object is the
lowercase name of the object associated with the controller.
• For example, a page that uses the Account standard controller can
use {!account.name} to return the value of the name field on the
account that is currently in context.
6. Accessing Data with a Standard Controller
• You can traverse up to five levels of child-to-parent relationships. For
example, if using the Contact standard controller, you can use
{!contact.Account.Owner.FirstName} (a three-level child-to-parent
relationship) to return the name of the owner of the account record
that is associated with the contact.
• You can traverse one level of parent-to-child relationships. For
example, if using the Account standard controller, you can use
{!account.Contacts} to return an array of all contacts associated with
the account that is currently in context.
7. Using Standard Controller Actions
• Action methods perform logic or navigation when a page event
occurs, such as when a user clicks a button, or hovers over an area of
the page. Action methods can be called from page markup by using
{! } notation in the action parameter of one of the following tags:
• <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
8. Using Standard Controller Actions
• The following table describes the action methods that are supported
by all standard controllers. You can associate these actions with any
Visualforce component that includes an action attribute.
Action Description
save Inserts a new record or updates an existing record if it is currently in context. After this
operation is finished, the save action returns the user to the original page (if known),
or navigates the user to the detail page for the saved record.
quicksave Inserts a new record or updates an existing record if it is currently in context. Unlike
the save action, this page does not redirect the user to another page.
edit Navigates the user to the edit page for the record that is currently in context. After this
operation is finished, the edit action returns the user to the page where the user
originally invoked the action.
9. Using Standard Controller Actions
Action Description
delete Deletes the record that is currently in content. After this operation is finished, the
delete action either refreshes the page or sends the user to tab for the associated
object.
cancel Aborts an edit operation. After this operation is finished, the cancel action returns the
user to the page where the user originally invoked the edit.
List Returns a PageReference object of the standard list page, based on the most recently
used list filter for that object. For example, if the standard controller is contact, and the
last filtered list that the user viewed is New Last Week, the contacts created in the last
week are displayed.
11. Validation Rules and Standard Controllers
• If a user enters data on a Visualforce page that uses a standard
controller, and that data causes a validation rule error, the error can
be displayed on the Visualforce page. If the validation rule error
location is a field associated with an <apex:inputField> component,
the error displays there. If the validation rule error location is set to
the top of the page, use the <apex:pageMessages> or
<apex:messages> component within the <apex:page> to display the
error.
12. Styling Pages that Use Standard Controllers
• Any page associated with a standard controller automatically inherits
the style that is used for standard Salesforce pages associated with
the specified object. That is, the tab for the specified object appears
selected, and the associated color of the tab is used to style all page
elements.
• You can override the styling of a page that uses a standard controller
with the tabStyle attribute on the <apex:page> tag.
13. Checking for Object Accessibility
• If a user has insufficient privileges to view an object, any Visualforce
page that uses a controller to render that object will be inaccessible.
To avoid this error, you should ensure that your Visualforce
components will only render if a user has access to the object
associated with the controller.
14. Checking for Object Accessibility
• It is good practice to provide an alternative message if a user cannot
access an object. For example:
<apex:page standardController="Lead">
<apex:pageBlock rendered="{!$ObjectType.Lead.accessible}">
<p>This text will display if you can see the Lead object.</p>
</apex:pageBlock>
</apex:page>
<apex:page standardController="Lead">
<apex:pageBlock rendered="{!$ObjectType.Lead.accessible}">
<p>This text will display if you can see the Lead object.</p>
</apex:pageBlock>
<apex:pageBlock rendered="NOT({!$ObjectType.Lead.accessible})">
<p>Sorry, but you cannot see the data because you do not have access to the Lead object.</p>
</apex:pageBlock>
</apex:page>