A Visualforce Controller Extension in Salesforce is an Apex class designed to extend the functionality of a standard or custom controller associated with a Visualforce page. This extension allows developers to add custom methods, access additional data, and override standard behaviors, enhancing the capabilities of the page beyond the limitations of standard controllers.
Controller extensions are particularly useful when additional logic is needed while still leveraging the built-in CRUD (Create, Read, Update, Delete) operations provided by standard controllers. By passing a standard controller as a parameter in the extension’s constructor, the extension enables seamless data interaction, offering a flexible way to customize user interactions on Visualforce pages.
In our previous Salesforce Tutorial we have learned about Visualforce Custom controller and in this Salesforce Training Tutorial we are going to learn about visualforce controller extension with simple example.
What is Visualforce controller extension ?
Visualforce controller Extension :-If we want to use both custom controller functionality and standard controller functionality we use extension controllers. Extension Controllers begins with Standard controller and extended or overridden with custom controller with custom apex code.
Syntax :-
<apex:page standardcontroller = “contact” extensions = “Testclass1, Testclass2?></apex:page>
Controller extension is the third type of controller which extends the behaviour of standard controller. Controller extensions are used to integrate visualforce page with native user interface. Controller extension can not override the functions of standard buttons.
Custom controllers are used along with standard controllers to become controller extensions. In controller extensions we can use multiple extensions in single visualforce page. Here the extension controller is divided in to smaller controllers. These smaller controllers acts as a subset of the behaviours.
let us see an example of extension controller.
public class MyPageController {
private ApexPages.StandardController controller;
// Constructor that accepts a StandardController instance
public MyPageController(ApexPages.StandardController controller) {
this.controller = controller;
}
// Sample method to perform an action
public PageReference doSomething() {
return null;
}
}
- Purpose of Controller Extension: This Apex class (
MyPageController
) is a controller extension, which means it extends the functionality of the standard controller that is associated with a Visualforce page. - Constructor: The constructor accepts a
StandardController
object as a parameter. This allows the controller extension to access data for a specific record that is being displayed or edited on the Visualforce page.ApexPages.StandardController
is used to work with a single record (e.g., viewing or editing a particularAccount
,Contact
, etc.).
- Method (
doSomething
): This is a placeholder method that currently does nothing (return null
). In a real-world scenario, this method would contain logic to perform specific actions, such as updating a field or triggering a process when a user interacts with the page.
Key Terms:
StandardController
: Used for single record handling in Salesforce.StandardSetController
: Though not directly used in this example, this controller type allows handling multiple records, often used for bulk operations in list views or reports.
Above shown code is the class for controller extension. For controller extension a constructor is used to pass a single record or multiple records to the class. StandardController is used to pass single record to the class and StandardSetcontroller is used to pass multiple records to the class.
Visualforce Page
<apex:page standardController="Resource__c" extensions="MyPageController">
<apex:form>
<apex:commandButton action="{!doSomething}" value="Do Something" />
</apex:form>
</apex:page>
- Standard Controller:
standardController="Resource__c"
indicates that the page is associated with the custom objectResource__c
. The standard controller automatically provides basic data operations like viewing, editing, and saving a record of this object type. - Extension:
extensions="MyPageController"
attaches theMyPageController
extension to the page. This extension provides additional methods or logic that aren’t available in the standard controller by default. - Command Button:
<apex:commandButton>
creates a button labeled “Do Something” on the page. When clicked, it invokes thedoSomething
method from theMyPageController
extension. In this case, sincedoSomething
currently returnsnull
, the button won’t trigger any additional behavior, but it demonstrates how custom logic can be added.
Component | Description |
---|---|
ApexPages.StandardController | Standard controller for handling a single record, such as displaying or updating a record on a Visualforce page. |
MyPageController | Custom controller extension that adds functionality to the standard controller of Resource__c . |
doSomething() Method | Placeholder method in MyPageController for custom actions on the Visualforce page. |
standardController="Resource__c" | Associates the page with the Resource__c object, providing standard operations for a Resource__c record. |
extensions="MyPageController" | Extends the standard controller’s functionality with custom methods from MyPageController . |
<apex:commandButton> | Button that triggers the doSomething method in the controller extension when clicked. |