StandardController Class

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

ApexPages

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.

You can instantiate a StandardController in the following way:

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

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

The following are constructors for StandardController.

Creates a new instance of the ApexPages.StandardController class for the specified standard or custom object.

public StandardController(SObject controllerSObject)

  • controllerSObject: Type: SObject A standard or custom object.

The following are methods for StandardController. All are instance methods.

  • addFields(fieldNames)

    When a Visualforce page is loaded, the fields accessible to the page are based on the fields referenced in the Visualforce markup. This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well.

  • cancel()

    Returns the PageReference of the cancel page.

  • delete()

    Deletes record and returns the PageReference of the delete page.

  • edit()

    Returns the PageReference of the standard edit page.

  • getId()

    Returns the ID of the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

  • getRecord()

    Returns the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

  • reset()

    Forces the controller to reacquire access to newly referenced fields. Any changes made to the record prior to this method call are discarded.

  • save()

    Saves changes and returns the updated PageReference.

  • view()

    Returns the PageReference object of the standard detail page.

When a Visualforce page is loaded, the fields accessible to the page are based on the fields referenced in the Visualforce markup. This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well.

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.

Returns the PageReference of the cancel page.

public System.PageReference cancel()

Type: System.PageReference

Deletes record and returns the PageReference of the delete page.

public System.PageReference delete()

Type: System.PageReference

Returns the PageReference of the standard edit page.

public System.PageReference edit()

Type: System.PageReference

Returns the ID of the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

public String getId()

Type: String

Returns the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

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.

You can work around this restriction by including a hidden component that references any additional fields that you want to query. Hide the component from display by setting the component’s rendered attribute to false.

Forces the controller to reacquire access to newly referenced fields. Any changes made to the record prior to this method call are discarded.

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.

Saves changes and returns the updated PageReference.

public System.PageReference save()

Type: System.PageReference

Returns the PageReference object of the standard detail page.

public System.PageReference view()

Type: System.PageReference