Activity Forums Salesforce® Discussions Explain The Need Or Importance Of The Controller Extension in Salesforce?

  • shariq

    Member
    September 20, 2018 at 3:11 pm

    Hi,

    Controller Extension is very useful and important concept introduced by the salesforce recently. It gives the power to the programmer to extend the functionality of existing custom controller or standard controller.

    A Visualforce can have a single Custom controller or standard controller but many controller extensions.

    We can say that the custom extension is the supporter of custom or standard controller.

    Consider one example: If there is one controller written and used by the multiple Visualforce pages and one of them needs some extra logic. Then instead of writing that logic to controller class (Which is used by many Visualforce pages) we can create a controller extension and apply to that page only.

    Thanks

  • Parul

    Member
    September 20, 2018 at 6:17 pm

    controller extension is very important it's 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.

    Thanks

  • shariq

    Member
    September 20, 2018 at 10:46 pm

    Hi,

    Adding Examples -

    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.

  • Parul

    Member
    September 21, 2018 at 3:30 am

    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.

Popular Salesforce Blogs