The Complete Guide to Controllers in Salesforce
Salesforce controllers connect with the database and pull the information from the database to see the information through a page made by the summit page.
Controllers in Salesforce are utilized in characterizing both the information and the activities for the different VisualForce pages and lighting Components. They are commonly utilized for getting to various kinds of items through the APIs. These are not quite the same as the expansions.
To show a particular record or gathering of records, we need the record ID. At the point when incorporated with other Visualforce pages the ID can stream to the controller page consequently. Be that as it may, on an independent page we have to determine the record ID physically to see the controller working.
There are three types of controllers:
- Standard Controller
- Custom Controller
- Controller Extension
Standard Controllers
The Standard Controllers in Salesforce are the ones that help in your collaboration with the VisualForce pages. As the name recommends it is the genuine 'norm' or the point from which you begin making use and even actualize the genuine usefulness of the norm or any custom article viz. the Account or the <TestObject>_c object. The Standard rundown of controllers is regularly required for showing a total rundown of records in the Salesforce VisualForce.These controllers are additionally valuable in getting to the Child Objects, that lie one stage beneath in the chain of command, similar to Contact to Account.
Types of Standard Controllers in Salesforce are:
- Standard Set Controllers
- Standard List Controllers
Don't forget to check out: Collections In Salesforce | Apex Developer Guide
The following methods are for Standard Controller:
- cancel()
- first() - Returns the first page .
- getCompleteResult()
- getFilterId()
- getHasNext()
- getHasPrevious()
- getListViewOptions()
- getPageNumber()
- getPageSize()
- getRecord() - Returns the sObject that represents the changes to the selected records from org.
- getResultSize() - Returns the number of records in the set.
- getSelected()
- last()
- next() - Returns the next page of records.
- previous()
- save()
Custom Controller
Custom Controller is characterized as an Apex class that executes the rationale of a page without utilizing an ordinary controller. To make your visualforce pages execute totally in framework mode, we need to utilize custom controllers. Custom controllers incorporate information control and custom rationale which are utilized by the visualforce page. Custom Controllers get certain things, and the visualforce utilizes them as a controller. It is utilized to bring the information that will be shown on the visualforce page.
Customs controller classes are used in framework mode.
Through the Keyword "with sharing", we can handle level security, Role Hierarchy, and so on.
Steps Create a Custom Controller Apex Class
Step 1) Go to the Setup and select Developer console and press File/New/Apex class to establish the latest apex class.
Step 2) Open the editor, replace the code with the below code:
Here is an example controller code:-
public class ExamController { // desired code }
Check out another amazing blog by Ayush here: Email Services in Salesforce | The Developer Guide
Controller Extension
Controller expansion fundamentally alludes to a particular class of APEX with the all-encompassing usefulness of both the custom and standard controller. These at last clear a path for you to solidify the functionalities of both the controllers by augmentation as it considers more than one expansion for a specific Custom or a Standard controller.
The need for Controller Extension:
- When new functions/Actions have to be added
- More modularity and even the readability
Reference: mindmajix, uni-due.de, stackoverflow, doi
Responses