controller extension

Controller Extensions in Salesforce | The Developer Guide

Controller Extension is an Apex Code that extends the functionality of a Standard or Custom Controller.

When to Use Controller Extensions

  • When we want to use the built-in functionality of a Standard Controller but override one or more actions, like edit, view, save, or delete.
  • When we want to add new actions.
  • When we want to build a Visualforce page that respects user permissions
  • If the Controller Extension extends a Standard Controller it will execute in user mode in which permissions, field-level security, and sharing rules of the current user apply, even though a controller extension class executes in system mode.

dont miss out iconDon't forget to check out: Enhance Your Single View of The Customer Using Salesforce Marketing Cloud Connect

Advertisement

Types of Extensions

  1. Standard controllers: It is used for performing operations on a single record.
  2. Standardsetcontrollers: It is used for performing operations on a group of records.

Example: Methods of apexpages.standardcontroller

Apex Class:

public class allmethods {
    public apexpages.StandardController cont {set;get;}
    public account acc {set;get;}
    public account acc1 {set;get;}
    public allmethods(apexpages.StandardController controller){
        cont=controller;
        string[] ss= new string[]{'name','email__c','industry','rating'};
        acc=(account)cont.getrecord();
    }
    public void savenew(){
        insert acc;
        acc.clear();
    }
}

Visualforce Page:

<apex:page standardController="account" extensions="allmethods">
    <apex:form>
        <apex:commandButton value="SAVE" action="{!save}"/>
        <!---- This is applicable only for Edit and detail page---> 
        <apex:commandButton value="cancel" action="{!cancel}"/> 
        <!---- This is applicable only for Edit and detail page---> 
        <apex:commandButton value="delete1" action="{!delete}"/> 
        <!---- This is applicable only for detail page---> 
        <apex:commandButton value="edit1" action="{!edit}"/> 
        <!---- This is applicable only for detail page---> 
        <apex:commandButton value="SAVEnew" action="{!savenew}"/> 
        <!---- This is a custom functionality so it is visible in the page---> 
        <apex:pageblock> 
            <apex:inputField value="{!account.name}"/> 
            <apex:inputField value="{!account.Email__c}"/> 
            <apex:inputField value="{!account.industry}"/> 
            <apex:inputField value="{!account.rating}"/> 
        </apex:pageblock>
    </apex:form>
</apex:page>

Example1: Customizing “new” button with a custom Visualforce page in account object without extensions.

Visualforce Page:

<apex:page standardController="account">
    <apex:form>
        <apex:pageBlock>
            <apex:commandButton value="save" action="{!save}"/>
            <apex:commandButton value="cancel" action="{!cancel}"/>
            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="account name"/>
                    <apex:inputField value="{!account.name}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Email"/>
                    <apex:inputField value="{!account.Email__c}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="industry"/>
                    <apex:inputField value="{!account.industry}"/>
               </apex:pageBlockSectionItem>
               <apex:pageBlockSectionItem>
                   <apex:outputLabel value="rating"/>
                   <apex:inputField value="{!account.rating}"/>
               </apex:pageBlockSectionItem>
           </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>

The above page is linked to new button using below steps Setup -> Customize -> Accounts -> Buttons, Links, and Actions -> Edit(New) -> Visualforce page -> save

dont miss out iconCheck out another amazing blog by Kirandeep here: What Are Events In Salesforce Lightning Component

Example2: Creating functionality of “save&New” Button using Extensions(We cannot retrieve existing save&new functionality).

Apex Class:

public class SaveNew {
    public account acc {set;get;}
    public saveNew(apexpages.StandardController controller){
        acc=new account();
    }
    public void savene(){
        insert acc;
        acc=new account();
    }
}

Visualforce Page:

<apex:page standardController="account" extensions="SaveNew">
    <apex:form>
        <apex:pageBlock>
            <apex:commandButton value="save" action="{!save}"/>
            <apex:commandButton value="save&New" action="{!saveNe}"/>
            <apex:commandButton value="cancel" action="{!cancel}"/>
            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="account name"/>
                    <apex:inputField value="{!acc.name}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Email"/>
                    <apex:inputField value="{!acc.Email__c}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="industry"/>
                    <apex:inputField value="{!acc.industry}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="rating"/>
                    <apex:inputField value="{!acc.rating}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection> 
        </apex:pageBlock> 
    </apex:form>
</apex:page>

Responses

Popular Salesforce Blogs