Curriculum
Controller Extensions are Apex classes that extend the functionality of Standard Controllers or Custom Controllers in Visualforce applications. They allow developers to add custom business logic while still taking advantage of the built-in functionality provided by Salesforce Standard Controllers.
Controller Extensions are commonly used when Standard Controllers provide most of the required functionality, but additional custom processing, validation, calculations, or business rules are needed. This approach reduces development effort while maintaining flexibility and scalability.
Understanding Controller Extensions is essential for Salesforce Developers because they provide a powerful way to customize Visualforce applications without replacing Standard Controller functionality.
A Controller Extension is an Apex class that adds extra functionality to an existing Controller.
Extensions allow developers to:
Extensions work alongside existing Controllers.
Without Extensions:
Standard Controller
↓
Limited Customization
With Extensions:
Standard Controller
+
Custom Logic
↓
Enhanced Functionality
Extensions provide flexibility while preserving built-in features.
Leverage built-in Salesforce features.
Avoid rewriting existing logic.
Support complex requirements.
Keep code organized.
Support large applications.
These benefits make Extensions highly valuable.
Standard Controllers provide:
However, they cannot always handle:
Extensions solve these limitations.
Visualforce Page:
Visualforce Page
↓
Standard Controller
↓
Controller Extension
↓
Custom Logic
This architecture combines standard and custom functionality.
public class
AccountExtension{
}
This is a simple Extension class.
Every Extension requires a constructor.
Example:
public class
AccountExtension{
private Account
accountRecord;
public
AccountExtension(
ApexPages.StandardController
controller){
accountRecord =
(Account)
controller.getRecord();
}
}
The constructor receives the Standard Controller.
Example:
ApexPages.StandardController
controller
This object provides access to:
The Extension can interact with the active record.
Visualforce Page:
<apex:page
standardController=
"Account"
extensions=
"AccountExtension">
</apex:page>
The page now uses both:
Example:
public
AccountExtension(
ApexPages.StandardController
controller){
accountRecord =
(Account)
controller.getRecord();
}
The current Account record becomes available.
Example:
public String
message =
'Welcome Student';
Visualforce pages can access Extension variables.
Extension:
public String
courseName =
'Salesforce';
Visualforce Page:
<apex:outputText
value="{!courseName}"/>
Output:
Salesforce
Data binding works the same as Controllers.
Extensions can contain custom methods.
Example:
public String
getMessage(){
return
'Welcome to Training';
}
Methods provide dynamic functionality.
Visualforce Page:
<apex:outputText
value="{!message}"/>
Output:
Welcome to Training
The method executes automatically.
Example:
public PageReference
processEnrollment(){
return null;
}
These methods execute when users interact with the page.
Visualforce Page:
<apex:commandButton
value="Enroll"
action="{!processEnrollment}"/>
When clicked:
processEnrollment()
is executed.
Business Requirement:
Calculate course fee with discount.
Extension:
public Decimal
calculateFee(){
Decimal fee =
10000;
Decimal discount =
1000;
return fee -
discount;
}
The Extension performs custom calculations.
Visualforce Page:
<apex:outputText
value="{!calculateFee}"/>
Output:
9000
Business logic is displayed dynamically.
Extension:
public Boolean
validateAge(
Integer age){
return age >= 18;
}
This supports custom validation rules.
Extensions can use SOQL.
Example:
List<Student__c>
students =
[
SELECT Id,
Name
FROM Student__c
];
Extensions can retrieve Salesforce data.
Example:
public void
updateStudent(){
update studentRecord;
}
Extensions can perform DML operations.
Visualforce supports multiple Extensions.
Example:
<apex:page
standardController=
"Account"
extensions=
"AccountExtension,
StudentExtension">
</apex:page>
Multiple Extensions improve modularity.
| Feature | Standard Controller | Extension |
|---|---|---|
| Built-In CRUD | Yes | Uses Controller |
| Custom Logic | Limited | Yes |
| Apex Development | No | Yes |
| Business Rules | Limited | Advanced |
Extensions provide flexibility.
User Opens Page
↓
Standard Controller Loads
↓
Extension Loads
↓
Custom Logic Executes
↓
Page Displays Results
This flow drives Extension behavior.
A software training institute needs:
A Standard Controller manages records while an Extension performs custom calculations and validations.
This demonstrates practical usage.
These practices improve maintainability.
Developers should avoid these issues.
These advantages make Extensions a preferred solution.
Developers should understand these considerations.
Understanding Controller Extensions helps professionals:
Extensions are widely used in enterprise Visualforce projects.
Controller Extensions allow developers to enhance Standard Controllers by adding custom business logic, validation, calculations, database operations, and user interactions. Through constructors, methods, data binding, action processing, and Apex integration, Extensions provide a flexible and scalable way to customize Visualforce applications while retaining Salesforce’s built-in functionality. Mastering Controller Extensions is essential for advanced Visualforce development.
A Controller Extension is an Apex class that adds custom functionality to a Standard Controller.
They allow developers to add custom logic while reusing built-in Salesforce functionality.
Yes. Extensions can access records through the Standard Controller.
Yes. Extensions are written as Apex classes.
Yes. Visualforce supports multiple Controller Extensions.
They provide a balance between built-in functionality and custom business requirements.
Looking to learn more technologies and programming skills?
Send us your inquiry and start your journey towards a successful IT career.
✔ 100% Secure ✔ No Spam ✔ Instant Response
WhatsApp us