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
Tim Johnson-Reynolds 8Tim Johnson-Reynolds 8 

picklist values by record type

I'll preface this with the fact I am new to visualforce.

Is it possible to display picklist values on a visualforce page by recordtype using the standard controller (and optionally adding an extension)?

Currently picklist values load based on my default record type.

I would like to load the correct ones on the visualforce page, and additionally would like the option to change the record type on the visualforce page and have the picklists update with the correct options

I would like to do this using standard functionality ideally rather than load options via apex, or I would like to know that what I am trying to achieve is not possible.

Thanks

Tim

 
Best Answer chosen by Tim Johnson-Reynolds 8
Karan KeharKaran Kehar
Hi Tim,

You can try the below code for this.
 
public with sharing class AccountRecordType{
 
    public String selectedRT {get;set;}
    public List<SelectOption> recordTypeList {get;set;}
    public Account account {get;set;}
     
    public AccountRecordType(){
     
        account = new Account();
        recordTypeList = new List<SelectOption>();
        getRecordTypeList();
     
    }
     
    public void getRecordTypeList(){
          
        List<RecordType> rtList = [SELECT Id,Name FROM RecordType WHERE SObjectType='Account'];
        recordTypeList.add(new SelectOption('--None--', '--None--'));
        for(RecordType rt : rtList)
        {
            recordTypeList.add(new SelectOption(rt.Id, rt.Name));
        }
    }
     
     public void getPickListValues(){
      
        if(selectedRT != null){
            account = new account(RecordTypeId = selectedRT);
        }
    }
}
 
<apex:page controller="AccountRecordType">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:selectList value="{!selectedRT}" size="1" multiselect="false" label="Record Type" title="Record Type" id="recordTypes"> 
                    <apex:actionSupport event="onchange" action="{!getPickListValues}" reRender="categoryPicList" />
                    <apex:selectOptions   value="{!RecordTypeList}" /> 
                </apex:selectList>
                <apex:inputField id="categoryPicList" value="{!account.Type}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please mark this as the best answer if it resolves your concern.