Newer Version Available
StandardController Class
Namespace
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
1ApexPages.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:
1public class myControllerExtension {
2
3 private final Account acct;
4
5 // The extension constructor initializes the private member
6 // variable acct by using the getRecord method from the standard
7 // controller.
8 public myControllerExtension(ApexPages.StandardController stdController) {
9 this.acct = (Account)stdController.getRecord();
10 }
11
12 public String getGreeting() {
13 return 'Hello ' + acct.name + ' (' + acct.id + ')';
14 }
15}The following Visualforce markup shows how the controller extension from above can be used in a page:
1<apex:page standardController="Account" extensions="myControllerExtension">
2 {!greeting} <p/>
3 <apex:form>
4 <apex:inputField value="{!account.name}"/> <p/>
5 <apex:commandButton value="Save" action="{!save}"/>
6 </apex:form>
7</apex:page>StandardController Methods
The following are methods for StandardController. All are instance methods.
addFields(fieldNames)
Signature
public Void addFields(List<String> fieldNames)
Return Value
Type: Void
Usage
This method should be called before a record has been loaded—typically, it's called by the controller's constructor. If this method is called outside of the constructor, you must use the reset() method before calling addFields().
The strings in fieldNames can either be the API name of a field, such as AccountId, or they can be explicit relationships to fields, such as something__r.myField__c.
This method is only for controllers used by dynamicVisualforce bindings.
cancel()
Signature
public System.PageReference cancel()
Return Value
Type: System.PageReference
delete()
Signature
public System.PageReference delete()
Return Value
Type: System.PageReference
edit()
Signature
public System.PageReference edit()
Return Value
Type: System.PageReference
getId()
Signature
public String getId()
Return Value
Type: String
getRecord()
Signature
public SObject getRecord()
Return Value
Type: sObject
Usage
Note that only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression.
Example
1<apex:outputText
2value="{!account.billingcity}
3{!account.contacts}"
4rendered="false"/>reset()
Signature
public Void reset()
Return Value
Type: Void
Usage
This method is only used if addFields is called outside the constructor, and it must be called directly before addFields.
This method is only for controllers used by dynamicVisualforce bindings.
save()
Signature
public System.PageReference save()
Return Value
Type: System.PageReference
view()
Signature
public System.PageReference view()
Return Value
Type: System.PageReference