Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Full Custom Controller entry
How-to guide

How to build and maintain a Custom Controller

Writing a Custom Controller is standard Apex development with Visualforce-specific patterns for getters, setters, and action methods. The work is mostly about understanding the page lifecycle.

By Dipojjal Chakrabarti · Founder & Editor, Salesforce DictionaryLast updated May 20, 2026

Writing a Custom Controller is standard Apex development with Visualforce-specific patterns for getters, setters, and action methods. The work is mostly about understanding the page lifecycle.

  1. Define the Apex class

    From Setup or Developer Console, create a new Apex class. Make it public. Add public properties for data the page needs, getter methods for computed values, and action methods for button handlers.

  2. Reference the controller from the Visualforce page

    Set the apex:page controller attribute to the class name. Reference properties through the merge syntax and action methods through commandButton actions.

  3. Handle form input

    Add public setter properties for each form field. Salesforce auto-populates them on form submit. Validate values inside the action method, not the setter, to avoid surprise side effects.

  4. Manage view state

    Mark large properties as transient to keep them out of view state. Query results that are needed on the current request but not on the next can stay transient.

  5. Write Apex tests

    Build a test class that instantiates the controller, sets input properties, calls action methods, and asserts state transitions and PageReference returns. Aim for 75 percent code coverage minimum.

  6. Migrate to LWC when feasible

    For pages that get heavy use or face performance issues, rebuild as LWC with an Apex controller called through AuraEnabled. The modern stack is faster and more maintainable.

Gotchas
  • View state is serialized on every page render. Large collections in public properties exhaust the 170 KB view state limit and cause runtime errors.
  • Action methods that perform DML must respect Apex governor limits. The 150 DML statements per transaction limit can be reached fast on bulk operations.
  • Setters fire on form submit in arbitrary order. Do not rely on order-of-setter execution for business logic.
  • Custom Controllers do not work in Lightning Experience the same way as in Classic. Test in both contexts; some behaviors render differently.
  • Custom Controllers cannot be deleted if they are referenced by a Visualforce page. Delete the page references first.

See the full Custom Controller entry

Custom Controller includes the definition, worked example, deep dive, related terms, and a quiz.