Tag Archives: Visualforce Page

Salesforce Visualforce Page Lifecycle

Visualforce page’s lifecycle is, how the page is created and destroyed during the period of a user session. The lifecycle of a visualforce page is determined not just by the content of the page, but also by how the page was requested.

When a user views a Visualforce page, instances of the controller, extensions, and components associated with the page are created by the server. The order in which these elements are executed can affect how the page is displayed to the user.

There are two types of Visualforce page requests:

Visualforce Page Get Requests:
A get request is an initial request for a page either made when a user enters an URL or when a link or button is clicked that takes the user to a new page.

Order of Execution for Visualforce Page Get Requests: The following diagram shows how a Visualforce page interacts with a controller extension or a custom controller class during a get request:

In the diagram above the user initially requests a page, either by entering a URL or clicking a link or button. This initial page request is called the get request.

  • The constructor methods on the associated custom controller/extension classes are called, instantiating the controller objects.
  • If the page contains any custom components, they are created and the constructor methods on any associated custom controllers or controller extensions are executed. If attributes are set on the custom component using expressions, the expressions are evaluated after the constructors are evaluated.
  • The page then executes any assignTo attributes on any custom components on the page. After the assignTo methods are executed, expressions are evaluated, the action attribute on the component is apex:pageevaluated, and all other method calls, such as getting or setting a property value, are made.
  • If the page contains an apex:form component, all of the information necessary to maintain the state of the database between page requests is saved as an encrypted view state. The view state is updated whenever the page is updated.
  • The resulting HTML is sent to the browser. If there are any client-side technologies on the page, such as JavaScript, the browser executes them.

Note:

  • Once a new get request is made by the user, the view state and controller objects are deleted.
  • If the user is redirected to a page that uses the same controller and the same or a proper subset of controller extensions, a post back request is made. When a post back request is made, the view state is maintained.

Visualforce Page Postback Requests:
A postback request is made when user interaction requires a page update, such as when a user clicks on a Save button and triggers a save action.
Order of Execution for Visualforce Page Postback Requests: The following diagram shows how a Visualforce page interacts with a controller extension or a custom controller class during a postback request:

  • During a postback request, the view state is decoded and used as the basis for updating the values on the page. (A component with the immediate attribute set to true bypasses this phase of the request. In other words, the action executes, but no validation is performed on the inputs and no data changes on the page.)
  • After the view state is decoded, Expressions are evaluated and set methods on the controller and any controller extensions, including set methods in controllers defined for custom components, are executed. If update is not valid due to validation rule or incorrect data type, data is not updated and page redisplay with error message.
  • Data updated on action there is no evaluation for component attribute. (The action attribute on the component is not evaluated during a postback request. It is only evaluated during a get request.)
  • The resulting HTML is sent to the browser.

Note:

  • Once the user is redirected to another page, the view state and controller objects are deleted.
  • You can use the setRedirect attribute on a pageReference to control whether a postback or get request is executed. If setRedirect is set to true, a get request is executed. Setting it to false does not ignore the restriction that a postback request will be executed if and only if the target uses the same controller and a proper subset of extensions. If setRedirect is set to false, and the target does not meet those requirements, a get request will be made.

How to get picklist values based on record type in Visualforce page?

Here in below example I’ve a custom object “Customer__c”, which has two record types “HR” and “Marketing”.

Apex Class:

public with sharing class recordTypeSample{

    public String selectedRT {get;set;}
    public List<SelectOption> recordTypeList {get;set;}
    public Customer__c customer {get;set;}
    
    public recordTypeSample(){
    
        customer = new Customer__c();
        recordTypeList = new List<SelectOption>();
        getRecordTypeList();
    
    }
    
    public void getRecordTypeList(){
         
        List<RecordType> rtList = [SELECT Id,Name FROM RecordType WHERE SObjectType='Customer__c'];
        recordTypeList.add(new SelectOption('--None--', '--None--'));
        for(RecordType rt : rtList)
        {
            recordTypeList.add(new SelectOption(rt.Id, rt.Name));
        }
    }
    
     public void getPickListValues(){
     
        if(selectedRT != null){
            customer = new Customer__c(RecordTypeId = selectedRT);
        }
    }
}

Visualforce Page:

<apex:page controller="recordTypeSample">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:selectList value="{!selectedRT}" size="1" multiselect="false" label="Record Type" title="Record Type" id="recordTypes"> 
                    <apex:actionSupport event="onchange" action="{!getPickListValues}" reRender="categoryPicList" />
                    <apex:selectOptions   value="{!RecordTypeList}" /> 
                </apex:selectList>
                <apex:inputField id="categoryPicList" value="{!customer.Category__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

HR Record Type Picklist :

Marketing Record Type Picklist :

Radio Button in Visualforce Page

<apex:page >
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >Gender
                    <apex:selectRadio >
                        <apex:selectOption itemLabel="Male" itemValue="m"/>
                        <apex:selectOption itemLabel="Female" itemValue="f"/>           
                    </apex:selectRadio>     
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Custom Label in Salesforce

Custom labels enable developers to create multilingual applications by automatically presenting information (for example, help text or error messages) in a user’s native language. Custom labels are custom text values that can be accessed from Apex classes, Visualforce pages, or Lightning components. The values can be translated into any language Salesforce supports.

We can create up to 5,000 custom labels for your organization, and they can be up to 1,000 characters in length.

Setup | App Setup | Custom Labels | Click ‘New Custom Label’ Button

Fill in the details and Click ‘Save’ button.

 

Custom Label in Visualforce page:
$Label.CustomLabel for Visualforce page.

<apex:page standardController="Account">
	
<h1>Example for Custom labels</h1>

	<apex:outputLabel value="{!$Label.Message}"/>
</apex:page>

 

Custom Label in Apex Class:
System.Label.Label_name for apex class

String msg = System.Label.Message; 

 

Custom Label in Lightning Component:
$Label.c.labelName for the default namespace.
$Label.namespace.labelName if your org has a namespace, or to access a label in a managed package.

<aura:component implements="flexipage:availableForAllPageTypes">
    
<div onclick="{!c.clickLabel}">
        <ui:outputText value="{!$Label.c.Message}}" />
    </div>

</aura:component>

 

Custom Label in Javascript:
$A.get(“$Label.c.labelName”) for the default namespace.
$A.get(“$Label.namespace.labelName”) if your org has a namespace, or to access a label in a managed package.

({
    clickLabel : function(component, event, helper) {
        var label = $A.get("$Label.c.Message");
        alert(label);
    }
})

Summary field as footer of pageblock table in Visualforce page.

In this post I’ll show, how can we add the summary field or total amount or other information in footer of PageBlockTable or DataTable in Visualforce page.

In the below example I’m using the apex:facet name="footer" tag, to show sum of all the opportunity line items of an Opportunity in footer of PageBlockTable.

<apex:page standardController="Opportunity">    
    <apex:pageBlock title="{!Opportunity.Name}">    
        <apex:pageblocktable value="{!Opportunity.OpportunityLineItems}" var="item">
            <apex:column value="{!item.PricebookEntry.Name}"/>
            <apex:column value="{!item.ServiceDate}"/>
            <apex:column value="{!item.Quantity}"/>
            <apex:column value="{!item.UnitPrice}">
                <apex:facet name="footer">
                    <apex:outputText value="Total:" style="float: right;"/>
                </apex:facet>
            </apex:column>
            <apex:column value="{!item.TotalPrice}">
                <apex:facet name="footer">
                    ${!Opportunity.Amount}
                </apex:facet>
            </apex:column>
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>