Standard Controllers Salesforce: Complete Guide | SalesforceTutorial

Written by Prasanth Kumar Published on Updated on

Standard Controllers Salesforce: Complete Guide | SalesforceTutorial

What are Standard Controllers in Salesforce?

A Standard Controller is an automatically provided controller object for all standard and custom objects in Salesforce. It binds to a Visualforce page using the “standardController” attribute and provides a reference to a single record or list of records, along with common data processing actions and default navigation capabilities.

Standard controllers eliminate the need to write custom Apex code for basic CRUD operations, making them essential for rapid Visualforce development. They’re particularly valuable in Salesforce admin scenarios where declarative solutions are preferred over programmatic approaches.

Core Functions of Salesforce Standard Controllers

Standard controllers provide three primary capabilities:

  • Data Control: Fetching and binding record data to Visualforce components
  • Action Control: Handling user interactions like save, cancel, edit, and delete
  • Navigation Control: Managing page transitions and record navigation

Controlling Data with Standard Controllers

Standard controllers fetch data from Salesforce objects and provide it to views such as pages, lists, dialogs, or forms. This data binding is the foundation of Visualforce page functionality.

Single Record Data Binding

To associate a standard controller with a Visualforce page for a single record, use the standardController attribute:

<apex:page standardController="Account">                // Standard object
<apex:page standardController="Customer__c">        // Custom object

Access record fields using merge field syntax, identical to email templates and formula fields:

<apex:outputField value="{!Account.Name}"/>
<apex:outputField value="{!Account.AccountNumber}"/>
<apex:inputField value="{!Account.Industry}"/>
<apex:inputField value="{!Account.Type}"/>

For related parent object fields, use dot notation:

<apex:inputField value="{!Account.Owner.Name}"/>

Display related lists using the apex:relatedList component:

<apex:relatedList list="Contacts"/>

To view the page output, append the record ID to your Visualforce URL:

https://c.apX.visual.force.com/apex/vfpage?id=XXXXXXXXXXXXXXX

List of Records Data Management

Standard controllers can manage multiple records using the recordSetVar attribute:

<apex:page standardController="Account" recordSetVar="accounts">

This enables pagination actions for large datasets:

  • First: Navigate to the first page of records
  • Last: Navigate to the last page of records
  • Next: Navigate to the next page of records
  • Previous: Navigate to the previous page of records

Standard Controller Actions and Navigation

Action Types

Standard controllers respond to user commands through two action categories:

Stateful Actions: Direct binding to a standard controller instance referenced by the page. These maintain state between page interactions and include:

  • Save
  • Cancel
  • Edit
  • Delete
  • Clone

Stateless Actions: Indirect binding to the Force.com platform’s standard controller instruction set. These don’t maintain page state and include navigation actions.

Navigation Control

Standard controllers automatically navigate users to appropriate views based on the action performed. For example, after saving a record, the controller redirects to the record detail page, while canceling returns to the previous page.

Best Practices for Standard Controllers

  • Security: Standard controllers respect field-level security and sharing rules automatically
  • Performance: Use recordSetVar for list views to enable efficient pagination
  • Governor Limits: Standard controllers count against SOQL query limits when fetching data
  • Testing: Standard controller functionality requires proper test data setup in test classes

Common Use Cases in Salesforce Development

Standard controllers are frequently used in:

  • Custom record detail pages
  • Data entry forms
  • List views with custom formatting
  • Mobile-optimized pages
  • Integration with Salesforce security models

Limitations and Considerations

While powerful, standard controllers have limitations:

  • Cannot perform complex business logic without custom controller extensions
  • Limited to single object operations (no cross-object transactions)
  • Pagination is fixed at 20 records per page for list views
  • Cannot override standard validation rules

For advanced scenarios requiring custom logic, consider using controller extensions or custom controllers alongside standard controllers.

Frequently Asked Questions

What is the difference between standard controllers and custom controllers in Salesforce?

Standard controllers are automatically provided by Salesforce for all objects and handle basic CRUD operations without custom code. Custom controllers require Apex code and provide complete control over page behavior, data processing, and business logic.

Can standard controllers work with custom objects?

Yes, standard controllers work with both standard and custom objects. Use the same syntax: <apex:page standardController=”CustomObject__c”> for custom objects.

How do standard controllers handle Salesforce security and sharing rules?

Standard controllers automatically enforce field-level security, object permissions, and sharing rules. Users can only access data they have permission to view or modify through their profile and permission sets.

What are the governor limits for standard controllers?

Standard controllers count against SOQL query limits (100 synchronous, 200 asynchronous) and heap size limits. Each page load with a standard controller typically uses 1-2 SOQL queries depending on related data accessed.

Can I extend standard controller functionality?

Yes, use controller extensions to add custom methods while retaining standard controller functionality. Syntax: <apex:page standardController=”Account” extensions=”MyControllerExtension”>