Standard Controllers

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.

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:

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}.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.