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.
ApexPages.StandardController sc = new ApexPages.StandardController(sObject);
The following example shows how a StandardController object can be used in the constructor for a standard controller extension:
public class myControllerExtension { private final Account acct; // The extension constructor initializes the private member // variable acct by using the getRecord method from the standard // controller. public myControllerExtension(ApexPages.StandardController stdController) { this.acct = (Account)stdController.getRecord(); } public String getGreeting() { return 'Hello ' + acct.name + ' (' + acct.id + ')'; } }
The following Visualforce markup shows how the controller extension from above can be used in a page:
<apex:page standardController="Account" extensions="myControllerExtension"> {!greeting} <p/> <apex:form> <apex:inputField value="{!account.name}"/> <p/> <apex:commandButton value="Save" action="{!save}"/> </apex:form> </apex:page>
The following are methods for StandardController. All are instance methods.
public Void addFields(List<String> fieldNames)
Type: Void
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.
public System.PageReference cancel()
Type: System.PageReference
public System.PageReference delete()
Type: System.PageReference
public System.PageReference edit()
Type: System.PageReference
public String getId()
Type: String
public SObject getRecord()
Type: sObject
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.
<apex:outputText value="{!account.billingcity} {!account.contacts}" rendered="false"/>
public Void reset()
Type: Void
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.
public System.PageReference save()
Type: System.PageReference
public System.PageReference view()
Type: System.PageReference