Standard Controllers

Exclusive offer: get 50% off this eBook here
Visualforce Developer’s guide

Visualforce Developer’s guide — Save 50%

Learn the latest developments in SalesForce with this hands-on pocket guide with this book and ebook

$23.99    $12.00
by W.A.Chamil Madusanka | October 2013 | Enterprise Articles

This article, given by Chamil Madusanka, the author of Visualforce Developer's Guide, covers a set of instructions that can react on the user's interaction with Visualforce markup (for example, a button click or a link click) is called as a controller. A controller can control the behavior of a page and it can be used to access the data which should be displayed on the page. This article covers what are standard controllers.

(For more resources related to this topic, see here.)

This article includes a set of examples to explain the important elements and features of Visualforce. There are four custom objects (API names: Customer__c, Item__c, Order__c , Order_Line__c) in this application. The following is the E-R diagram of an order processing application which we will create on the Force.com platform:

The E-R diagram of an order processing application

The Force.com platform provides a few types of controllers. The first one is standard controller and every sObject has a standard controller. They have the same logic and functionality as they are originally used in standard pages. Therefore we can use standard controllers with Visualforce pages. For example, if we use Contact standard controller for a Visualforce page, we can implement the standard Save method for Contact without writing any additional Apex code. This behavior is the same as implementing the Save method on the standard Contact edit page.

How to use a standard controller with a Visualforce page

The <apex:page> tag has an attribute called standardController which is used to associate a standard controller with a Visualforce Page. The value of the standardController attribute would be the API name of an sObject:

<apex:page standardController="Customer__c"> </apex:page>

The preceding code shows the usage of the standardController attribute.

You cannot use the standardController and controller attributes at the same time.

Standard controller actions

In Visualforce pages, we can define the action attribute for the following standard Visualforce components:

  • <apex:commandButton>: This component creates a button that calls an action
  • <apex:commandLink>: This component creates a link that calls an action
  • <apex:actionPoller>: This component periodically calls an action
  • <apex:actionSupport>: This component makes an event (such as onclick, onmouseover, and so on) on another named component and calls an action
  • <apex:actionFunction>: This component defines a new JavaScript function that calls an action
  • <apex:page>: This component calls an action when the page is loaded

An action method can be called from the page using the {!} notation. For example, if your action method's name is MyFirstMethod, then you can use the {!MyFirstMethod} notation for calling the action method from the page markup. This action method can be from a standard controller or a custom controller or a controller extension.

A standard controller has a few standard action methods, as follows:

  • save: This method inserts/updates a record. Upon successful completion it will be redirected to the standard detail page or a custom Visualforce page.
  • quicksave: This method inserts/updates a record. There are no redirections to a detail page or custom Visualforce page.
  • edit: This method navigates the user to the edit page for current record. Upon successful completion it will be returned to the page that invoked the action.
  • delete: This method deletes the current record. It redirects the user to the list view page by selecting the most recently viewed list filter.
  • cancel: This method cancels an edit operation. Upon successful completion it will be returned to the page that invoked the edit action.
  • list: This method redirects to the list view page by selecting the most recently-viewed list filter.

For example, the following page allows us to insert a new customer or update an existing customer record. If we are going to use this page to update a customer record, then the URL must be specified with the ID query string parameter. Every standard controller has a getter method that returns the record specified by the ID query string parameter in the page URL. When we click on Save, the save action is triggered on the standard controller, and the details of the customer are updated. If we are going to use this page to insert a customer record, then the URL must not be specified as a parameter. In this scenario, when we click on Save, the save action is triggered on the standard controller, and a new customer record is inserted.

<apex:page standardController="Customer__c">
<apex:form >
<apex:pageBlock title="New Customer" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="My Content Section"
columns="2">
<apex:inputField value="{!Customer__c.Name}"/>
<apex:inputField value="{!Customer__c.Email__c}"/>
<apex:inputField value="{!Customer__c.Address__c}"/>
<apex:inputField value="{!Customer__c.Telephone__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

The page markup allows you to access fields of a particular sObject by using {!sObjectAPIName.FieldAPIName}. For example, if you want to access the Email field of the Customer object, the page that uses the Customer__c standard controller can use {!Customer__c.Email__c} to return the value of the Email field of the customer who is in the current context.

The following page allows us to view a customer record. In this page also, the URL must be specified in the ID query string parameter. The getter method of the Customer__c standard controller returns the record specified by the ID query string parameter in the page URL:

<apex:page standardController="Customer__c">
<apex:form >
<apex:pageBlock title="Customer" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Customer Details"
columns="2">
<apex:outputField value="{!Customer__c.Name}"/>
<apex:outputField value="{!Customer__c.Email__c}"/>
<apex:outputField value="{!Customer__c.Address__c}"/>
<apex:outputField value="{!Customer__c.
Telephone__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

To check the accessibility of a particular object for the logged user, you can use the {!$ObjectType.objectname.accessible} notation. This expression returns a Boolean value. For a example, if you want to check the accessibility of the Customer object, you can use {!$ObjectType.Customer__c.accessible}.

<apex:page standardController="Customer__c">
<apex:form >
<apex:pageBlock title="New Customer" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton rendered="{!$ObjectType.
Customer__c.accessible}" action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Customer Details"
columns="2">
<apex:inputField value="{!Customer__c.Name}"/>
<apex:inputField value="{!Customer__c.Email__c}"/>
<apex:inputField value="{!Customer__c.Address__c}"/>
<apex:inputField value="{!Customer__c.Telephone__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

The preceding code explains the usage of object accessibility. According to the example, you can see the Save button, only if the particular user has security permission to access the customer record.

Summary

In this article, we became familiar with controllers. We learned the differences and the usage of standard controller.

Resources for Article:


Further resources on this subject:


Visualforce Developer’s guide Learn the latest developments in SalesForce with this hands-on pocket guide with this book and ebook
Published: October 2013
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


W.A.Chamil Madusanka

W.A.Chamil Madusanka is a Salesforce.com certified Force.com developer. He has been working on Force.com projects since 2011. He is working as a developer for many custom applications built on Force.com and has also trained end users and new Salesforce developers in his current company (Attune Lanka (pvt) Ltd.) and former company (Sabre Technologies (pvt) Ltd.) He has won the Salesforce New Year Resolution 2013 challenge which was rolled out by Salesforce. He is an active member of the Force.com community and he has been contributing to the Force.com community through various channels. He is avid about Force.com and shares his knowledge on Force.com technologies through his blog (http://salesforceworld.blogspot.com/). He is a super-contributor on the Force.com discussion board and shares his knowledge and experience on Force.com by providing effective solutions to developer questions. He is the initiator and the group leader of the Sri Lanka Salesforce Platform Developer User Group. His contribution to the Sri Lanka Salesforce community has led to an increase in Salesforce competency in Sri Lanka. He completed his B.Sc in Computer Science from the University of Colombo School of Computing, Sri Lanka (UCSC). His areas of interest include Cloud computing, semantic web technologies, and Ontology-based systems. Hailing from Polonnaruwa, which is an ancient city in Sri Lanka, he currently resides in Gampaha which is located in the Western province of Sri Lanka. His interests include reading technology books and technology blog posts, and playing cricket. Chamil can be reached via Twitter (@chamilmadusanka), Skype (chamilmadusanka), and e-mail (chamil.madusanka@gmail.com).

Books From Packt


Salesforce CRM: The Definitive Admin Handbook
Salesforce CRM: The Definitive Admin Handbook

Salesforce CRM: The Definitive Admin Handbook - Second Edition
Salesforce CRM: The Definitive Admin Handbook - Second Edition

Salesforce CRM Admin Cookbook
Salesforce CRM Admin Cookbook

Force.com Tips and Tricks
Force.com Tips and Tricks

Force.com Developer Certification Handbook (DEV401)
Force.com Developer Certification Handbook (DEV401)

Visualforce Development Cookbook
Visualforce Development Cookbook

Oracle APEX Best Practices
Oracle APEX Best Practices

Oracle APEX 4.0 Cookbook
Oracle APEX 4.0 Cookbook


Your rating: None Average: 5 (1 vote)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
N
G
u
K
f
p
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software