Hi folks, In this tutorial, we are going to learn Standard Controllers in Salesforce. A Controller in Salesforce is an apex class which is used to implement the business logic of visualforce without leveraging the standard functionality.

After completing this tutorial you will be able to understand:

  • Short Introduction about Controller and Types of Controller in Salesforce.
  • What are Standard Controllers in Salesforce?
  • How to use a Standard Controller in the Visualforce page.

What is a Controller?

A Controller in salesforce is an apex class which is used to implement business logic for visualforce without leveraging the standard functionality. Force.com provides the following three types of Controller.

  • Standard Controller.
  • Custom Controller.
  • Extension Controller.

Here we are going to learn about the standard Controller in Salesforce.

What is a Standard Controller in Salesforce?

In Force.com every sObject whether it is Standard Object like Account, Contact etc or any custom object has a Standard Controller associated with it. These Standard Controllers Contain the same logic as they are used in Standard Pages. For Example, we can use the Standard Save functionality for Account object using the Standard Account Controller for a Visualforce
page.

Standard Controller in Salesforce provides the ability to access and interact with structure business data contained in records and display the records in the proper user interface.

The syntax for using Standard Controller in Visualforce Page.

To use a Standard Controller in a Visualforce page we need to use standardcontroller attribute in <apex:page> tag. We can not use the controller and standardcontroller attribute at the same time. However, we can use an extension controller with standard controller attribute using the extensions attribute in <apex:page>. Following is the syntax for using Standard Controller in the visualforce page.

<apex:page standardcontroller="Account"></apex:page>

The task of Standard Controllers:

The Standard Controllers performs the following task.

  • Controlling Data.
  • Controlling Action.
  • Controlling Navigation.

Example 1: Create Account using Standard Controller.

Create a visualforceĀ page and replace the following code.

<apex:page standardController="Account" showHeader="false" sidebar="false" >
<apex:form>
<p>
Account Name:
<br/>
<apex:inputField value="{!account.name}"/> 
</p>
<br/>
<p>
Site
<br/>
<apex:inputField value="{!account.site}"/>
</p>
<br/>
<p>
Type
<br/>
<apex:inputField value="{!account.type}"/>
</p>
<br/>
<p>
Account Number
<br/>
<apex:inputField value="{!account.accountNumber}"/>
 
</p> 
<br/>
<apex:commandButton action="{!save}" value="Save"/>

</apex:form> 
 
</apex:page>

Preview the visualforce page.

Output:

Standard Controllers

Example 2: Display Account Records using Standard Controller.

<apex:page standardController="Account" showHeader="false" sidebar="false" recordSetVar="accounts">
<apex:form>

<apex:pageBlock title="Contacts List">
 
<apex:pageBlockTable value="{! accounts }" var="objAccount">
<apex:column value="{! objAccount.Name }"/>
<apex:column value="{! objAccount.Site }"/>
<apex:column value="{! objAccount.Type }"/>
<apex:column value="{! objAccount.AccountNumber }"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form> 
 
</apex:page>

View More:

Conclusion:

I hope you would love this tutorial. Please don’t hesitate to comment for any technical help. Your suggestions are welcome to me.

Thank You.

Leave a Comment