{"id":2408,"date":"2026-10-21T13:00:00","date_gmt":"2026-10-21T07:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/"},"modified":"2026-11-15T11:29:55","modified_gmt":"2026-11-15T05:59:55","slug":"visualforce-custom-controllers-extensions","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/","title":{"rendered":"Visualforce Custom Controllers and Controller Extensions Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Custom Controllers give a Visualforce page its own Apex class, so you decide what data it loads and what its buttons do. A standard controller handles one record, but a dashboard page that joins three objects needs real code behind it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/visualforce-basics-standard-controllers\/\">Visualforce Basics<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-sharing-keywords-with-without-inherited\/\">Apex sharing keywords<\/a>. We will build an enrolment summary page for Bright Path Tutors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Custom Controllers?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Custom Controllers are Apex classes attached to a Visualforce page with the <code>controller<\/code> attribute. The class supplies data through getters and handles clicks through action methods. Nothing is provided for you, so you write the queries and the save logic yourself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a restaurant. A standard controller is the set menu, already planned. A custom controller is the kitchen, where you decide every dish.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Custom Controllers Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Standard controllers only understand one object and one record. Many real pages need totals across several objects, a filtered list or a wizard spanning multiple steps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Custom Controllers handle all of that. Extensions sit in between, letting you keep the standard behaviour and add just the extra you need.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Custom Controllers Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The page and the controller talk in a cycle on every request.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-1-1024x640.png\" alt=\"Custom Controllers diagram showing the page and controller cycle\" class=\"wp-image-2406\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How a page and its controller work together.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">A Basic Custom Controller<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class CourseListController {\n    public List&lt;Course__c&gt; courses { get; private set; }\n    public String keyword { get; set; }\n\n    public CourseListController() {\n        loadCourses();\n    }\n\n    public PageReference search() {\n        loadCourses();\n        return null;\n    }\n\n    private void loadCourses() {\n        String like = '%' + String.escapeSingleQuotes(\n            keyword == null ? '' : keyword) + '%';\n        courses = [SELECT Id, Name, Fee__c FROM Course__c\n                   WHERE Name LIKE :like ORDER BY Name LIMIT 100];\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The constructor runs once when the page loads. The search method runs when a button calls it, and returning <code>null<\/code> keeps the user on the same page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Matching Page<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;apex:page controller=\"CourseListController\"&gt;\n    &lt;apex:form&gt;\n        &lt;apex:pageBlock title=\"Courses\"&gt;\n            &lt;apex:inputText value=\"{!keyword}\"\/&gt;\n            &lt;apex:commandButton action=\"{!search}\" value=\"Search\" reRender=\"results\"\/&gt;\n            &lt;apex:pageBlockTable value=\"{!courses}\" var=\"c\" id=\"results\"&gt;\n                &lt;apex:column value=\"{!c.Name}\"\/&gt;\n                &lt;apex:column value=\"{!c.Fee__c}\"\/&gt;\n            &lt;\/apex:pageBlockTable&gt;\n        &lt;\/apex:pageBlock&gt;\n    &lt;\/apex:form&gt;\n&lt;\/apex:page&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>reRender<\/code> updates only the table, which is faster than reloading the whole page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Controller Extensions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An extension keeps the standard controller and adds to it. Its constructor must take a <code>StandardController<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class CourseExtension {\n    private final Course__c course;\n    public Integer seatsTaken { get; private set; }\n\n    public CourseExtension(ApexPages.StandardController c) {\n        this.course = (Course__c) c.getRecord();\n        seatsTaken = [SELECT COUNT() FROM Enrolment__c\n                      WHERE Course__c = :course.Id];\n    }\n\n    public PageReference closeCourse() {\n        course.Status__c = 'Closed';\n        update course;\n        return null;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The page then names both.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;apex:page standardController=\"Course__c\" extensions=\"CourseExtension\"&gt;\n    &lt;apex:outputText value=\"Seats taken: {!seatsTaken}\"\/&gt;\n    &lt;apex:form&gt;\n        &lt;apex:commandButton action=\"{!save}\" value=\"Save\"\/&gt;\n        &lt;apex:commandButton action=\"{!closeCourse}\" value=\"Close course\"\/&gt;\n    &lt;\/apex:form&gt;\n&lt;\/apex:page&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both the standard <code>save<\/code> and your <code>closeCourse<\/code> work on the same page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Showing Messages to the User<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public PageReference closeCourse() {\n    if (seatsTaken &gt; 0) {\n        ApexPages.addMessage(new ApexPages.Message(\n            ApexPages.Severity.ERROR, 'Cannot close a course with enrolments.'));\n        return null;\n    }\n    course.Status__c = 'Closed';\n    update course;\n    return null;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add <code>apex:pageMessages<\/code> to the page and the error appears in the standard Salesforce style. Because you returned null, the user stays on the page with their input intact. That is the normal pattern for a validation failure, since sending them away would lose their work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Controller Member Reference<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This table shows what each piece does.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Piece<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td>Constructor<\/td><td>Runs once on page load<\/td><\/tr><tr><td>Property with get<\/td><td>Supplies a value to the page<\/td><\/tr><tr><td>Action method<\/td><td>Runs when a button is clicked<\/td><\/tr><tr><td>PageReference return<\/td><td>Sends the user elsewhere, or null to stay<\/td><\/tr><tr><td>transient keyword<\/td><td>Keeps a variable out of view state<\/td><\/tr><tr><td>ApexPages.addMessage<\/td><td>Shows a message on the page<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Custom Controller vs Extension<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This table shows when each fits.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Custom controller<\/th><th>Extension<\/th><\/tr><\/thead><tbody><tr><td>Standard actions<\/td><td>None<\/td><td>Inherited<\/td><\/tr><tr><td>Record supplied<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Constructor argument<\/td><td>None<\/td><td>StandardController<\/td><\/tr><tr><td>Page attribute<\/td><td>controller<\/td><td>extensions<\/td><\/tr><tr><td>Best for<\/td><td>Pages with their own data<\/td><td>Adding to a record page<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-2-1024x640.png\" alt=\"Custom Controllers compared with controller extensions\" class=\"wp-image-2407\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Custom controller compared with an extension.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, use an extension when the page is still about one record. Use a full controller when it is not.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Custom Controllers Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Bright Path wants a page showing one course with its enrolments and a running total of fees collected.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Extension<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class CourseSummaryExt {\n    private final Course__c course;\n    public List&lt;Enrolment__c&gt; enrolments { get; private set; }\n    public Decimal collected { get; private set; }\n\n    public CourseSummaryExt(ApexPages.StandardController sc) {\n        course = (Course__c) sc.getRecord();\n        enrolments = [SELECT Id, Student_Name__c, Amount_Paid__c\n                      FROM Enrolment__c\n                      WHERE Course__c = :course.Id\n                      ORDER BY Student_Name__c\n                      LIMIT 500];\n        collected = 0;\n        for (Enrolment__c e : enrolments) {\n            collected += (e.Amount_Paid__c == null) ? 0 : e.Amount_Paid__c;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The total is worked out once in the constructor, not in a getter, so it does not repeat on every render.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: The Page<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;apex:page standardController=\"Course__c\" extensions=\"CourseSummaryExt\"&gt;\n    &lt;apex:pageMessages\/&gt;\n    &lt;apex:pageBlock title=\"{!Course__c.Name}\"&gt;\n        &lt;apex:outputText value=\"Collected so far: {!collected}\"\/&gt;\n        &lt;apex:pageBlockTable value=\"{!enrolments}\" var=\"e\"&gt;\n            &lt;apex:column value=\"{!e.Student_Name__c}\"\/&gt;\n            &lt;apex:column value=\"{!e.Amount_Paid__c}\"\/&gt;\n        &lt;\/apex:pageBlockTable&gt;\n    &lt;\/apex:pageBlock&gt;\n&lt;\/apex:page&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Test It<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tests must set the page context, which is the part people forget.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@isTest\nprivate class CourseSummaryExtTest {\n    @isTest static void totalsPaidAmounts() {\n        Course__c c = new Course__c(Name = 'Python Basics');\n        insert c;\n        insert new Enrolment__c(Course__c = c.Id, Amount_Paid__c = 5000);\n\n        Test.setCurrentPage(Page.CourseSummary);\n        ApexPages.StandardController sc = new ApexPages.StandardController(c);\n        CourseSummaryExt ext = new CourseSummaryExt(sc);\n\n        System.assertEquals(5000, ext.collected);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These are the pieces a controller test needs.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Step<\/th><th>Why<\/th><\/tr><\/thead><tbody><tr><td>Test.setCurrentPage<\/td><td>Gives the test a page context<\/td><\/tr><tr><td>new StandardController(record)<\/td><td>Supplies the record<\/td><\/tr><tr><td>new Extension(sc)<\/td><td>Runs the constructor<\/td><\/tr><tr><td>Call action methods<\/td><td>Covers the button logic<\/td><\/tr><tr><td>Assert on properties<\/td><td>Proves the data is right<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Custom Controllers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Querying inside a getter, which repeats on every render.<\/li>\n\n<li>Holding large lists in the controller, which blows the view state limit.<\/li>\n\n<li>Forgetting with sharing, so the page shows records the user should not see.<\/li>\n\n<li>Returning a PageReference when you meant to stay on the page.<\/li>\n\n<li>Testing the class without setting the page context.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Custom Controllers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Declare with sharing unless you have a reason not to.<\/li>\n\n<li>Load data in the constructor and store it in a property.<\/li>\n\n<li>Mark large or temporary variables transient.<\/li>\n\n<li>Use an extension when the standard behaviour is still useful.<\/li>\n\n<li>Keep business logic in a service class, so it is testable without a page.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write an extension for Course that counts enrolments and exposes the number as a property. Hint: the constructor must accept an ApexPages.StandardController.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Custom Controllers Interview Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Custom Controllers questions appear whenever an interviewer covers Visualforce.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Custom Controllers Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is a custom controller?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Custom Controllers are Apex classes attached to a Visualforce page, supplying all its data and actions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: What is a controller extension?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An Apex class that adds methods to a standard controller while keeping its built-in behaviour.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What argument does an extension constructor take?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An ApexPages.StandardController, or a StandardSetController for list pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: Which page attribute attaches each one?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">controller for a custom controller and extensions for one or more extensions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Can a page have several extensions?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. List them comma separated, and the first matching method wins.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: What does an action method return?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A PageReference that sends the user to another page, or null to stay on the same one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: How do you show an error on the page?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call ApexPages.addMessage and include apex:pageMessages in the markup.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: When does the constructor run?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once when the page is first loaded, before any getters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Intermediate Custom Controllers Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: Do custom controllers enforce sharing?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not by default. They run in system mode, so declare with sharing to respect record access.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Do extensions enforce sharing?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The standard controller still checks record access, but your extension code follows the class sharing declaration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: What is view state and why does it matter?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is the data kept between postbacks, capped at 170 KB. Large collections in the controller cause errors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How do you reduce view state?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mark variables transient, query fewer fields and avoid storing big lists.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: Why should a getter not contain SOQL?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because getters can run several times per render, repeating the query each time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: What is reRender used for?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It refreshes only part of the page after an action, which is faster than a full reload.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: How do you read a URL parameter?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With ApexPages.currentPage().getParameters().get(&#8216;name&#8217;).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: What does StandardSetController give you?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">List handling with paging, selection and mass actions for an extension on a list page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Custom Controllers Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Scenario: a page errors with Maximum view state size exceeded. What do you do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mark unneeded fields transient, trim the query to needed columns, and move heavy work to JavaScript Remoting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: What is JavaScript Remoting and when is it better?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A static Apex method called from JavaScript with no view state. It suits large data and responsive pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How would you build a multi-step wizard?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hold the in-progress record in the controller, return a PageReference per step, and only commit DML at the end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Why keep logic out of the controller?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because a service class can be tested without a page context and reused by triggers, Flow and LWC.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: Scenario: a user sees records they should not. What is the likely cause?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The controller is declared without sharing, or no sharing declaration at all, so it runs in system mode.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q22: How do you migrate a controller to support LWC?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Move the queries into static @AuraEnabled methods, then call them from the component and retire the page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q23: What happens to controller state between requests?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Non-transient member variables are serialised into view state and restored on the next postback.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q24: How do you test an action method properly?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Set the page, build the controller, call the method, then assert on the returned PageReference and the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q25: Can Custom Controllers be used by more than one page?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Several pages can name the same class, which is handy for a shared search screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q26: How do Custom Controllers handle bulk data?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Query with a LIMIT and page the results. Because everything sits in view state, large lists quickly break the page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q27: Scenario: a button works for admins but fails for users. What do you check?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The sharing declaration first, then field level security on every field the page writes. System mode hides those problems from you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q28: Do Custom Controllers work with Lightning Experience?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, if the page is marked available for Lightning Experience. Still, the styling may need work to look right.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q29: What is the first thing to check when a page shows stale data?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whether the value is cached in a property set in the constructor. So an action method must refresh it explicitly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q30: Should new projects use Custom Controllers?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Only for Visualforce work that must stay. Otherwise build in LWC, because that is where Salesforce invests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.pages.meta\/pages\/pages_controller_custom.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Visualforce Guide: Custom Controllers and Extensions<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.pages.meta\/pages\/pages_view_state.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Visualforce Guide: View State<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexref.meta\/apexref\/apex_namespace_ApexPages.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Reference: ApexPages Namespace<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Custom Controllers supply all the data and actions for a page.<\/li>\n\n<li>Extensions add to a standard controller and keep its behaviour.<\/li>\n\n<li>Load data in the constructor, never in a getter.<\/li>\n\n<li>Declare with sharing, since controllers default to system mode.<\/li>\n\n<li>Watch view state and mark large variables transient.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we produce documents with <a href=\"https:\/\/swapnilzambare.com\/blog\/visualforce-pdf-lightning\/\">Visualforce to PDF and Visualforce in Lightning<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Custom Controllers give a Visualforce page its own Apex class, so you decide what data it loads and what its buttons do. A standard&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2405,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[245,8,96],"tags":[68,98,254],"swapz_series":[],"class_list":["post-2408","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lightning-web-components","category-salesforce","category-salesforce-developer","tag-advanced","tag-apex","tag-aura-and-visualforce"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visualforce Custom Controllers and Extensions<\/title>\n<meta name=\"description\" content=\"Learn Visualforce Custom Controllers and controller extensions, constructors, action methods, sharing and view state, plus top interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visualforce Custom Controllers and Extensions\" \/>\n<meta property=\"og:description\" content=\"Learn Visualforce Custom Controllers and controller extensions, constructors, action methods, sharing and view state, plus top interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-21T07:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T05:59:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Visualforce Custom Controllers and Controller Extensions Explained\",\"datePublished\":\"2026-10-21T07:30:00+00:00\",\"dateModified\":\"2026-11-15T05:59:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/\"},\"wordCount\":1552,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/visualforce-custom-controllers-extensions-featured.png\",\"keywords\":[\"Advanced\",\"Apex\",\"Aura and Visualforce\"],\"articleSection\":[\"Lightning Web Components\",\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/\",\"name\":\"Visualforce Custom Controllers and Extensions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/visualforce-custom-controllers-extensions-featured.png\",\"datePublished\":\"2026-10-21T07:30:00+00:00\",\"dateModified\":\"2026-11-15T05:59:55+00:00\",\"description\":\"Learn Visualforce Custom Controllers and controller extensions, constructors, action methods, sharing and view state, plus top interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/visualforce-custom-controllers-extensions-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/visualforce-custom-controllers-extensions-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Custom Controllers featured image: code editor card with controller constructor\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/visualforce-custom-controllers-extensions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visualforce Custom Controllers and Controller Extensions Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visualforce Custom Controllers and Extensions","description":"Learn Visualforce Custom Controllers and controller extensions, constructors, action methods, sharing and view state, plus top interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/","og_locale":"en_US","og_type":"article","og_title":"Visualforce Custom Controllers and Extensions","og_description":"Learn Visualforce Custom Controllers and controller extensions, constructors, action methods, sharing and view state, plus top interview questions.","og_url":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-21T07:30:00+00:00","article_modified_time":"2026-11-15T05:59:55+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Visualforce Custom Controllers and Controller Extensions Explained","datePublished":"2026-10-21T07:30:00+00:00","dateModified":"2026-11-15T05:59:55+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/"},"wordCount":1552,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-featured.png","keywords":["Advanced","Apex","Aura and Visualforce"],"articleSection":["Lightning Web Components","Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/","url":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/","name":"Visualforce Custom Controllers and Extensions","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-featured.png","datePublished":"2026-10-21T07:30:00+00:00","dateModified":"2026-11-15T05:59:55+00:00","description":"Learn Visualforce Custom Controllers and controller extensions, constructors, action methods, sharing and view state, plus top interview questions.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/visualforce-custom-controllers-extensions-featured.png","width":2400,"height":1260,"caption":"Custom Controllers featured image: code editor card with controller constructor"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/visualforce-custom-controllers-extensions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Visualforce Custom Controllers and Controller Extensions Explained"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=2408"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2408\/revisions"}],"predecessor-version":[{"id":3200,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2408\/revisions\/3200"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2405"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2408"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}