What is the difference between a standard controller and a custom controller?

Let’s break this down! I’ll explain the difference between a standard controller and a custom controller in Salesforce — it’s a common question for Visualforce development. ?

1. Standard Controller :
What it is :
  • Provided by Salesforce, no Apex code required.
  • Automatically gives you basic operations for standard and custom objects (like View, Edit, Save, and Delete).
When to use :
  • When you only need basic CRUD (Create, Read, Update, Delete) functionality.
  • When you want to leverage built-in validation rules, security, and field-level access.
Example :
<apex:page standardController="Account">
    <h1>{!Account.Name}</h1>
    <apex:form>
        <apex:inputField value="{!Account.Phone}"/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>

* This uses the Account standard controller — Salesforce handles loading the record and saving changes automatically!


2. Custom Controller :
What it is :
  • Written in Apex, giving you complete control over the logic and data.
  • You define the methods (e.g., querying data, handling actions) and business rules.
When to use :
  • When you need custom business logic beyond standard CRUD operations.
  • When you need to work with multiple objects or make complex queries.
  • When you want to bypass standard security (field-level and object-level security are not enforced automatically with custom controllers).

Example :

Apex Controller :

public class MyCustomAccountController {
    public Account acc {get; set;}
    
    public MyCustomAccountController() {
        acc = new Account();
    }
    
    public PageReference saveAccount() {
        insert acc;
        return null;
    }
}

Visualforce Page :

<apex:page controller="MyCustomAccountController">
    <apex:form>
        <apex:inputField value="{!acc.Name}"/>
        <apex:commandButton value="Save" action="{!saveAccount}"/>
    </apex:form>
</apex:page>

* Here, the save logic is handled by the saveAccount method in the custom controller.


3. Controller Extensions
(Bonus!) :
What it is :
  • Hybrid approach — extends a standard controller with custom logic.
  • You get the best of both worlds: standard controller’s built-in functionality + custom logic.
Example :
public class MyAccountExtension {
    private final Account acc;
    public MyAccountExtension(ApexPages.StandardController stdController) {
        this.acc = (Account)stdController.getRecord();
    }
    
    public String getCustomGreeting() {
        return 'Hello, ' + acc.Name + '!';
    }
}

Visualforce Page :
<apex:page standardController="Account" extensions="MyAccountExtension">
    <h1>{!customGreeting}</h1>
</apex:page>
Quick Comparison :
Feature Standard Controller Custom Controller
CRUD Operations Automatic Manual (you write the logic)
Business Logic Limited Fully customizable
Security Enforcement Enforced automatically Must handle manually
Multiple Objects No Yes
Ease of Use Simple, quick to set up More effort, but flexible