Activity › Forums › Salesforce® Discussions › What Is The Controller Extension?
-
What Is The Controller Extension?
Posted by Aman on September 20, 2018 at 3:07 PMWhat Is The Controller Extension?
Parul replied 7 years, 9 months ago 3 Members · 4 Replies -
4 Replies
-
Hi,
Any apex class having a public constructor with Custom Controller or Standard Controller object as a single argument is known as controller extension.
Thanks
- [adinserter block='9']
-
Hi
A controller extension is an Apex class that extends the functionality of a standard or custom controller. Use controller extensions when: You want to leverage the built-in functionality of a standard controller but override one or more actions, such as edit, view, save, or delete.
You want to add new actions.
You want to build a Visualforce page that respects user permissions. Although a controller extension class executes in system mode, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead, it executes in user mode, in which permissions, field-level security, and sharing rules of the current user apply. -
Hi,
Adding Example –
Code for Visualforce page
<apex:page Controller=”AddmultipleAccountsController”>
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value=”{!listAccount}” var=”acc”>
<apex:column headerValue=”Account Name”>
<apex:inputField value=”{!acc.Name}”/>
</apex:column>
<apex:column headerValue=”Account Number”>
<apex:inputField value=”{!acc.AccountNumber}”/>
</apex:column>
<apex:column headerValue=”Account Type”>
<apex:inputField value=”{!acc.Type}”/>
</apex:column>
<apex:column headerValue=”Industry”>
<apex:inputField value=”{!acc.Industry}”/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value=”Add one more account” action=”{!addAccount}”/>
<apex:commandButton value=”Save Accounts” action=”{!saveAccount}”/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Code for custom controller:public class AddmultipleAccountsController {
Account account = new Account();
public list<Account> listAccount{ get; set; }public AddmultipleAccountsController()
{
listAccount=new list<Account>();
listAccount.add(account);
}Public void addAccount()
{
Account acc = new Account();
listAccount.add(acc);
}
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
{
insert listAccount;
}
return Page.Allaccountssaved; // I am returning another vf page here.
}
}Hope this helps.
-
Hi Shariq, Here is the example of Controller Extension:
<apex:page standardController=”Account” extensions=”myControllerExtension”>
{!greeting} <p/>
<apex:form>
<apex:inputField value=”{!account.name}”/> <p/>
<apex:commandButton value=”Save” action=”{!save}”/>
</apex:form>
</apex:page>public class myControllerExtension {
private final Account acct;
// The extension constructor initializes the private member
// variable acct by using the getRecord method from the standard
// controller.
public myControllerExtension(ApexPages.StandardController stdController) {
this.acct = (Account)stdController.getRecord();
}public String getGreeting() {
return ‘Hello ‘ + acct.name + ‘ (‘ + acct.id + ‘)’;
}
}Thanks
Log In to reply.