Black Friday Deal : Up to 40% OFF! + 2 free self-paced courses + Free Ebook  - SCHEDULE CALL

- Salesforce Blogs -

An Ultimate Guide to Salesforce Controllers

The best career you can have today in the world of IT is in Salesforce. It is one of the greatest CRM software in the world. Also, it is growing, and there is no looking back. However, there are many steps and aspects of learning Salesforce. Controller extension is one significant part of the Salesforce. 

Let us take this in detail.

It is always better to know something at length, its benefits and how will it be helpful to you in your career graph. It is also important to know how to make most of your time to learn the most important parts so that you get the right opportunities. Even when you are mid-way in your career and looking for the next step in your career, you will be required to upgrade your knowledge and skills. 

In this blog, the idea is to take you about the controller extensions and everything around them so you have a better picture of them before you actually make your mind to dig deeper into these.

Let us begin by finding out what are these controllers.

Types of Controllers in Salesforce

Controllers in Salesforce are employed in defining both the data and the actions for the various VisualForce pages. They are typically used for accessing different types of objects through the APIs. These are different from the extensions. Where the custom controllers are used for containing all the data and actions in a particular page that has to be executed at a future time, the extensions let the data auctioned that is already present as buttons, custom overrides, links, etc. Also, if you make use of the Properties while you use controllers, it will help you to highly difficult code for a long time without any interruptio, s and also you will not be required to go through any kind of clutter while you review the code. 

types of controllers in salesforce

1) Standard Controllers 

The standard controllers in Salesforce are the ones that help in your interaction with the VisualForce pages. As the name suggests it is the real ‘standard’ or the point from which you start making use and even implement the true functionality of the standard or any custom object viz. the Account or the <TestObject>_c object. 

These come endowed with many inbuilt functionalities. Save, Edit, Delete, and Cancel, etc. are some of the standard actions that are available in these types of controllers. 

The standard list of controllers is often needed for displaying a complete list of records in the Salesforce VisualForce. These are thus, used for leveraging the basic or the default behaviour of objects like the clone, save, delete, edit in addition to many other basic methods or other properties that are present in those objects. 

These controllers are also useful in accessing the Child Objects, that lie one step below in the hierarchy, like Contact to Account, etc. 

Many pagination options like Fast, Like, Previous and even Next are also available. All these are highly similar to other Standard Controllers except the recordSetVar. 

Read: Salesforce Admin Salary Trends in 2024 | How much do Salesforce Admins Earn

In a basic initialization of the controller for various objects, <sObject> is often accepted as a parameter. Thus, once the object is there it becomes important to leverage it by making use of the following section of initialization of the standard controller. 

<sObject> obj = (<sObject>) std.getRecord();

Herein, the <sObject> has to be replaced by the kind of sObject that you are handling in that particular case. 

The syntax for these controllers can be in form of the following:

Syntax: <apex:page standardcontroller="account" recordsetvar="accounts">

The VisualForce pages that make use of the declaration of the standard controller, are also used as the custom buttons on the objects by joining them on the VisualForce page that has been defined. Also, to avoid a standard button in an object like the New, Edit, Delete, etc. the defined VisualForce Page has to make use of the standard controller of the object. 

List of Standard Controllers in Salesforce are:

  1. Standard Set Controllers: They are said to accept a list or a query locator of the basic standard controller object kind.
  2. Standard List Controllers: They are on the other hand said to provide the ability for navigation via the recordSetVar list of the standard controller’s type of object. 

salesforce Curriculum

It has to be noted that a particular case of using a Standard Set/List Controller is linked with buttons on the ListViews. This is typically the case when the getRecords() returns a particular list of objects and getSelected () will then return a list of the ones that have been selected before by making use of the checkboxes that lie next only to the records in a typical list view.

Some of the most known uses of the Standard Controllers are for adjustment of the default object landing pages, custom and List view functionality, inline pages, etc. 

2) Custom Controllers

Apart from the standard controllers, there are the custom controllers in Salesforce. They are the ones that take on the functionality of the Standard Controller and also allow access to extra information:

  1. Accessing the data that is not present or available with a typical Standard Controller. It is just like more than one level down a child or other related objects.
  2. Creation of the custom behavior of various properties or methods.
  3. Making a change in present functionality, i.e. the ways in which the default actions work and give rise to many new actions.
  4. Customization of the navigation like the custom layout, custom navigation from one action to another, sub-tabs, etc. 

These controllers are basically used for the implementation of the Custom Logic with the VisualForce pages. Custom controllers also allow the possibility of the creation of new functionalities. 

The experienced developers often resort to custom controllers as and when they need additional complexity and some kind of new behavior. This often happens at the cost of the use of a standard controller. This is often used when one is thinking of the next types of the controller as an add-on strategy. 

Read: Salesforce Tutorial - Ultimate Learning Guide for Beginners & Beyond

The syntax for these controllers is

Syntax: <apex:page controller="account">

3) Extension Controller

Controller extension basically refers to a specific class of APEX with the extended functionality of both the custom and standard controller. These ultimately make way for you to consolidate the functionalities of both the controllers by extension as it allows for more than one extension for a particular Custom or a Standard controller.

Controller extensions can be used in multiple scenarios like the following:

  1. When new functions have to be added.
  2. When a new function has to be used and many changes have to be made to the functional flow.
  3. When a VFP has to be build that has to be applied to various permissions for the user. This happens even when the controller extension is executed in the system mode.

You have to remember that although it is possible to add extensions to any kind of controller, you still have to make a choice between the Standard Controller or the Custom Controller but not both, while you are declaring a particular page. However, you can use many extensions as desired if you really want to extend the functionality. 

The Controller extensions can also be used for overriding various kinds of permissions. This is possible because it is possible for the extension class to possess a setting for different sharing than the one that is inherited.

It becomes important for you to keep in mind that the extensions extend the basic standard custom controller. Thus, every extension will accept the Standard or the Custom Controller as a different parameter to the constructor as shown below:


public class myControllerExtension {
private final Account acct;
// The extension constructor initializes the private member 
// variable acct by using the getRecord method from the standard 
// controller 
public myControllerExtension(ApexPages.Standard Controller stdController) {
this.acct = (Account) stdController.getRecord();
      }
public String getGreeting() {
return 'Hello ' + acct.name + ' (' + acct.id + ')';
      }
}

You have to remember that it is vital for you to make use of the initialization like

(<sObject>)stdController.getRecord();

for accessing the real object that the extension is working for. 

Hence, the controller extensions are basically the best of both the worlds for using the standard and the custom controller functionality and further extend them. It allows for a much more reusable and a modular kind of approach and even the design. 

Now that you have realized and know in detail about the different types of controllers, it is also essential to find out when is the best time for using each of them. 

Read: What is Test Classes in Apex Salesforce?

salesforce quiz

Best Timing of Use

  • Standard Controllers: These are found useful in use cases like the Clone, Save, Delete, Edit and also those ones where single records are handled without much customization. Also, in case of more than one record of the same type, the standard controller functionality is added. A Standard Set/ List Controller is often used for those particular cases. 
  • Custom Controllers: These are used at times when you more than one record of the same object that is not served by the Standard Controllers. Hence, basically they are used when you actually want to change the functionality of the standard controller, and also change the way things actually work and also give more complex functionality.
  • Controller Extensions: These are used when the functionality of the existing controller needs to be extended and the actions of the native controller are properly leveraged or the functionality of the custom controller is increased. These are thus used for increasing the organization of the functionality, and even provision of more modularity and even the readability. 

In all of these, one thing gets underlined and that is the same functionality has to be used again for multiple pages. You also have to ensure that you use the same controllers for avoiding the duplication of the same kind of functionality. The same controller can be used over more than one page. 

Now that everything seems settled, let us also walk through some deeper lanes in the Standard controller and see how they actually work to access data.

Standard Controller Methods in Salesforce

A Standard Controller usually makes use of a getter method for accessing the data. The method is said to return the particular record that has been marked by the id query string parameter in the URL of the page. 

  • Getter Method: In this method, the linked page markup of the reference field is allowed on the context record by making use of the {!object} syntax, where the object is present in the lowercase name of the object linked with the controller. For instance, if a page that makes use of the Account standard controller can actually use {!account.name} for returning the value of the name field on the account that is under the lens. 

This method is successful only when the record that is marked in the specific id query string parameter in the URL is of the same form as is the standard controller. 

  • Using Merge Field Syntax: This is particularly used in the queries that use the Lightning Platform API. The field syntax can be merged for retrieving data from all the linked records. 

It allows you to walk up to five levels of the child-to-parent relationship like if you are making use of the contact standard controller then you can use the {! contact.Account.Owner.FirstName} shows a three-level child-to-parent relationship for returning the name of the owner of the account record that is linked with the contact. 

Also, you can also walk up to one level of the child-to-parent relationship like if you are using the Account standard controller then you can use the {!account. Contacts} for returning an array of all the contacts that are linked with the particular account in question. 

Use of Standard Controller Actions

salesforce controllersEvery time a page event takes place, the action methods help in performing logic and navigation like when a user hits on a button or simply visits a particular area of the webpage. All the action methods are actually called from the page markup by using {! } notation in the various action parameters of the following tags.

  • <apex:CommandButton> This helps in the creation of a button that actually creates the action.
  • <apex:Commandlink> This helps in the creation of a link that calls an action.
  • <apex:actionPoller> This helps in periodically calling an action.
  • <apex:actionSupport> This helps in making an event like “onclick”, “onmouseover” on some component that is called an action.
  • <apex:actionFunction> This helps in defining a new JavaScript function that also calls an action.
  • <apex: page> This also calls an action when the page is loaded.

Action Methods in Standard Controllers in Salesforce

  • Save: It helps in inserting new records or even updating an existing one which is in context. After the completion of the operation, the save action helps in returning the user to the original page. 
  • Quicksave: It helps in the insertion of a new record or updating an existing record if it is in context. This does not redirect the user to another page.
  • Edit: It helps in the navigation of the user to the edit page for the record that is in context. After the completion of the action, the edit action helps in returning the user to the page where the user invoked the action originally. 
  • Delete: This helps in deleting the record that is in context. After the completion of the operation, the page is either deleted or refreshed or the user is sent to the tab for the linked object.
  • Cancel: This as the name suggests aborts the edit operation. After the operation is completed, the cancel returns the user to the page where the user had used the edit originally. 
  • List: This helps in returning a PageReference object of the standard list page. This is based on the recent most used list filter for that specific object. For instance, if the standard controller is contact and the last filtered list that was viewed by the user was the New Last Week, the contacts that were created in the last week are displayed. 

free salesforce demo

Summary

Every new step in life is based on the choice we make. These little choices although small usually pave the way for something really big in life. Salesforce is the real future of the online sphere as it is spreading its wings to different areas. It is the way things are handled at every step that will help you in your life. Hence, Salesforce has many aspects that have to be studied at length to grow in the organization. Everything matters and has to be considered. These were some of the important pointers about the controllers in Salesforce. Knowledge of these is essential and has to be well-taken. Do let us know how far it was useful for enhancing your knowledge so we may keep bringing more of these to you.

Read: Top 125 Visual Salesforce Interview Questions and Answers For Freshers and Experienced


Salesforce Tutorial Overview

fbicons FaceBook twitterTwitter lingedinLinkedIn pinterest Pinterest emailEmail

     Logo

    JanBask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


  • fb-15
  • twitter-15
  • linkedin-15

Comments

Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

-0 day 23 Nov 2024

QA Course

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

-0 day 23 Nov 2024

Salesforce Course

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

-0 day 23 Nov 2024

Business Analyst Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst Course

Upcoming Class

-0 day 23 Nov 2024

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

-0 day 23 Nov 2024

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

-0 day 23 Nov 2024

DevOps Course

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

4 days 27 Nov 2024

Hadoop Course

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

13 days 06 Dec 2024

Python Course

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

7 days 30 Nov 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

-0 day 23 Nov 2024

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

34 days 27 Dec 2024

 Tableau Course

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau Course

Upcoming Class

13 days 06 Dec 2024

Search Posts

Reset

Receive Latest Materials and Offers on Salesforce Course

Interviews