Standard Controllers

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

(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:


Books to Consider

comments powered by Disqus
X

An Introduction to 3D Printing

Explore the future of manufacturing and design  - read our guide to 3d printing for free