Standard Controllers

Learn All About Standard Controllers in Salesforce | The Developer Guide

The Visualforce controller is used to access visual markers associated with a specific link or button. These controllers can be used to provide access to the data displayed on the page and modify the behavior of components. There are many standard controllers for each Salesforce object, and they provide functions similar to custom controllers.

The custom controller is a user-defined Apex class that appliance all the logic of the page without using the standard controller. Whenever the Visualforce page needs to run completely in system mode and does not enforce the permissions and field-level security of the current user, the custom controller will work. You can extend the controller extension to every controller in Visualforce.

If we want to use the built-in functions of the standard controller, but we want to override one or more of them for custom specifications, such as editing, viewing, saving, or deleting. we can add new actions. A Visualforce page that respects user permissions. The controller extension class is executed in system mode, but if the standard controller is extended, the logic from the standard controller will not be executed in system mode. Instead, it is executed in user mode, sharing rules, field-level security, and the permissions of the current user are applied.

dont miss out iconDon't forget to check out: The Complete Guide to Controllers in Salesforce

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();
    }
}

VF 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>

Example2: Creating a 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();
    }
}

VF 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>

dont miss out iconCheck out another amazing blog by Kirandeep here: How To Use Database.Stateful Interface In Batch Apex In Salesforce

One more Way of the above program.Where we are directly accessing object fields.

Apex Class:

public class SaveNew {
    public account acc {set;get;}
    public saveNew(apexpages.StandardController cont){
        acc=(Account)cont.getrecord();
    }
    public void savene(){
        insert acc;
        acc.clear();
    }
}

VF 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="{!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>

 

Responses

Popular Salesforce Blogs