+ Start a Discussion
pooja biswaspooja biswas 

display visualforce page based on picklist selection

Hi
I have created two record types for contact object.
I have a custom picklist displaye don vf page which has record types of contact object as picklist values.
I have a vf page called "contactsedit" which I want to display based on user selection in picklist.
In picklist if user selects record_type_1 then, the page "contactsedit" would be called with the customized selections made for record_type_1.
Here I will insert values into and save to UI.
similary in picklist if user selects "record_type_2" then the page "contactsedit" would be called with the customized selections made for record_type_2.
Here is the code.
public with sharing class bindrecordtypepicklist
{ 
   list<SelectOption> options {get;set;} 
 
   public String selectedRecordType{get;set;} 

   private Contact con;
   
   public bindrecordtypepicklist(ApexPages.StandardController stdcontroller)
   {
       this.con = (Contact)stdController.getRecord();
   }
   public list<SelectOption> getRecordTypes() 
   { 
      options = new list<SelectOption>(); 
      options.add(new selectOption('', '- None -'));
      for(RecordType rt:[SELECT Name FROM RecordType WHERE SobjectType = 'Contact' AND IsActive=True]) 
      { 
         if(rt != null) 
         {
           options.add(new SelectOption(rt.ID,rt.Name)); 
         }
      } 
      return options; 
  }   
  public void CallContactsEditPage()
  {
   //
  }
}

<apex:page standardController="Contact"
           extensions="bindrecordtypepicklist">
 <apex:form id="TheForm"> 
 <apex:pageblock > 
    <apex:outputLabel > <b>Record Types</b> : </apex:outputLabel> 
    <apex:selectList size="1" 
                     value="{!selectedRecordType}"
                     multiselect="false"> 
    <apex:selectOptions value="{!RecordTypes }"/> 
      <apex:actionSupport event="onchange"
                          action="{!CallContactsEditPage}"
                          reRender="TheForm"/>
    </apex:selectList> <br/> 
  </apex:pageblock> 
 <h1>You have selected :<apex:outputLabel value="{!selectedRecordType}"
                                          id="TheForm"/></h1>
  </apex:form> 
</apex:page>

contactedit visual force page
-----------------------------
<apex:page standardController="Contact">
	<apex:sectionHeader title="Contact Edit" subtitle="{!Contact.Name}"></apex:sectionHeader>
	<p>Contacts not associated with accounts are private and cannot be viewed by other users or included in reports.</p>
	<apex:pageMessages></apex:pageMessages>
	<apex:form>
		<apex:pageBlock title="Contact Edit" id="pageBlock" mode="edit">
			<apex:pageBlockButtons>
				<apex:commandButton action="{!save}" value="Save"></apex:commandButton>
				<apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>
			</apex:pageBlockButtons>
			<apex:pageBlockSection columns="2" title="Contact Information">
				<apex:inputField value="{!Contact.Salutation}"></apex:inputField>
				<apex:inputField value="{!Contact.FirstName}"></apex:inputField>
				<apex:inputField value="{!Contact.Phone}"></apex:inputField>
				<apex:inputField value="{!Contact.LastName}" 
                                                 required="true"></apex:inputField>

			<apex:inputField value="{!Contact.MobilePhone}"></apex:inputField>
				<apex:inputField value="{!Contact.AccountId}"></apex:inputField>
				<apex:inputField value="{!Contact.LeadSource}"></apex:inputField>
				<apex:inputField value="{!Contact.Birthdate}"></apex:inputField>
				<apex:inputField value="{!Contact.Email}"></apex:inputField>
				<apex:inputField value="{!Contact.Title}"></apex:inputField>
				<apex:inputField value="{!Contact.Fax}"></apex:inputField>
				<apex:inputField value="{!Contact.Department}"></apex:inputField>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>
so when the user selects picklist value "record_type_1" the contactsedit page should be loaded with the selections made for recordtype "record_type_1"
Pls help me.

thanks
pooja

 
Best Answer chosen by pooja biswas
mritzimritzi
Following is a sample code which can render different layout based on selectList selected values,
you can change it to suit your needs. (It's pretty much in line with your requirement)

Apex:
public class TestClass2 {
    public Opportunity opp{get;set;}
    public boolean pb2Rendered{get;set;} // true -> show pb2
    public boolean pb3Rendered{get;set;} // true -> show pb3
    public String selectedRT{get;set;} // selected value of selectList
    public TestClass2(){
        opp = new Opportunity(Name='Some Random Name',stageName='Prospecting');
        pb2Rendered = pb3Rendered = false;
        selectedRT='';
    }
    public void save(){
        // your logic to save data here
        //you may choose to have different save functions for different record types
    }
    public void onChangeFnCall(){
        if(selectedRT=='abc'){
            pb2Rendered = true;
            pb3Rendered = false;
        }
        else if(selectedRT=='def'){
            pb2Rendered = false;
            pb3Rendered = true;
        }
        else{
            pb2Rendered = false;
            pb3Rendered = false;
        }
    }
}

VF:
<apex:page id="pg" controller="TestClass2">
    <apex:form >
        <apex:pageblock id="pb1">
            Select: <apex:selectList value="{!selectedRT}" size="1" multiselect="false">
                <apex:selectOption itemValue="" itemlabel="--None--"/>
                <apex:selectOption itemValue="abc" itemlabel="Abc"/>
                <apex:selectOption itemValue="def" itemlabel="Def"/>
                <apex:actionSupport event="onchange" action="{!onChangeFnCall}" />
            </apex:selectList>
        </apex:pageBlock>
        <apex:pageblock id="pb2" rendered="{!pb2Rendered}">
            Field1 (RecordType 1)<apex:inputText /><br/>
            Field2 (RecordType 1)<apex:inputText /><br/>
            Field3 (RecordType 1)<apex:inputText /><br/>
            <apex:pageBlockButtons >
                <apex:commandButton value="save1" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageblock>
        <apex:pageblock id="pb3" rendered="{!pb3Rendered}">
            Field1 (RecordType 2)<apex:inputText /><br/>
            Field2 (RecordType 2)<apex:inputText /><br/>
            Field3 (RecordType 2)<apex:inputText /><br/>
            <apex:pageBlockButtons >
                <apex:commandButton value="save2" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageblock>
    </apex:form>
</apex:page>



If this helps you out, please mark this Best Answer.

All Answers

J CesarJ Cesar

The purpose of record types is to display different fields, so you'll need 2 separate contact edit pages for your record types.
If you wish to display any content dynamically, use <apex:actionSupport event="onchange" reRender="PageBlockYouWishToReRender" action="{!DefineActionInController}" />
Of course replace the variables with whatever your criteria for re-render are and the function in your controller that will handle it. This tag goes into the picklist input field where users pick the record type, like this: <apex:inputField value="{!MyPicklist}><apex:actionSupport....... /></apex:inputField>

If you wish to redirect to 2 alternative pages, why not simply create 2 buttons with each linking to a different record page?

mritzimritzi
Following is a sample code which can render different layout based on selectList selected values,
you can change it to suit your needs. (It's pretty much in line with your requirement)

Apex:
public class TestClass2 {
    public Opportunity opp{get;set;}
    public boolean pb2Rendered{get;set;} // true -> show pb2
    public boolean pb3Rendered{get;set;} // true -> show pb3
    public String selectedRT{get;set;} // selected value of selectList
    public TestClass2(){
        opp = new Opportunity(Name='Some Random Name',stageName='Prospecting');
        pb2Rendered = pb3Rendered = false;
        selectedRT='';
    }
    public void save(){
        // your logic to save data here
        //you may choose to have different save functions for different record types
    }
    public void onChangeFnCall(){
        if(selectedRT=='abc'){
            pb2Rendered = true;
            pb3Rendered = false;
        }
        else if(selectedRT=='def'){
            pb2Rendered = false;
            pb3Rendered = true;
        }
        else{
            pb2Rendered = false;
            pb3Rendered = false;
        }
    }
}

VF:
<apex:page id="pg" controller="TestClass2">
    <apex:form >
        <apex:pageblock id="pb1">
            Select: <apex:selectList value="{!selectedRT}" size="1" multiselect="false">
                <apex:selectOption itemValue="" itemlabel="--None--"/>
                <apex:selectOption itemValue="abc" itemlabel="Abc"/>
                <apex:selectOption itemValue="def" itemlabel="Def"/>
                <apex:actionSupport event="onchange" action="{!onChangeFnCall}" />
            </apex:selectList>
        </apex:pageBlock>
        <apex:pageblock id="pb2" rendered="{!pb2Rendered}">
            Field1 (RecordType 1)<apex:inputText /><br/>
            Field2 (RecordType 1)<apex:inputText /><br/>
            Field3 (RecordType 1)<apex:inputText /><br/>
            <apex:pageBlockButtons >
                <apex:commandButton value="save1" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageblock>
        <apex:pageblock id="pb3" rendered="{!pb3Rendered}">
            Field1 (RecordType 2)<apex:inputText /><br/>
            Field2 (RecordType 2)<apex:inputText /><br/>
            Field3 (RecordType 2)<apex:inputText /><br/>
            <apex:pageBlockButtons >
                <apex:commandButton value="save2" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageblock>
    </apex:form>
</apex:page>



If this helps you out, please mark this Best Answer.
This was selected as the best answer
pooja biswaspooja biswas
Hi Jernej
Thanks for your reply.
As u said I will keep two command buttons, first command button will call page1(it will display fields of recordtype_1) and second one will call page2.
I would appreciate a piece of code would be helpful.

Thanks
pooja
pooja biswaspooja biswas
Hi mritzi
ur code has helped me understand a lot.
so if record_type_1 is selected from picklist then I will call page_1 and if record_type_2 is selected from picklist then I will call page_2 through a command button.
so I will create 2 different visualforce pages.
This part is fine but now I have a doubt.
for record_type_1, assume LeadSource=Web / Phone Inquiry
for record_type_2 , assume LeadSource=Partner referral / Purchase List

Now when I select record_type_1 from picklist, then how to make sure that page which loads has LeadSource with the above values only.

could u please highlight on this?

thanks
pooja biswas
 
J CesarJ Cesar
Hi Pooja,

What you're asking about is called dependent picklists - load picklist values based on another picklist. You can find good documentation on how to implement them here: https://help.salesforce.com/apex/HTViewHelpDoc?id=fields_defining_field_dependencies.htm&language=en_US

Here is syntax for command button in Visualforce: 
<apex:commandButton action="{!editRecordType1}" value="Record Type 1" id="editRecordType1"/>

Value is the label that displays on your button and action is the function that executes when you click on it. You define your custom action in your controller.

The tricky part of saving a record across 2 pages is that you will need to save the changes after the first page, you can achieve that by invoking standard Save function from your custom function.

If it's only dependent picklists you're after you actually don't need Apex and Visualforce, you can achieve it as an admin by following guidelines in the link I provided above.
mritzimritzi
@Pooja,

If you are sure, that each record type will have fixed dropdown options, you can have static picklist on those pages/ pageblocks:

On page1/pageBlock1 you will have:
<apex:selectList value={!objectVar__c.fieldName__c}>
    <apex:selectOption itemValue="Web" itemLabel ="Web"/>
    <apex:selectOption itemValue="Phone Inquiry" itemLabel ="Phone Inquiry"/>
</apex:selectList>
On Page2/Pageblock2 you will have:
<apex:selectList value={!objectVar__c.fieldName__c}>
	<apex:selectOption itemValue="Partner Referral" itemLabel ="Purchase List"/>
	<apex:selectOption itemValue="Partner Referral" itemLabel ="Partner Referral"/>
</apex:selectList>


Make sure the text inside itemValue attribute on VF Page is present and is exactly same in the picklist field of the object.
Change "value" attribute in the selectList as per you sObject Variable name & field

If this helps you out, please mark this as Best Answer.
pooja biswaspooja biswas
Hi mtrizi
This resolves my requirement.
Thanks