Visualforce Pages in Salesforce

Visualforce Pages in Salesforce  

Introduction to Creating Visualforce Pages:

Visualforce pages are basic building blocks for application developers. A Visualforce page is similar to a standard Web page, but includes powerful features to access, display, and update your organization’s data. Pages can be referenced and invoked via a unique URL, just as they would be on a traditional web server.
Visualforce uses a tag-based markup language that’s similar to HTML. Each Visualforce tag corresponds to a coarse or fine-grained user interface component, such as a section of a page, a list view, or an individual field. Visualforce boasts nearly 150 built-in components, and provides a way for developers to create their own components. Visualforce markup can be freely mixed with HTML markup, CSS styles, and JavaScript libraries, giving you considerable flexibility in how you implement your app’s user interface.

What is a Controller in Salesforce?
Controller in Salesforce is an Apex class which is used to implement all the logic of Visualforce without leveraging the standard functionality. In visualforce there are three types of Controllers. They are

  1. Standard Controllers
  2. Custom Controllers / Controller
  3. Extension Controllers


1. Standard Controllers : 

When our VFP is dependent on any controller which is actually an object (standard or custom, does not matter) then that controller is called as Standard Controller.
Note: At a time, only 1 object can be used. Either standard or custom.

To use Standard Controller in Visualforce pages, StandardController attribute must be used on<apex:page> tag. When implementing standardcontroller attribute in Visualforce pages, we can not use the controller attribute at the same time.

Standard Controller Limitations:

  • We can not show data from multiple objects.
  • We can not write our logic in some APEX class and call the same when button is pressed
  • We can not fetch data from database
  • We can not do any DML
  • Relationship data fetching process is not feasible.


Standard Controller Example (Visualforce Page)

<apex:page standardController="Account">     <apex:form> <apex:pageBlock title="My Content"> <apex:pageBlockButtons> <apex:commandButton action="{!save)"value="Save Record"/> </apex:pageBlockButtons> <apex:pageBlockSection title="My Content Section"columns="2"> <apex:inputField value="{!Account.name}"required="false"></apex:inputField> <apex:inputField value="{!Account.fax}"/> <apex:inputField value="{!Account.phone}"/> <apex:inputField value="{!Account.account Number}"/> <apex:inputField value="{!Account.industry}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

2. Custom Controller:

  • These are written in a class, using apex code
  • These are used when we cant perform the needed work using the standard controllers
  • When we want additional functionality in the page, by processing the objects, we need to use Custom Controllers
  • They are used to create rich UI with complex data sets etc


Custom Controller Example (Visualforce Page): 

  <apex:page controller="ExampleClass"> <apex:form> <apex:pageBlock title="My Content"> <apex:pageBlockSection title="My Content Section" columns="2"> <Apex:outputLabel> Enter Name:</apex:outputLabel> <apex:inputText value="{!UserName}"/> <apex:outputLabel> <Apex:commandButton value="Click Me"Action="{!ShowMessage}"/> <Apex:outputLabel>{!message}</Apex:outputLabel> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

ExampleClass (Apex Class): 

public with sharing class ExampleClass{ public string UserName(set;get;} public string message(set;get;} public void ShowMessage(){ message='Welcome+UserName; } }

3. Extension Controllers :

  • It gives the benefit of Standard as well as Custom Controllers.
  • We canuse the fascility or ease that we get in Standard controller as well as we can get the flexibility provided by the customer controller.
  • We can get all these benefits in one single page using the Controller Extension
  • We cant use more than1object in Standard Controller,as well as we cant use more thanIclass in Custome controller. But using Controller Extension,we can go beyond these limitiations.
  • Controller Extenstions are widely used in higher level Visual force page where we want to implement complex kind of logieaproduce the related UI

Syntax:
<Apex:page StandardController='Account' extensions='MyClass1,MyClass2,My Class3>


Extention Controller Example (View All Leads Visualforce Page)
 : 

<apex:page controller="LeadController"> <apex:form > <apex:pageBlock title="All Leads" tabStyle="Lead"> <apex:pageBlockSection columns="3" title="Search Lead"> <apex:inputText value="{!searchText}" html-placeholder="Enter text to search"/> <apex:commandButton value="Search Lead" action="{!searchLead}" /> </apex:pageBlockSection> <apex:pageBlockTable value="{!lstLeads}" var="objLead"> <apex:column > <apex:facet name="header">Action</apex:facet> <apex:commandLink value="View" action="{!viewLead}"> <apex:param name="rowId" value="{!objLead.Id}" assignTo="{!leadId}" /> </apex:commandLink> | <apex:commandLink value="Edit" action="{!edit}"> <apex:param name="rowId" value="{!objLead.Id}" assignTo="{!leadId}" /> </apex:commandLink> | <apex:commandLink value="Delete" action="{!deleteLead}" onclick="if(!confirm('Are you sure?')) return false;"> <apex:param name="rowId" value="{!objLead.Id}" assignTo="{!leadId}" /> </apex:commandLink> </apex:column> <apex:column value="{!objLead.Name}" /> <apex:column value="{!objLead.Company}" /> <apex:column value="{!objLead.Email}" /> <apex:column value="{!objLead.Status}" /> <apex:column value="{!objLead.IsConverted}" /> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>

Lead View (Visualforce Page) : 

<apex:page standardController="Lead" extensions="LeadExtension" > <apex:form > <apex:sectionHeader title="Open Lead" subtitle="Lead View"/> <apex:pageBlock title="Lead View" rendered="{!$CurrentPage.parameters.id != ''}"> <apex:pageBlockSection columns="2" title="Lead Information" collapsible="false"> <apex:outputField value="{!Lead.Name}" /> <apex:outputField value="{!Lead.Company}"/> <apex:outputField value="{!Lead.Email}"/> <apex:outputField value="{!Lead.Status}" /> <apex:outputField value="{!Lead.Phone}" /> </apex:pageBlockSection> <apex:pageBlockButtons > <apex:commandButton value="Convert Lead" action="{!convertLead}" /> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page>

LeadController (Apex Class): 

public class LeadController { public List<Lead> lstLeads {get; set;} public String searchText {get; set;} public String leadId {get; set;} public LeadController() { lstLeads = [SELECT Id, Name, Company, Email, Status, IsConverted FROM Lead WHERE IsConverted = false]; } public void searchLead() { searchText = '%'+searchText+'%'; lstLeads = [ SELECT Id, Name, Company, Email, Status, IsConverted FROM Lead WHERE IsConverted = false AND Name LIKE : searchText ]; searchText = ''; } public PageReference viewLead() { String url = '/apex/ViewLead?id='+leadId; System.PageReference objRef = new System.PageReference(url); objRef.setRedirect(true); return objRef; } public PageReference edit() { String url = '/' + leadId + '/e?retURL=/apex/VfAllLeads'; System.PageReference objRef = new System.PageReference(url); objRef.setRedirect(true); return objRef; } public PageReference deleteLead() { Lead objLead = new Lead(Id= leadId); System.debug('leadId' + leadId); delete objLead; String url = '/apex/VfAllLeads'; System.PageReference objRef = new System.PageReference(url); objRef.setRedirect(true); return objRef; } }

LeadExtension (Apex Class)

public class LeadExtension { public String leadId; public LeadExtension(ApexPages.StandardController ctrl) { leadId = ctrl.getId(); System.debug('Lead Id : ' + leadId); } public PageReference convertLead() { Database.LeadConvert leadConvert = new Database.LeadConvert(); leadConvert.setLeadId(leadId); LeadStatus leadStatusObj = [SELECT Id, IsConverted, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1]; leadConvert.setConvertedStatus(leadStatusObj.MasterLabel); leadConvert.setDoNotCreateOpportunity(true); Database.convertLead(leadConvert); String url = '/p/lead/ViewConvertedLead/d?id='+ leadId; System.PageReference objRef = new System.PageReference(url); objRef.setRedirect(true); return objRef; } }

# Custom Errors In Visualforce Pages:

How to add Custom error messages in visualforce page?
Validation rules are not applied here

Howmany types of errors in visualforce page?

  • CONFIRM
  • ERROR
  • FATAL
  • INFO
  • WARNING


Calculator Page (Visualforce Page) : 

<apex:page controller="Calculator"> <apex:pageMessages id="showmsg"></apex:pageMessages> <apex:form> <apex:pageBlock title="My VP Calculator"> <apex:pageBlockSection title="My Calculator"> Number1:<apex:input Text value="{!NumberA}"/> Number2:<apex:input Text value="{!NumberB}"/> <Apex:commandButton value="Sum"Action="{!ShowAdd}"/> <Apex:commandButton value="Sub"Action="{!ShowSub}"/> <Apex:commandButton value="Mul"Action="{!ShowMul}"/> <Apex:commandButton value="Div"Action="{!ShowDiv}"/> <Apex:outputLabel id="Id1">{!message}</Apex:outputLabel> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

Calculator (Apex Class) : 

public class Calculator{ public integer NumberA{set;get;} Public integer NumberB{set;get;} public string message{set;get;} public void ShowAdd(){ if(NumberA<0){ ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'First number is negative')); } if(NumberB<0){ ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Warning,'Second number is negative')); } string result='The result is'+(NumberA+NumberB);     message = result; } }





Resources:

 # Trailhead: Visualforce Basics 

 # Create Visualforce Pages 

Standard Component Reference 


.

Comments

Popular Posts

Asynchronous Apex in Salesforce : Future Methods

Salesforce Admin Interview Questions and Answers

Apex Triggers in Salesforce

DML - Data Manipulation Language in Apex