function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RudraRudra 

Rerender picklists of a field for a specific record type in a visualforce page

I have two record types for Opportunity object RecordType1 and RecordType2. Lead Source field on Opportunity object has the following picklist values :
Web , Partner, Phone Inquiry, Partner Referral, Purchased List and Other.

I have created a visualforce page(using a custom controller)  and in that I have created a custom picklist for the Record type with picklist values RecordType1 and RecordType2..

The requirement is that : The user will select either of the recordtypes and depending on the that the picklist values for the field Lead Source will get rerendered in the visualforce page.

For Eg: if we select RecordType1 the picklist values will be : Web , Partner, Phone Inquiry
and for RecordType2 the picklist values will be : Partner Referral, Purchased List ,Other.

Thanks in advance!!!

Sonam_SFDCSonam_SFDC
You can create 2 selectlists  with selectoption - one for recordtype and other for the lead source picklist (http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_selectList.htm;
https://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_selectOption.htm)
on the visualforce page and use actionsupport (http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionSupport.htm) to rerender the lead source picklist depending on the value chosen in the recordtype selectlist.
VikashVikash (Salesforce Developers) 
Hi Rudra,

Here is a Sample code you can try:

Controller:
public with sharing class DependantPicklistCon
{

// Create strings to put our values in from the page
public string strRecordType         {get;set;}

private final Account Acc;

// The extension constructor initializes the private member
// variable Acc by using the getRecord method from the standard
// controller.
public DependantPicklistCon(ApexPages.StandardController stdController)
{  
    this.Acc = (Account)stdController.getRecord();
}

//Create our list of record types
public list<SelectOption> getRecordTypes()
{
    list<SelectOption> options = new list<SelectOption>();

    for(RecordType sRecordType : [SELECT Id, Name FROM RecordType WHERE sObjectType = 'Account'])
    {
        // Select options are added with key,value pairing
        options.add(new SelectOption(sRecordType.Id, sRecordType.Name));
    }
    return options;
}

public void updateRecordType()
{
    Acc.RecordTypeId = strRecordType;
}

}

Visualforce Page:

<apex:page standardcontroller="Account" extensions="DependantPicklistCon">
<apex:form id="TheForm">
    <apex:selectList value="{!strRecordType}" multiselect="false">
        <apex:selectOptions value="{!RecordTypes}"/>
        <apex:actionsupport event="onchange" action="{!updateRecordType}" rerender="TheForm"/>
    </apex:selectList>
    <apex:inputfield value="{!Account.AccountSource}"/>
</apex:form>

Hope this helps!!!!

Thanks
Vikash_SFDC
RudraRudra
Hi Sonam/Vikash,

Thanks for the reply.

@Vikash - I tried with your code and it seemed to work fine, but when I took a closer look I found out that it has one issue.

Scenario : In my case I had two Record types,
RecordType1 picklist values -->  Partner Referral, Purchased List.
RecordType2 picklist values --> Web, Phone Inquiry, Other
Name of the picklist filed used is AccountSource

I used the piece of code that you provided. The issue is that on changing the recordtype pisklist it is rerendering the field picklist values as expected but when
I am selecting a particular value in the field picklist by clicking  then  I try to change the record type it is getting added to the other record type.
For eg : In my case if I have RecordType1, the field AccountSource has the following picklists Partner Referral and Purchased List. Now if I select "Purchased List" by clicking on it, then if I try to change the record type to  RecordType2, then after rerendering the AccountSource field is showing the following values,
Web, Phone Inquiry, Other and Purchased List. Thus adding the selected vaue to the existing picklist values. This is clearly incorrect.

Basically what I am looking for is to find out a way to query and find out the picklist values of a field for a particular record type.
Is that possible ?
Can we query picklist values of a field depending on record type? If that is possible then we can very well achieve the required functionality.

Thanks