Standard Controllers. salesforce Visualforce Developer Guide
Below you will find brief information for Visualforce Developer Guide. The guide offers comprehensive details on Visualforce which allows developers to build sophisticated user interfaces for Salesforce applications, providing deep customization and control over the user experience.
Advertisement
Advertisement
CHAPTER 5
Standard Controllers
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.
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
Associating a Standard Controller with a Visualforce Page
To associate a standard controller with a Visualforce page, use the standardController
attribute on the
<apex:page >
tag and assign it the name of any Salesforce object that can be queried using the Force.com API.
For example, to associate a page with the standard controller for a custom object named MyCustomObject, use the following markup:
<apex:page standardController= "MyCustomObject__c" >
</apex:page >
Note:
When you use the standardController
attribute on the
<apex:page >
tag, you cannot use the controller attribute at the same time.
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.
74
Standard Controllers Using Standard Controller Actions
Note: For the getter method to succeed, the record specified by the id
query string parameter in the URL must be of the same type as the standard controller. For example, a page that uses the Account standard controller can only return an account record.
If a contact record ID is specified by the id
query string parameter, no data is returned by the
{!account}
expression.
As with queries in the Force.com API, you can use merge field syntax to retrieve data from related records:
•
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.
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:
•
creates a button that calls an action
•
creates a link that calls an action
•
•
makes an event (such as “onclick”, “onmouseover”, and so on) on another, named component, call an action
•
defines a new JavaScript function that calls an action
•
calls an action when the page is loaded
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
save quicksave edit delete cancel list
Description
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.
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.
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.
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.
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.
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.
75
Standard Controllers Validation Rules and Standard Controllers
For example, the following page allows you to update an account. When you click Save, the save
action is triggered on the standard controller, and the account is updated.
<apex:page standardController= "Account" >
<apex:form >
<apex:pageBlock title= "My Content" mode= "edit" >
<apex:pageBlockButtons >
<apex:commandButton action= "{!save}" value= "Save" />
</apex:pageBlockButtons >
<apex:pageBlockSection title= "My Content Section" columns= "2" >
<apex:inputField value= "{!account.name}" />
<apex:inputField value= "{!account.site}" />
<apex:inputField value= "{!account.type}" />
<apex:inputField value= "{!account.accountNumber}" />
</apex:pageBlockSection >
</apex:pageBlock >
</apex:form >
</apex:page >
Note: Remember, for this page to display account data, the ID of a valid account record must be specified as a query parameter in the URL for the page. For example: https://
Salesforce_instance
/apex/myPage?id=001x000xxx3Jsxb
Note: Command buttons and links that are associated with save
, quicksave
, edit
, or delete
actions in a standard controller are only rendered if the user has the appropriate permissions. Likewise, if no particular record is associated with a page, command buttons and links associated with the edit
and delete
actions are not rendered.
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.
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. For example, the following page uses the Account standard controller, but renders a page that highlights the Opportunities tab and uses the Opportunity tab's yellow coloring:
<apex:page standardController= "Account" tabStyle= "Opportunity" >
</apex:page >
76
Standard Controllers Checking for Object Accessibility
To use the styling associated with MyCustomObject:
<apex:page standardController= "Account" tabStyle= "MyCustomObject__c" >
</apex:page >
To use the styling associated with a custom Visualforce tab, set the attribute to the name (not label) of the tab followed by a double-underscore and the word tab. For example, to use the styling of a Visualforce tab with the name Source and a label Sources, use:
<apex:page standardController= "Account" tabStyle= "Source__tab" >
</apex:page >
Alternatively, you can override standard controller page styles with your own custom stylesheets and inline styles.
SEE ALSO:
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.
You can check for the accessibility of an object like this:
{!$ObjectType.objectname.accessible}
This expression returns a true or false value.
For example, to check if you have access to the standard Lead object, use the following code:
{!$ObjectType.Lead.accessible}
For custom objects, the code is similar:
{!$ObjectType.MyCustomObject__c.accessible} where
MyCustomObject__c
is the name of your custom object.
To ensure that a portion of your page will display only if a user has access to an object, use the render
attribute on a component. For example, to display a page block if a user has access to the Lead object, you would do the following:
<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 >
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: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 >
77
Standard Controllers
</apex:pageBlock >
</apex:page >
Checking for Object Accessibility
78
Download
Advertisement
Key features
Custom UI Development
Integration with Apex
PDF rendering
AJAX support
Custom components
Standard controller
Custom controller
Controller extension
Frequently asked questions
Visualforce is a framework that allows you to create custom user interfaces for Salesforce applications.
The document does not explicitly state which editions support Visualforce. See Salesforce official documentation for details.
The document explains some aspects of Visualforce architecture.