Types of Custom Controller
- Standard Controller: In salesforce, standard controllers are used to interacting with and access the structured business data present in the records.
- Custom Controller : Custom controllers are used to developing customised functionalities and they can replace the available functionalities
- Controller Extension: In Salesforce, controller extensions are used to include or expand the services of a Custom controller or a standard controller.
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
- In salesforce, a custom controller is applied to overrule the embedded services of a regular controller.
- To include new actions.
- To develop visualforce pages that follows user permissions.
- Customs controller classes are implemented in system mode.
- Through the Keyword “ with sharing”, we can do field-level security, Role Hierarchy, etc.
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.
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>
Difference between Standard Controller and Custom Controller and Extension?
Standard Controller:
- Standard controller in apex, inherits all the standard object properties and standard button functionality directly.
- It contain the same functionality and logic that are used for standard Salesforce pages.
- It Can be used with standard objects and custom objects.
- It can be extended to implement custom functionality using
extensions
keyword. - It provides a save method to allow you to persist changes.
- You’d use this when you have a singe object to manipulate.
Custom Controller:
- It is an Apex class that implements all of the logic for a page without leveraging a standard controller.
- Custom Controllers are associated with Visualforce pages through the controller attribute.
- Custom controller defines its own functionality.
- Use custom controllers when you want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.
- You’d use this when your page isn’t dealing with a main object.
Extension:
- A controller extension is any Apex class containing a constructor that takes a single argument of type ApexPages.
- It provides additional functionality to a controller – either a standard controller or a custom controller.
- You can use multiple Apex classes separated by comma.
- Use controller extensions when:You want to leverage the built-in functionality of a standard controller but override one or more actions, such as edit, view, save, or delete.You want to add new actions.You want to build a Visualforce page that respects user permissions. Although a controller extension class executes in system mode, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead, it executes in user mode, in which permissions, field-level security, and sharing rules of the current user apply.
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.
public class Example { public void Set(String name) { this.name = name; } public String getName() { return name; } }
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.