Visualforce Controllers: Standard, Custom, and Extensions
Visualforce Controllers are the backbone of business logic in Visualforce pages, managing data and user interactions. There are three types: Standard Controllers, Custom Controllers, and Controller Extensions. **Standard Controllers:** Salesforce provides built-in standard controllers for every st… Visualforce Controllers are the backbone of business logic in Visualforce pages, managing data and user interactions. There are three types: Standard Controllers, Custom Controllers, and Controller Extensions. **Standard Controllers:** Salesforce provides built-in standard controllers for every standard and custom object (e.g., Account, Contact). They offer out-of-the-box functionality such as saving, editing, deleting, and viewing records without writing any Apex code. You reference them using the `standardController` attribute: `<apex:page standardController="Account">`. Standard controllers automatically handle record access based on user permissions and sharing rules. They also support standard list controllers for working with sets of records. **Custom Controllers:** Custom controllers are Apex classes written from scratch to define all the logic for a Visualforce page. They are used when standard controller functionality is insufficient. Declared using the `controller` attribute: `<apex:page controller="MyCustomController">`. Custom controllers run in system mode by default, meaning they don't enforce field-level security or object permissions automatically — developers must handle security explicitly. They provide complete flexibility to implement complex business logic, call external services, or perform custom DML operations. **Controller Extensions:** Controller extensions augment either standard or custom controllers by adding additional functionality. They are Apex classes that extend existing controller behavior without replacing it. Declared using the `extensions` attribute: `<apex:page standardController="Account" extensions="MyExtension">`. The extension class constructor must accept an `ApexPages.StandardController` (or the custom controller) as a parameter. Multiple extensions can be comma-separated, with method resolution following left-to-right precedence. Extensions are ideal for adding custom methods, overriding standard actions, or introducing new properties while retaining standard controller benefits like record context and navigation. **Key Exam Points:** - Standard controllers enforce sharing rules; custom controllers run in system mode. - You cannot use `standardController` and `controller` attributes simultaneously. - Extensions can be used with both standard and custom controllers. - Method resolution in multiple extensions follows left-to-right order, with the controller being the last fallback.
Visualforce Controllers: Standard, Custom, and Extensions – Complete Guide for Salesforce Platform Developer 1
Why Are Visualforce Controllers Important?
Visualforce controllers are the backbone of business logic in Visualforce pages. They determine how data is retrieved, manipulated, and saved. Understanding controllers is critical for the Salesforce Platform Developer 1 exam because questions frequently test your ability to distinguish between Standard Controllers, Custom Controllers, and Controller Extensions, and to know when and why you would use each one. Controllers connect the View (Visualforce page) to the Model (Salesforce data), making them essential for any developer building custom user interfaces on the Salesforce platform.
What Are Visualforce Controllers?
A Visualforce controller is an Apex class or built-in Salesforce functionality that provides the data and actions available to a Visualforce page. There are three types:
1. Standard Controllers
Standard controllers are provided out-of-the-box by Salesforce for every standard and custom object. They give you automatic access to the same functionality available in standard Salesforce pages, such as saving, editing, deleting, and viewing records.
Key characteristics:
- No Apex code is required.
- Automatically provides getter methods for all fields on the associated object.
- Supports standard actions like save(), edit(), delete(), cancel(), and view().
- Used by specifying the standardController attribute on the <apex:page> tag, e.g., <apex:page standardController="Account">.
- Respects the user's CRUD permissions, field-level security, and sharing rules.
- The record ID is typically passed via the URL parameter id.
- Standard List Controllers allow you to work with sets of records (e.g., recordSetVar="accounts").
2. Custom Controllers
Custom controllers are Apex classes that you write from scratch to define all the logic, data access, and navigation for a Visualforce page. They replace the standard controller entirely.
Key characteristics:
- Defined using the controller attribute on the <apex:page> tag, e.g., <apex:page controller="MyCustomController">.
- You must write all getter and setter methods yourself.
- You must manually implement any save, edit, delete, or navigation actions.
- Custom controllers run in system mode by default — meaning they do NOT automatically enforce the running user's CRUD, FLS, or sharing rules unless you explicitly use with sharing keyword on the class.
- Useful when you need logic that does not map directly to a single standard object or when complex business processes are required.
- You cannot use both a standardController and a controller attribute on the same page.
- The constructor takes no arguments.
3. Controller Extensions
Controller extensions are Apex classes that extend the functionality of either a standard controller or a custom controller. They allow you to add new methods and override existing ones without completely replacing the controller.
Key characteristics:
- Defined using the extensions attribute on the <apex:page> tag, e.g., <apex:page standardController="Account" extensions="MyExtension">.
- Multiple extensions can be specified in a comma-separated list. When methods conflict, the leftmost extension wins.
- The constructor for an extension that extends a standard controller takes a ApexPages.StandardController parameter.
- The constructor for an extension that extends a custom controller takes the custom controller class as a parameter.
- Extensions are ideal when you want to leverage standard controller functionality (such as save, edit, CRUD enforcement) but also need additional custom logic.
- Extensions also run in system mode by default unless the with sharing keyword is used.
- Extensions can be used with Standard List Controllers by accepting ApexPages.StandardSetController in the constructor.
How Do Visualforce Controllers Work?
When a Visualforce page loads:
1. Salesforce identifies which controller or extension is referenced on the <apex:page> tag.
2. The controller or extension class is instantiated. For standard controllers, Salesforce auto-instantiates the built-in controller. For extensions, the standard or custom controller is passed into the extension's constructor.
3. The page's merge fields and expressions are evaluated. Getter methods in the controller or extension return data to the page.
4. When a user performs an action (e.g., clicks a button bound to <apex:commandButton>), the corresponding action method in the controller or extension is invoked.
5. After the action method executes, the view state is updated and the page is re-rendered.
Example — Standard Controller:
<apex:page standardController="Contact">
<apex:form>
<apex:inputField value="{!Contact.FirstName}"/>
<apex:commandButton action="{!save}" value="Save"/>
</apex:form>
</apex:page>
Example — Custom Controller:
public class MyCustomController {
public String greeting { get; set; }
public MyCustomController() {
greeting = 'Hello World';
}
public PageReference doSomething() {
// custom logic
return null;
}
}
Page: <apex:page controller="MyCustomController">
Example — Controller Extension:
public class MyExtension {
private final Account acct;
public MyExtension(ApexPages.StandardController stdController) {
this.acct = (Account)stdController.getRecord();
}
public String getCustomField() {
return 'Extended: ' + acct.Name;
}
}
Page: <apex:page standardController="Account" extensions="MyExtension">
Key Differences Summary Table
| Feature | Standard Controller | Custom Controller | Controller Extension |
|---|---|---|---|
| Apex Code Required | No | Yes | Yes |
| Attribute Used | standardController | controller | extensions |
| Runs In | User Mode | System Mode | System Mode |
| Constructor Parameter | N/A | None | StandardController or Custom Controller |
| Can Override Standard Actions | No | N/A | Yes |
| Multiple Allowed on Page | No | No (only one) | Yes (comma-separated) |
Exam Tips: Answering Questions on Visualforce Controllers
Tip 1: Know the Attribute Names
The exam will test whether you know that standardController is used for standard controllers, controller is used for custom controllers, and extensions is used for extensions. Remember: you cannot use standardController and controller on the same page simultaneously.
Tip 2: Understand System Mode vs. User Mode
Standard controllers run in user mode, enforcing CRUD, FLS, and sharing rules automatically. Custom controllers and extensions run in system mode by default. If a question asks about security enforcement, check whether the class uses the with sharing keyword.
Tip 3: Constructor Signatures Matter
If the exam presents code, check the constructor. A standard controller extension constructor must accept an ApexPages.StandardController parameter. A custom controller constructor takes no parameters. An extension for a custom controller takes the custom controller type as a parameter. Getting this wrong will result in a compile-time error.
Tip 4: Extension Method Resolution Order
When multiple extensions are listed, and a method exists in more than one, the leftmost extension takes priority. If neither extension has the method, the standard or custom controller's method is used. This is a commonly tested concept.
Tip 5: When to Use Which
- Use a Standard Controller when you need basic CRUD operations on a single object and don't need custom logic.
- Use a Controller Extension when you need the standard controller functionality PLUS additional custom logic.
- Use a Custom Controller when your page's logic is entirely custom and doesn't map to a single object's standard operations.
Tip 6: Standard List Controllers
Know that standard list controllers use recordSetVar to work with collections. Extensions for list controllers accept ApexPages.StandardSetController in their constructor. Expect questions that test your understanding of pagination methods like next(), previous(), getHasNext(), and getHasPrevious().
Tip 7: getRecord() and addFields()
In extensions, stdController.getRecord() returns only the fields referenced on the Visualforce page. If you need additional fields in your extension logic that are NOT on the page, you must call stdController.addFields(List<String>) in the constructor before calling getRecord(). This is a nuanced concept that appears on the exam.
Tip 8: Transient Keyword and View State
Remember that the transient keyword prevents a variable from being included in the view state. If a question discusses view state size issues, look for answers involving the transient keyword or reducing the data stored in controller properties.
Tip 9: Page References and Navigation
Action methods that return PageReference control navigation. Returning null refreshes the current page. Know how to construct a PageReference to redirect to another page, and that setRedirect(true) performs a client-side redirect (new view state), while setRedirect(false) performs a server-side forward (retains view state).
Tip 10: Read the Question Carefully
Many exam questions about controllers present a scenario and ask which approach is best. Focus on whether the scenario requires standard functionality (standard controller), additional custom behavior on top of standard functionality (extension), or entirely custom behavior (custom controller). Eliminate answers that mix incompatible attributes like using both standardController and controller on the same page.
Final Summary
Mastering Visualforce controllers is essential for the Platform Developer 1 exam. Focus on understanding the differences in security context (user mode vs. system mode), constructor signatures, method resolution with multiple extensions, and when to choose each type of controller. Practice identifying the correct approach from scenario-based questions, and always pay attention to the specific attribute names used on the <apex:page> tag.
🎓 Unlock Premium Access
Salesforce Certified Platform Developer I + ALL Certifications
- 🎓 Access to ALL Certifications: Study for any certification on our platform with one subscription
- 2750 Superior-grade Salesforce Certified Platform Developer I practice questions
- Unlimited practice tests across all certifications
- Detailed explanations for every question
- PD1: 5 full exams plus all other certification exams
- 100% Satisfaction Guaranteed: Full refund if unsatisfied
- Risk-Free: 7-day free trial with all premium features!