Skip to main content
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

 
7 answers
  1. May 12, 2016, 6:48 AM
    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.
0/9000