An Apex class that enhances the functionality of a standard or custom controller is called a controller extender.
A controller extension is a class of APEX that extends the functionality of a custom or standard controller. We use controller extensions:
In This Tutorial, You Will Learn |
As discussed in the previous articles, Extensions has several benefits, it can have standard & custom controllers. In Extension, we can have one standard or custom controller and can have multiple classes. There are various scenarios in which we can use the extension.
We have a business scenario in which the standard & custom extensions should be explained in the Visualforce Page and the DML including in the Visualforce Page also.
public class MyControllerExtensionExample5Feb{
public static string message{get;set;}
public static string VariableForStoringAccountName {get;set;}
public MyControllerExtensionExample5Feb(ApexPages.StandardController stdController) {
//write any code which u want to execute at the start
}
public static void ShowGreeting() {
message = 'Welcome to Extension demo' ;
}
public void mysave() {
Account NewAccount = New Account();
NewAccount.Name = VariableForStoringAccountName;
NewAccount.industry = 'Chemical';
insert NewAccount;
}
}
Log in to Salesforce → Setup → Build → Develop → Visualforce Pages(Click on it) → Click on New button
#Code for creating a Visualforce Page:
<apex:page standardController="Account" extensions="MyControllerExtensionExample">
<apex:form >
<apex:pageBlock title="Page Block 1">
<apex:pageBlockSection title="Page Block Section 1 | Custom Controller Example" Columns="2">
<apex:pageBlockSectionItem ><Apex:commandButton value="Greeting" reRender="id1" Action="{!ShowGreeting}"/></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ><Apex:outPutLabel id="id1"> {!message} </Apex:outPutLabel> </apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Page Block 2">
<apex:pageBlockSection title="Page Block Section 2 | Standard Controller Example" Columns="2">
<apex:pageBlockSectionItem >New Company:<apex:inputField value="{!Account.name}" required="False" /></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ><apex:commandButton value="Standard Save" action="{!save}"/></apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Page Block 3">
<apex:pageBlockSection title="Page Block Section 3 | Using Custom code For Save" Columns="2">
<apex:pageBlockSectionItem >New Company:<apex:inputText value="{!VariableForStoringAccountName}" /></apex:pageBlockSectionItem>
<apex:pageBlockSectionItem ><apex:commandButton value="Custom Save" action="{!mysave}"/></apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Result:
On pressing the button available on the screen, it will hit the APEX class and retrieve the message saved in it.
Note: By default, Objects has 2 things that are common, the data and the save, delete, edit, and other functions.
On button press, the data will be saved to the Default “Account” Object.
Checkout Salesforce Interview Questions
Note: On button press, the code hits the Apex Class which is already saved by name “MyControllerExtensionExample5Feb” and with the variable name (binding between the Apex Class & Visualforce Page) “VariableForStoringAccountName” and executes the code written in it and saves the data in Account Object.
In the next topic, we will discuss in detail “Batch Class in Salesforce”. Keep following us for more info on Salesforce Development/Programming.
Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers! |
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
Name | Dates | |
---|---|---|
Salesforce Training | Aug 05 to Aug 20 | |
Salesforce Training | Aug 08 to Aug 23 | |
Salesforce Training | Aug 12 to Aug 27 | |
Salesforce Training | Aug 15 to Aug 30 |
Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.
1 /15
Copyright © 2013 - 2023 MindMajix Technologies