Salesforce Dictionary — Free Salesforce GlossarySalesforce Dictionary

Apex Controller

Development🔴 Advanced

Definition

An Apex Controller is an Apex class that provides the data and business logic for a Visualforce page. It handles user interactions, executes queries, performs DML operations, and controls page navigation. Salesforce provides Standard Controllers for each object, and developers can write Custom Controllers or Controller Extensions to add custom behavior.

Real-World Example

a senior developer at TerraForm Tech uses Apex Controller to solve a complex business requirement that cannot be addressed with declarative tools alone. They implement Apex Controller with proper error handling, write 98% test coverage, and document the solution for future maintainers. The code passes security review on the first attempt.

Why Apex Controller Matters

An Apex Controller is an Apex class that provides data and behavior to a Visualforce page. It handles user interactions (button clicks, form submissions), runs SOQL queries to fetch records for display, performs DML operations, and controls page navigation. Salesforce provides Standard Controllers automatically for every sObject (so a Visualforce page tied to Account gets record-handling logic for free), and developers can write Custom Controllers or Controller Extensions when they need behavior beyond the defaults.

Controller Extensions are a particularly useful pattern: rather than writing a full Custom Controller, you extend a Standard Controller with additional methods, keeping the built-in Save, Delete, and navigation logic while adding your own features. Apex Controllers respect the running user's field-level security and sharing rules unless explicitly overridden with 'without sharing', which is an important security consideration when building Visualforce pages that handle sensitive data.

How Organizations Use Apex Controller

  • TerraForm TechWrote a Controller Extension for a Visualforce page on the Account object that adds a 'Calculate Risk Score' button. The extension reuses the Standard Controller's Save behavior but adds a custom method that runs their scoring algorithm and writes the result back to the record.
  • CodeBridgeMaintains a legacy Custom Controller that powers a complex wizard UI on a Visualforce page. The controller manages a multi-step state machine in memory and commits everything to the database only on the final step.
  • Quantum LabsBuilt an Apex Controller with careful 'with sharing' declarations to ensure that their customer-facing Visualforce page only shows records the logged-in user is allowed to see. Skipping this would have exposed private data across customer boundaries.

🧠 Test Your Knowledge

1. What is an Apex Controller?

2. Which of these does Salesforce provide automatically for every sObject?

3. What is a Controller Extension?

See something that could be improved?

Suggest an Edit