Home / Salesforce

Custom Controller in Salesforce

Rating: 5.0Blog-star
Views: 9692
by Arogyalokesh
Last modified: May 28th 2021

Introduction to Custom Controller

Custom Controller is defined as an Apex class that executes the logic of a page without using a regular controller. To make your Visualforce pages execute completely in system mode, we have to use custom controllers. Custom controllers in Saelsforce include data manipulation and custom logic which are used by the Visualforce page. Custom Controllers fetch certain items, and the visual force uses them as a controller. It is used to fetch the data that is to be displayed on the visualforce page.

Need of Custom Controller

  1. In salesforce, a custom controller is applied to overrule the embedded services of a regular controller.

  2. To include new actions.

  3. To develop visualforce pages that follows user permissions.

  4. Customs controller classes are implemented in system mode.

  5. Through the Keyword “ with sharing”, we can do field-level security, Role Hierarchy, etc.

Types of Custom Controller

  1. Standard Controller

In salesforce, standard controllers are used to interacting with and access the structured business data present in the records.

  1. Custom Controller

Custom controllers are used to developing customised functionalities and they can replace the available functionalities.

  1. Controller Extension

In Salesforce, controller extensions are used to include or expand the services of a Custom controller or a standard controller. 

 

Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Online Course.

 

Create a Custom Controller Apex Class

Several utility and system classes are available to develop custom controller logic. To create a custom controller class, we have to follow the below steps:

  • Step1: Go to the Developer console and press File/New/Apex class to establish the latest apex class. Name the class as “EmailidListController”
  • Step2: Open the editor, replace the code with the below code:
Public class EmailidListController {

Code

}

When you make any changes in the Apex, you have to save it.

  • Step3: you have to come to the Visualforce page again and save it.
  • Step4: To see the preview of the Visualforce page, you have to click the preview button. While making changes to the page, you can use a visualforce page. 

 

Related article - Salesforce Guide

 

Creating a Visualforce page that uses Custom Controller

  • Step1: Go to the developer console and press File/New/Visualforce Page to create the latest Visualforce page. Name the page as “EmailidListController”
  • Step2: Go to the editor, replace the markup with the following code:
<apex:page:controller=”EmailidListController”>

<apex:form>

< Apex: page block: Email Id List id =” Email Id_List”>

</apex:pageBlock>

</apex:form>

</apex:page>

Controller Methods

Visualforce Markup uses the following Methods:

1. Action Methods

These methods are used to carry out navigation or logic when a page event happens, like a user clicking on a button, etc. Action methods are invoked through (!) notation.

2. Getter Methods

These methods are used to retrieve values from the controller. Each value determined by the controller and exhibited in a visualforce page must have a getter method associated with it. 

For example, in a controller apex class if we have a variable called “account”, then it should have a “getAccount” method associated with it to access it through (!) notation. Value argument present in the < Apex:inputField> tag applies this notation to use the account, and dot notation is used to exhibit the name of the account.

3. Setter Methods

These methods are used to send particular values to the controller. In the controller, setter methods are executed automatically before the action methods get executed. For 

For example, if we want the markup to execute the search utility of leads on a page, then the corresponding controller integrates the setter and getter methods for the input of the search box. 

After that, the controller uses the search text to send a SOSL Query, once the user presses GO. Markup implements the setter method before it executes the search action method.

Conclusion

In Salesforce, custom controllers are used to implementing the logic of the visualforce pages.  Custom controllers will have the methods to retrieve the data that is to be displayed on the visualforce page.

In the next topic, we will discuss in detail Controller Extension in Salesforce”. Keep following us for more info on Salesforce Development/Programming.

 

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!

 

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training Salesforce Lightning Training
Salesforce Advanced Developer Training Salesforce Developer Training
Salesforce IoT Training Salesforce App Builder Certification Training
Salesforce AppExchange Training Salesforce Service Cloud Training
and many more.  

About Author

author
NameArogyalokesh
Author Bio

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.