Learning Objectives

After completing this unit, you’ll be able to:
  • Explain what a Visualforce standard controller is and describe its key attributes.
  • Add a standard controller to a Visualforce page.
  • Display data from a record retrieved by a page’s standard controller.
  • Write an expression that uses dot notation to access fields on a related record.

Introduction to the Visualforce Standard Controller

Visualforce uses the traditional model–view–controller (MVC) paradigm, and includes sophisticated built-in controllers to handle standard actions and data access, providing simple and tight integration with the Force.com database. These built-in controllers are referred to generally as standard controllers, or even the standard controller.

The MVC design pattern makes it easy to separate the view and its styling from the underlying database and logic. In MVC, the view (the Visualforce page) interacts with a controller, and the controller provides functionality to the page. For example, the controller can contain the logic to be executed when a button is clicked. A controller also typically interacts with the model (the database)—making available data that the view might want to display, or pushing changes back to the database.

Most standard and all custom objects have standard controllers that can be used to interact with the data associated with the object, so you don’t need to write the code for the controller yourself. You can extend the standard controllers to add new functionality, or create custom controllers from scratch. Here you’ll learn about the standard controllers.

Find a Record ID and Add It to the Request URL

Provide the record ID for a record to the standard controller by adding it as a parameter in the request URL.

If you want to use the standard controller to reference a specific record, it needs to know the record identifier, or ID, of the record to work with. It uses the ID to retrieve the data, and to save it back to the database when the record’s data is changed.

When your Visualforce pages interact with other pages in your organization, you can automatically pass in the record’s identifier, and your Visualforce page can use that to look up and display that record’s data. But during development your pages are stand-alone, so for your page to display data from a record in the database, you need to provide the record ID manually. The easiest way to do this is to add it as a GET parameter in the request URL.

  1. Open the Developer Console and click File | New | Visualforce Page to create a new Visualforce page. Enter AccountSummary for the page name.
  2. In the editor, replace any markup with the following.
    <apex:page sidebar="false">
        <apex:pageBlock title="Account Summary">
    This markup creates a box with platform styling, ready for you to add some useful information.
  3. Click Preview to open a preview of your page that you can look at while you make changes.
    Make sure you can see the URL field for the preview window. You’ll come right back to this in a few steps.
  4. In a separate browser window, go to the home page for your organization, and then select the Accounts tab.
    If the Accounts tab isn’t visible, switch to the Sales application by choosing Sales from the application menu in the upper-right corner.
  5. Ensure the View menu shows All Accounts, and click Go to view all of the account records.
    View all accounts and click Go
  6. On the All Accounts page, click Acme, or whatever is the name of the first company in the list.
    All Accounts list
  7. When the account details page finishes loading, look at the URL for the page.
    The URL will look something like this: https://SalesforceInstance/001D000000JRBes.

    The record’s identifier, its ID, is the series of letters and numbers at the end. In this example, it’s “001D000000JRBes” (but in your organization it’ll be different). It’s unique across all records of all object types in your organization.

  8. Select the record ID, and copy it to your clipboard.
    Before you leave the account detail page, take a look at the full page and the information displayed on it.
    Account details page
    This wasn’t a detour just to get a record ID. Before you’re done here, you’ll know how to create this page, yourself, in Visualforce code.
  9. Switch back to the preview page you opened from the Developer Console. Click into the URL field of the browser window, and at the end of the URL enter &id= and then paste in the record ID you copied previously.
    The URL should be something like this: https://SalesforceInstance/apex/AccountSummary?core.apexpages.request.devconsole=1&id=001D000000JRBes
  10. Press Return to load the page at the new URL.

Although loading the preview page with the record ID in it doesn’t look any different yet, adding the ID means you can now ask the standard controller to help you out by loading that record and making it available on the page.

Display Data from a Single Record

Add the standard controller for accounts to the page, and then reference account fields to display a record’s data.

Follow these steps to create a page that shows an account summary.

  1. At the beginning of your page markup, inside the <apex:page> tag, add the following attribute.
    When you save the page now, the preview page will reload as before, except this time the standard controller for accounts is active. When the page loads, the standard controller parses the parameters in the URL, finds the id parameter, and uses its value to retrieve a record and make it available to the page. You can’t see it yet, but it’s there.
  2. In the body of the page, add the following markup.
    Name: {! Account.name }
    Boom. Now you can see that the record is being retrieved! You should see the name of the account that has the record ID that you added to the URL.
  3. Replace the single line with the account name with the following markup.
    Name: {! Account.Name } <br/>
    Phone: {! Account.Phone } <br/>
    Industry: {! Account.Industry } <br/>
    Revenue: {! Account.AnnualRevenue } <br/>
    Your complete code should look like this.
    <apex:page sidebar="false" standardController="Account">
        <apex:pageBlock title="Account Summary">
                Name: {! Account.Name } <br/>
                Phone: {! Account.Phone } <br/>
                Industry: {! Account.Industry } <br/>
                Revenue: {! Account.AnnualRevenue } <br/>
    The result should be a simple account summary.
    A simple account summary page
So, what is going on here? A whole lot, courtesy of the standard controller!
  1. When the page is loaded, and the <apex:page> component is activated, it activates a standard controller for the account object.
  2. The standard controller sees that there’s an ID parameter in the URL, and searches for and retrieves the matching account record.
  3. The standard controller takes the record and puts it in a variable that it makes available to the page. The variable has the same name as the standard controller’s sObject: Account. It’s an object variable, and contains all of the fields available on the Account sObject.
  4. The four Visualforce expressions all reference the Account variable. They use “dot notation” to access individual fields within the Account variable. So, {! Account.Name } gets the name of the account, and so on.

But, what’s up with the revenue number? It’s displaying in scientific notation. How can you display that as currency instead?

The reason the number is displaying as a “raw” value in scientific notation is because the number is being directly output by an expression. It’s putting the raw value directly on the page. To control the formatting of the value, you need to use a component, and give the component the value to handle. The component will take the raw value and format it appropriately, and then take care of outputting the result onto the page. You’ll learn how to do that elsewhere. Here we’re just interested in accessing the record data.

Tell Me More...

The standard controller is quite powerful, and we’ve just scratched the surface. You’ll learn a lot more about them elsewhere, but here are a few highlights.

In addition to the easy data access illustrated here, the standard controller provides a set of standard actions, such as create, edit, save, and delete, that you can add to your pages using standard user interface elements such as buttons and links. You’ll learn more about these standard actions when you learn about Visualforce input forms and saving changes back to the database.

Visualforce pages that you want to embed within an object’s page layouts, use as object-specific custom actions, or use as mobile cards in Salesforce1 must use the standard controller for the relevant object.

If you’ve created your own custom objects (instead of using objects like Account) and want to know how to reference a field, you have to follow a slightly different procedure. From Setup, enter Objects in the Quick Find box, then select Objects, select your object, and then the field. The API Name now indicates the name of the field that you must use in your Visualforce pages. For example, if your field was called Foo, its API Name is Foo__c, and you’d reference it with that name—something like: {! myobject__c.foo__c}.

As powerful as the standard controller is, there are times when you need to do something different. And that’s no problem in Visualforce. At any time you can replace or augment them, and write your own controllers, or extensions to the built-in controllers, using Apex code.

Share Time Estimate

Having trouble with your challenge verification?

Here are some tips:

  1. Check for typos (hey, it happens)
  2. Try using a new Developer Edition (existing customizations can interfere with the validation)