Curriculum
Controllers in Visualforce are the backbone of Visualforce applications. A Controller acts as the bridge between a Visualforce page and Salesforce data. It manages user interactions, retrieves records, processes business logic, performs database operations, and controls what information is displayed on the page.
In the Model-View-Controller (MVC) architecture used by Visualforce, the Controller is responsible for coordinating communication between the user interface and the Salesforce database. Controllers enable developers to build dynamic, interactive, and data-driven applications.
Understanding Controllers is essential for Salesforce Developers because every Visualforce application relies on a Controller to process user requests and manage application behavior.
A Controller is a class that contains logic used by a Visualforce page.
A Controller can:
Controllers control page functionality.
Without Controllers:
Visualforce Page
↓
No Data Access
↓
No Business Logic
↓
Static Page
Controllers make pages dynamic and interactive.
Access Salesforce records.
Process user requests.
Display changing data.
Support maintainable applications.
Handle complex business requirements.
These benefits make Controllers essential.
Salesforce provides three controller types.
Each serves a different purpose.
A Standard Controller is a Salesforce-provided controller that automatically supports standard operations for an object.
Features:
No Apex code is required.
<apex:page
standardController=
"Account">
</apex:page>
Salesforce automatically manages Account records.
Standard Controllers simplify application development.
Visualforce Page:
<apex:page
standardController=
"Account">
<apex:outputField
value="{!Account.Name}"/>
</apex:page>
Output:
Groot Software
Salesforce retrieves data automatically.
Built-in Actions:
These actions require minimal development effort.
<apex:commandButton
value="Save"
action="{!save}"/>
The save action is provided automatically.
A Custom Controller is an Apex class written by developers.
Custom Controllers provide complete control over page behavior.
Example:
public class
StudentController{
}
Custom Controllers support advanced business requirements.
Organizations often require:
Custom Controllers provide this flexibility.
Example:
public class
StudentController{
public String
studentName =
'Rahul Sharma';
}
The Controller contains application logic.
Visualforce Page:
<apex:page
controller=
"StudentController">
</apex:page>
The page now uses the custom Controller.
Data Binding connects Visualforce components to Controller variables.
Controller:
public String
studentName =
'Salesforce Student';
Page:
<apex:outputText
value="{!studentName}"/>
Output:
Salesforce Student
Data updates automatically.
Example:
public String course =
'Salesforce';
Visualforce can display this value directly.
Variables store page data.
A Method performs an action.
Example:
public void
showMessage(){
}
Methods can be called from Visualforce pages.
Controller:
public class
StudentController{
public String
getMessage(){
return
'Welcome Student';
}
}
Page:
<apex:outputText
value="{!message}"/>
Output:
Welcome Student
Methods provide dynamic data.
Action Methods execute when users interact with the page.
Example:
public PageReference
saveStudent(){
return null;
}
These methods respond to user actions.
Visualforce Page:
<apex:commandButton
value="Save"
action="{!saveStudent}"/>
When clicked:
saveStudent()
executes automatically.
Controllers can retrieve records using SOQL.
Example:
public List<Student__c>
students {
get{
return
[
SELECT Id,
Name
FROM Student__c
];
}
}
The Controller retrieves Student records.
Visualforce Page:
<apex:dataTable
value="{!students}"
var="student">
</apex:dataTable>
Data appears dynamically.
Example:
public void
createStudent(){
Student__c student =
new Student__c();
insert student;
}
Controllers can create records.
Example:
public void
updateStudent(){
update studentRecord;
}
Controllers can modify records.
Example:
public void
deleteStudent(){
delete studentRecord;
}
Controllers can remove records.
A Constructor executes automatically when the Controller loads.
Example:
public class
StudentController{
public
StudentController(){
}
}
Constructors initialize data.
public
StudentController(){
studentName =
'Rahul Sharma';
}
The value is assigned automatically.
Controllers can navigate between pages.
Example:
public PageReference
goToHome(){
PageReference page =
Page.HomePage;
return page;
}
Navigation becomes dynamic.
User Opens Page
↓
Controller Loads
↓
Data Retrieved
↓
Page Displays Data
↓
User Action
↓
Controller Method Executes
This flow drives Visualforce applications.
| Feature | Standard Controller | Custom Controller |
|---|---|---|
| Built-In Operations | Yes | No |
| Custom Logic | Limited | Full Control |
| Development Speed | Fast | Moderate |
| Flexibility | Limited | High |
Both approaches are valuable.
A software training institute needs:
A Custom Controller can handle all business logic while Visualforce displays the interface.
This demonstrates practical usage.
These practices improve maintainability.
Developers should avoid these issues.
Understanding Controllers helps professionals:
Controllers are a core Visualforce concept.
Controllers are responsible for managing data, business logic, and user interactions in Visualforce applications. Through Standard Controllers, Custom Controllers, methods, variables, SOQL queries, and data binding, developers can create dynamic, data-driven Salesforce applications. Mastering Controllers is essential for professional Visualforce development and enterprise Salesforce projects.
A Controller manages data, business logic, and user interactions for a Visualforce page.
Standard Controllers, Custom Controllers, and Controller Extensions.
A Salesforce-provided controller with built-in CRUD functionality.
An Apex class that provides custom page behavior and business logic.
Data Binding connects Visualforce components to Controller variables and methods.
Controllers enable Visualforce pages to interact with Salesforce data and perform business operations.
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