Apex and Visualforce
VisualForce:it is framework used for develop
sophisticated, custom user interface that can be natively hosted on force.com
platform.
Standard Controller:
Visualforce uses the MVC (Model View
Controller) architecture, and includes standard built-in controllers to provide the standard
actions functionality when user clicks on the buttons and links in visualforce
page These built-in controllers are referred to generally as standard
controllers, or even the standard controller.
Custom Controller:
As salesforce provides standard controllers
same way developers can write their own controllers to use one functionality
when user clicks on buttons and links on visualforce pages.It is Apex class
that implements all the logic of the visualforce page functionality(Not UI)
without leveraging a standard controller.
A developer can either use a standard
controller provided by the Force.com platform, or add extension
controller (to standard controller) or custom controller logic with
a class written in Apex.
Note:- Developer can use only one
controller and many extensions(with standard controller).
Steps To create a custom controller
Go to Setup -> Develop -> Apex Classes
Click on New button
e.g:test vf page and test controller
example.
Controller Methods:
·
Getter (Property of the class)
·
Setter (Property of the class)
·
Actions
What is
a property?
•
Similar
to a variable, however, can do additional things in code.
Getter
Methods
•
Getter
methods return values from a controller.
•
Getter
methods must always be named getVariable.
Setter
Methods
•
Setter
methods pass user-specified values from page markup to a controller.
•
Any
setter methods in a controller are automatically executed before any action
methods.
•
Setter
methods must always be named setVariable.
What is
an action method?
•
Action
methods perform logic or navigation when a page event occurs, such as when a
user clicks a button.
•
Help
to define certain tasks that should be carried out when the corresponding
button or link is clicked on a visualforce page.
•
Action
methods can be called from page markup by using {! } notation in the action
parameter.
•
One
of the main differences between an action method and a regular method is that
an action method should return an object of type PageReference upon
completion.
•
An
action method as returning nothing (void) can be defined, but that is
equivalent to returning a NULL PageReference object.
There are
following types of collections in Apex
Lists
: Ordered collection of elements
Data type – Primitive, collections, sObjects,
User defined and built In apex types
Syntax
List<DataType> lst = new
List<DataType>();
List<DataType> lst = new string[5]
List Methods:
Add().
Remove().
Sort().
Sets: UnOrdered collection of elements
Do not contain duplicates
Syntax
Set<DataType> s1 = new
Set<DataType>();
Set<String> s1 = new
Set<String>{'a', 'b + c'};
Maps: Key-value pairs
Key are unique
Syntax
Map<DataType, DataType> State_code = new
Map<DataType, DataType>();
Map<String, Integer> State_Codes = new
Map<String, Integer>{'Maharashtra' => 1, 'Tamilnadu' => 2,’madhyapradesh’
=>3};
Standard List Controller
Create Visualforce pages that can display or act on a set of records.
Associating a Standard List Controller with a Visualforce Page
Accessing Data with List Controllers
Add Pagination to the List.
Standard list controller by default only shows the first 20 records that match your filter
criteria, if any.
Add standard web app user interface element, provided as part of Standard Controller
Actions.
Next & Previous.
Overriding Tabs Using a Standard List Controller.
Pages that use standard list controllers can be used to override tabs.
Eg, if we create a page for contacts using Standard List Controller, then
we can override the contacts tab to display our page.
From the object management settings for accounts, go to Buttons, Links, and Actions.
Click Edit for the Contacts Tab.
From the Visualforce Page drop-down list, select the overrideAccountTab page.
Click Save.
We can also add custom list buttons that can be used to open our
VF pages on the list page, or other places as desired. (Will be
covered later).
Pagination with a List Controller
Standard list controller provides
methods to implement pagination.
By utilizing the next and previous
actions, you can add pagination to a
page.
By default, a list controller returns 20
records on the page.
You can change that using pagesize()
method in an extension controller.
Using Static resources
Static resources allows you to upload content that you can reference in a Visualforce page, including archives (such as
.zip and .jar files), images, style sheets, JavaScript, and other files.
Referencing a Standalone file:
Use $Resource. <resource_name> as a merge field, where <resource_name> is the name you specified when you
uploaded the resource.
Ex:<apex:image url=“{!$Resource.TestImage}” width=“50” height=“50”/>
Referencing a files in archive:
Use the URLFOR function. Specify the static resource name that you provided when you uploaded the archive with
the first parameter, and the path to the desired file within the archive with the second.
Ex: <apex:image url="{!URLFOR($Resource.TestZip, 'Images/Tulips.jpg')}" width="50" height="50" />
Similarly use <apex:includeScript> to reference JavaScript files and <apex:stylesheet> to reference CSS files in
your Visualforce page.
Validation Rules and Standard Controllers
If a user enters data on a Visualforce page that uses a standard controller, and that data causes a
validation rule error, the error can be displayed on the Visualforce page.
If the validation rule error location is a field associated with an <apex:inputField> component, the
error dsplays there.
If the validation rule error location is set to the top of the page, use the <apex:pageMessages>
component within the <apex:page> to display the error.
Controller Extensions
A controller extension is an Apex class that extends the
functionality of a standard or custom controller.
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.
A controller extension is any Apex class containing a constructor
that takes a single argument of type
ApexPages.StandardController or CustomControllerName, where
CustomControllerName is the name of a custom controller you
want to extend..
Multiple Controller Extensions
Multiple controller extensions can be defined for a single page through a
comma-separated list.
This allows for overrides of methods with the same name.
<apex:page standardController="Account“
extensions="ExtOne,ExtTwo"
showHeader="false">
<apex:outputText value="{!foo}" />
</apex:page>
Overrides are defined by whichever methods are defined in the “leftmost”
extension, or, the extension that is first in the comma-separated list.
0 Comments