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 include data manipulation and custom logic which are used by the visualforce page. Custom Controllers fetch certain items, and the visualforce uses them as a controller. It is used to fetch the data that is to be displayed on the visualforce page.

Want to enhance your skills in dealing with the worlds best CRM, enroll in our Salesforce Training 

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.

Checkout Salesforce Interview Questions

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

Subscribe to our youtube channel to get new updates..!

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 Custom controller or a standard controller. 

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. 

Checkout SalesForce Tutorial

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 

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 setter method before it executes the dosearch action method.

Conclusion

In Salesforce, custom controllers are used to implement 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 about Controller Extension in Salesforce”. Keep following us for more info on Salesforce Development/Programming.

Are you looking to get trained on Salesforce, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our Salesforce Certification Training program from your nearest city.

Salesforce Training Chennai, Salesforce Training Dallas, Salesforce Training Bangalore, Salesforce Training Hyderabad, Salesforce Training Mumbai, Salesforce Training Delhi, Salesforce Training Noida, Salesforce Training New York, Salesforce Training Chicago, Salesforce Training Kolkata, Salesforce Training Gurgaon, Salesforce Training Pune.

These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified Salesforce Trainers

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

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