Experience Salesforce

Controller Extensions Part 1

What You’ll Learn

  • What Is Controller Extension In Visualforce?

What Is Controller Extension In Visualforce?

Extensions are apex classes used to provide addition functionalities to vf page other than controller.

Code Example Of Controller Extension

Here is the below example code for a standard controller extension class

public class SampleExtension {
    private ApexPages.StandardController stdController;
    public SampleExtension(ApexPages.StandardController controller) {
        stdController = controller;
    }
    
    public String getAccountName() {
        // Accessing the standard controller's record
        Account acc = (Account)stdController.getRecord();
        return acc.Name;
    }
}

Here is the below example code for a vf page having a standard controller and extension

public class SampleExtension {
    private ApexPages.StandardController stdController;
    
    public SampleExtension(ApexPages.StandardController controller) {
        stdController = controller;
    }
    
    public String getAccountName() {
        // Accessing the standard controller's record
        Account acc = (Account)stdController.getRecord();
        return acc.Name;
    }
}

salesforce-developer
Next Topic

Need Extra Support? Our FREE study materials have got you covered.

Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.

cts-img
Rakshabandhan Sale