Controllers in VisualForce
In the last story, we took up tags and skilled up ourselves to create new visualforce pages from scratch. Now let us jump to Controllers in visualforce. Try to understand Standard controllers, standard list controllers, custom controllers, and how to use them.
Controllers :
A VisualForce controller is a set of instructions that specifies what happens when a user interacts with the components in a VisualForce page.
A controller :
- Provides access to the data
- can modify a component’s behavior.
Standard Controllers:
The built-in controllers which are available in Salesforce are known as standard Controllers. A standard controller is an object, it can be a standard or custom object. A standard controller allows you to work on a single record.
Syntax for using a Standard controller in a VF page:
<apex:page standardController=”Name of Controller”>
In the above syntax, the VisualForce page uses Account object as it’s Standard Controller. Now this visualforce page can have all the built-in actions that a standard controller provides and also it can fetch data from account object.
There are some actions which are available in controllers and are very useful.
Action Methods supported by all standard controller:
save- saves a record and returns to object page
quicksave -saves the record and stays at same page
edit — takes the page to edit mode
delete — deletes a record
cancel — cancel the ongoing process
list — returns a pagereference object of the standard list page
{!save} syntax of using a save action method method
{!Account.name} syntax of showing a field name of the account
{!$user.FirstName} we need to use a $ sign before the user to bind the current user and to know the first name of the current user.
Standard List Controllers:
- Allows you to work with list of records(multiple records)
- can be used with both standard and custom objects
- uses attribute recordSetVar with standard controller attribute
<apex:page standardController=”Account” recordsetvar=”Accounts”> - supports Pagination by providing some pre-built actions
Custom Controller:
- uses an apex class, that implements all logics for a page without using a standard controller.
- use it when you want to run your VF page entirely in system mode, ie, without securities.
- a constructor should be defined inside a controller class
- uses attribute controller in <apex:page> tag
<apex:page controller=”something”>
Custom controller extensions:
- apex class that extends the functionality of standard or custom controller
- You can either add new functionalities or override the existing ones.
- Respect user permissions, does not run in system mode like custom controllers.
- uses attribute extensions with addition to standard controller
<apex:Page standarController=”Account” extensions=”Something”>
As now we are familiar with tags and controllers in Visualforce, I am moving to a trailhead module that gives us more clarity on topics and also provides hands-on experience with basic tags, controllers, and static resources in Salesforce. The trailhead module which we will take up is named as VisualForce Basics.
In Standard list controller, the requirement is stated as: Using a Standard List Controller, create a Visualforce page which displays a list of Accounts with links to their respective record detail pages.
In the above code, we have used a standard list controller for Account object.
The output link tag passes account’s id to URL which helps in redirecting the page to account page when clicked on name.
In custom controller, the requirement is stated as : Create a Visualforce page that uses a custom controller to display a list of cases with the status of ‘New’.
Challenge Requirements
So now we are good to go with visualforce and ready to jump to Lightning aura components. We will study and develop them in the next set of stories.
How Can You Contribute?
- Connect with me on Twitter or Linkedin or Instagram.
- Provide me continuous feedback and suggestions for moving ahead in this journey.
- Share the series on Twitter and LinkedIn.
Posts In This Series :
Salesforce Development — Learning Apex Programming, VisualForce, Lightning Components
First Step toward Learning Apex Programming
Day 2 in Apex — Writing apex codes
Working with sObjects, SOQL, and SOSL
Understanding Salesforce Triggers and working with them
Apex Test classes and methods
Understanding VisualForce Concepts
Controllers in VisualForce