Newer Version Available

This content describes an older version of this product. View Latest

StandardController Class

Use a StandardController when defining an extension for a standard controller.

Namespace

ApexPages

Usage

StandardController objects reference the pre-built Visualforce controllers provided by Salesforce. The only time it is necessary to refer to a StandardController object is when defining an extension for a standard controller. StandardController is the data type of the single argument in the extension class constructor.

Instantiation

You can instantiate a StandardController in the following way:
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17ApexPages.StandardController sc = new ApexPages.StandardController(sObject);

Example

The following example shows how a StandardController object can be used in the constructor for a standard controller extension:

1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class myControllerExtension {
18
19    private final Account acct;
20    
21    // The extension constructor initializes the private member
22    // variable acct by using the getRecord method from the standard
23    // controller.
24    public myControllerExtension(ApexPages.StandardController stdController) {
25        this.acct = (Account)stdController.getRecord();
26    }
27
28    public String getGreeting() {
29        return 'Hello ' + acct.name + ' (' + acct.id + ')';
30    }
31}

The following Visualforce markup shows how the controller extension from above can be used in a page:

1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page standardController="Account" extensions="myControllerExtension">
18    {!greeting} <p/>
19    <apex:form>
20        <apex:inputField value="{!account.name}"/> <p/>
21        <apex:commandButton value="Save" action="{!save}"/>
22    </apex:form>
23</apex:page>