Custom Controller
A Custom Controller in Visualforce is an Apex class written entirely from scratch that provides all the data and logic for a Visualforce page, without relying on a Standard Controller.
Definition
A Custom Controller in Visualforce is an Apex class written entirely from scratch that provides all the data and logic for a Visualforce page, without relying on a Standard Controller. Custom Controllers give developers complete control over page behavior, including custom queries, complex business logic, and custom navigation. The custom controller class is specified in the Visualforce page's controller attribute.
In plain English
“A Custom Controller is an Apex class that's written entirely from scratch to power a Visualforce page. Unlike a Standard Controller that comes free with an object, a Custom Controller gives you full control, but you also have to write all the code yourself for loading data, handling button clicks, and navigating.”
Worked example
A developer at Jacaranda Wines writes a Visualforce page for displaying a customer's wine-club history with a custom UI the Standard Controller can't deliver. He writes a Custom Controller class that defines: a public List<ClubShipment__c> property (data the page binds to), a constructor that runs a custom SOQL query joining four objects, and methods like resendShipment() and exportPdf() invoked from page buttons. The Visualforce page references the Custom Controller in its controller= attribute and uses {!shipments} to display data. Custom Controllers are the path when the platform's out-of-box Standard Controller can't model the page's behavior.
Why Custom Controller matters
A Custom Controller in Visualforce is an Apex class that provides all the data and logic for a Visualforce page without relying on a Standard Controller. Custom Controllers give developers complete control over how data is loaded, how user interactions are processed, and how navigation works. The custom controller class is specified in the Visualforce page's controller attribute, and the page can bind to any public method or property on the class.
Custom Controllers are the right choice when a page has fundamentally different behavior than the Standard Controller provides: aggregating data from multiple objects, integrating with external systems, or implementing complex multi-step workflows. They're more code-intensive than Standard Controllers plus Extensions, which is why Extensions are often the better starting point. But for pages where the Standard Controller model doesn't fit (like a search interface, a dashboard, or a multi-record editor), a Custom Controller gives you the freedom to build what you need.
How organizations use Custom Controller
Built a Custom Controller for a Visualforce page that aggregates data from Opportunities, Accounts, and a custom Project object into a single view. No Standard Controller would have worked for the cross-object aggregation.
Wrote a Custom Controller that orchestrates a multi-step wizard. The controller maintains state across steps through member variables and commits everything to the database only on the final step.
Uses Custom Controllers sparingly because Extensions plus Standard Controllers cover most of their needs. The few Custom Controllers they have are well-documented and carefully tested because they're harder to maintain.
Trust & references
Straight from the source - Salesforce's reference material on Custom Controller.
- Building a Custom ControllerSalesforce Developers
Test your knowledge
Q1. What is a Custom Controller?
Q2. When is a Custom Controller the right choice?
Q3. Where is the Custom Controller specified on a Visualforce page?
Discussion
Loading discussion…