Thursday 19 May 2016

Visualforce Page with Extension Examples

Extension Controller Example:


A visualforce page can be built using standard controller, this gives us capability to use all the standard functionality available with the standard salesforce pages plus we get the standard UI appearance. Now, if we need more complex logic in the visualforce page then we will definitely need a apex class for doing all the calculations.

When using standard controller we can associate an apex class with that visualforce page by using extensions. We need to specify name of the class in the extension attribute and the class becomes available for that visualforce page. In the below example, an account standard controller visualforce page is associated with a apex class controller 'accountDetailScreenController'. The page shows all the contacts associated with a account record. So as to get all the contacts, select query is used and all the contacts are then shown on the page.

Visualforce Page

<apex:page standardController="Account" extensions="accountDetailScreenController" tabStyle="Solution">
 <apex:form >
  <apex:pageBlock >
    <apex:pageBlockSection title="Account Details Using Standard Controller">
       <apex:outputField value="{!account.name}"/>
       <apex:outputField value="{!account.Type}"/>
       <apex:outputField value="{!account.Annualrevenue}"/>
       <apex:outputField value="{!account.AccountNumber}"/>
  <apex:commandButton value="Show Contacts" action="{!fetchContacts}" style="align:left" />
    </apex:pageBlockSection> 


    <apex:pageblockSection title="Contacts fetched using Extenssion class" rendered="{!showCOntactFlag }" id="displaycontacts">
    </apex:pageblockSection>

      <apex:pageBlockTable value="{!conList}" var="conRec">
        <apex:column value="{!conRec.name}"/>  
        <apex:column value="{!conRec.email}"/>
        <apex:column value="{!conRec.leadsource}"/>
        <apex:column value="{!conRec.Department}"/>
        <apex:column value="{!conRec.Description }"/>
      </apex:pageBlockTable>    


  </apex:pageBlock>
 </apex:form>
</apex:page>


Extension Controller class

Public with sharing class accountDetailScreenController {
Public list<contact> conList{get;set;}
Public id CurrentAccountId;
Public boolean showCOntactFlag{get;set;}
    public accountDetailScreenController(ApexPages.StandardController controller) {
      CurrentAccountId = controller.getrecord().id;
      showCOntactFlag = false;
    }
   Public void fetchContacts(){ 
     showCOntactFlag = true;
     conList = new List<contact>();
     conList = [select name,email,leadsource,Department,Description from contact where accountid =:CurrentAccountId];
   }
}


Output of the above example is as below,

3 comments:

  1. Nice information about the extension controller my sincere thanks for sharing this post and please continue to share this kind of post
    Salesforce Training in Chennai

    ReplyDelete
  2. It is very interesting that many of the bloggers to helped clarify a few things for me as well as giving. Most of ideas can be nice content. The people to give them a good shake to get your point and across the command. Salesforce Training in Chennai

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete