Thursday 19 May 2016

Multiple extension in visualforce page

Multiple extension in visualforce page


So as to associate an apex class with a visualforce page we have to use extension keyword. We can even associate multiple classes with a single visualforce page. So as to do this we have separate the multiple class names by coma as done in the below line,

<apex:page standardController="Account" extensions= "firstclass,secondclass">

An example below uses two different extension controller ,

Visualforce Page

<apex:page standardController="Account" extensions= "firstExtensionController, secondExtextensionController" tabStyle="Lead">
  <apex:form >
    <apex:pageBlock >    
      <apex:pageBlockSection title="Account Details From First Extenssion">    
        <apex:outputField value="{!accDetails.name}"/>
        <apex:outputField value="{!accDetails.accountnumber}"/>
        <apex:outputField value="{!accDetails.annualrevenue}"/>
        <apex:outputField value="{!accDetails.type}"/>
        <apex:commandButton value="Show Account Details" action="{!ShowAccountDetails}"/>
      </apex:pageBlockSection>
    
      <apex:pageblockSection title="Contacts Of Account From Second Extenssion">
         <apex:commandButton value="Show Contacts" action="{!ShowAllContactsOfAcc}"/>
      </apex:pageblockSection>

      <apex:pageblockTable value="{!conList}" var="conRec">
        <apex:column value="{!conRec.name}"/>
        <apex:column value="{!conRec.email}"/>
        <apex:column value="{!conRec.description}"/>
        <apex:column value="{!conRec.LeadSource}"/>
        <apex:column value="{!conRec.Department }"/>      
      </apex:pageblockTable>          
 
    </apex:pageBlock>
  </apex:form>
</apex:page>


Extension Controller 1

Public with sharing class firstExtensionController {
Public Account accDetails{get;set;}
Public id CurrentRecordId;
    public firstExtensionController(ApexPages.StandardController stdcontroller) {
      CurrentRecordId = stdcontroller.getrecord().id;
    }  
    Public void ShowAccountDetails(){
      accDetails = [select name,accountnumber,annualrevenue,type from account where id=:CurrentRecordId limit 1];
    }
}


Extension Controller 1

Public with sharing class secondExtextensionController {
Public List<contact> conList{get;set;}
Public id CurrentRecordId ;
    public secondExtextensionController(ApexPages.StandardController stdcontroller) {
        CurrentRecordId = stdcontroller.getrecord().id;
    } 
   Public Void ShowAllContactsOfAcc(){
     conList = [select name,email,description,LeadSource,Department from contact where accountid = : CurrentRecordId ];
   }
}
 


Output of the above example,


In developer mode you will see both the extension side by side as below,

No comments:

Post a Comment