StandardController and Custom Controller in Visualforce Page


Welcome to another salesforce post, if you learning Visualforce page UI development than you may had heard two terms which are StandardController and StandardSetController, I usually get confused in both and can’t able to decide which one should I use and for which purpose. I am pretty sure you all should all have used the StandardController when you wrote your first custom visualforce page but you might not have heard of StandardSetController, so let today see in this post will see what are StandardController and StandardSetController.


StandardController work with single record of an object, for an example you have created a visualforce page for an account record and you are saving account details through visualforce page, this visualforce page is enough if you want don’t want to perform any complex operations.

How and When to use StandardController

If you are creating a very basic page as discussed above a simple page to enter account information and save it in salesforce database as it doesn’t require any custom functionality, so we don’t need an extension, so we just use StandardController to perform basic operations.

Below is an example of simple basic VF page.

<apex:page standardController="Account" title="Account" >
    <apex:variable value="{!account}" var="a"/>
    <apex:sectionHeader subtitle="New Account" title="Account"/>
    <apex:messages title="errors" />
    <apex:form >
        <apex:pageBlock mode="edit" title="new account" tabStyle="Position__c">
                <apex:pageBlockButtons >
                    <apex:commandbutton action="{!Save}" title="save" value="save"/>
                    <apex:commandButton action="{!Cancel}" title="Cancel" value="Cancel"/>
            <apex:pageBlockSection collapsible="true" title="Account Information" columns="2" lang="Eng">
                <apex:inputField value="{!}"/>
                <apex:inputField value="{!}"/>
                <apex:inputField value="{!a.rating}"/>
                <apex:inputField value="{!}"/>
            <apex:pageBlockSection collapsible="false" columns="2" title="Address Information">
                <apex:inputField value="{!a.billingstreet}"/>
                <apex:inputField value="{!a.billingstate}"/>
                <apex:inputField value="{!a.billingpostalcode}"/>
                <apex:inputField value="{!a.billingcountry}"/>
                <apex:inputField value="{!a.billingcity}"/>
            <apex:pageBlockSection collapsible="false" columns="2" title="Description">
                <apex:inputField value="{!a.description}"/>

So the above code is a very simple basic Visual form page which we use to save account details and as we don’t require any custom functionality so we have not used any custom controller or extension.

Now we already seen when we can use StandardController but there is one more important reason to only use StandardController if you want that end user should comply the security settings of that object like if the user doesn’t have access to certain field then that field will not be visible to that user on the page.

Use Custom Controller as StandardController

Consider a scenario where you are working on a single account record and you want to push that record into the salesforce database but before doing that you need to perform some other operations like for an example finding duplicate records based on name of account which we are inserting into salesforce, so this does seem to be complex and can’t be achieve by just using a StandardController hence we use custom controller as standard controller to perform complex operations.


<apex:page controller="MyController" tabStyle="Account">
  <apex:form >
    <apex:pageBlock title="Congratulations {!$User.FirstName}">
    You belong to Account Name: <apex:inputField value="{!}"/>
    <apex:commandButton action="{!save}" value="save"/>

The code for my custom controller is given below,

public class MyController{
public Account account { get; private set; }
  public PageReference save() {
    try {
    } catch(System.DMLException e) {
    return null;
  // After successful Save, navigate to the default view page
  PageReference redirectSuccess = new ApexPages.StandardController(Account).view();
  return (redirectSuccess);

In save method you can perform your logic like searching for other account based on name of account you are inserting or you can also perform other logical operation based on your requirements.

When to Use Standard Controller or Custom Controller

I have discussed this in brief above that if your application is not that complex and you want that user should respect the security of the objects like if a field is hidden it shouldn’t visible on layout or if user doesn’t have access to that field then he can’t able to manipulate the value in that field for that particular record in that case you should standard controller, on other hand if your application use complex operation then you can use custom controller but keep in mind that custom controller will run in system mode.

Related Articles :

Speak Your Mind



This site uses Akismet to reduce spam. Learn how your comment data is processed.