fbpx

Free Salesforce Developers Tutorials >

Chapter 15 - Visualforce >

Using Controller Extension with Custom Controller

Using Controller Extension with Custom Controller

What You’ll Learn


S2 Labs

Controller Extension With Custom Controller

The way extentions are used with standard controller, the same way standard controller can be used with standard extensions.

Code Example Of Controller Extension With Custom Controller

Here’s a sample code example for a Visualforce page with a standard controller and multiple controller extensions in Apex:

Visualforce Page (samplePage.page):

<apex:page controller="Account" extensions="SampleExtension1, SampleExtension2">
    <h1>Welcome to the Sample Page</h1>
    
    <apex:outputText value="Account Name: {!accountName}" />
    <apex:outputText value="Contact Name: {!contactName}" />
</apex:page>

Controller Extension 1 (SampleExtension1.cls):

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

Controller Extension 2 (SampleExtension2.cls):

public class SampleExtension2 {
    private ApexPages.StandardController stdController;
    
    public SampleExtension2(ApexPages.StandardController controller) {
        stdController = controller;
    }
    
    public String getContactName() {
        // Accessing the standard controller's record
        Contact con = (Contact)stdController.getRecord();
        return con.Name;
    }
}
salesforce-developer

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *