8

In a visualforce page, picklist values can be displayed based on the profile of logged-in user and record type settings simply by using something like..

<apex:inputField id="testid" value="{!Case__c.Status__c}"> 

But I am seeking for some tips to achieve following in a single VF page...

  • In picklist Field A, user will select one record type from a list of record types (These record types for an object would have already been configured in the back end) ...
  • In the same page, another Picklist field B (whose values are dependent on the record type) gets populated with a list of values based on the record type selected in Field A...(The values displayed in Field B would have already been configured in the back end)....

Could it be done in a single VF page without or with the use of an extension? If so, any hints or useful links?

Thanks in advance for any insights!!

  • Mitch/Emilio, I used apex logic with minimal change in VF page. Please find the link below. The problem is, after selecting one of the record type 'International Conference' , the region picklist displays East,Europe,Asia,South America. In back end only Europe, Asia, and South America has been added for International Conference. But I am guessing, since the East was the first option being displayed for the record type chosen before the 'international conference'..it doesn't get removed even if the form rerenders. A bug or there's a way to avoid it? Youtube watch?v=nwRlrpztqAo Thanks!! – Raish Oct 17 '13 at 9:24
5

yes you can do this through the use of a standard controller and a controller extension. As described by Emilio you would us an actionsupport tag on the VFP to update the second picklist. Here is an example of a solution that you can try, I have tried to include comments wherever it might need explanation, comment if you want anything further explained. One thing to keep in mind is that there is about a .5 second delay in the picklist updating so you might want to add an apex:actionstatus, I will leave that for you to work out.

VFP

<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>

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;
}

}
  • Mitch, Thank you for your insights and thorough example. Will try it out and update it here :) – Raish Oct 16 '13 at 17:50
3

One easy way accomplish this requirement is by creating a Visualforce page and a controller extension wich contains one method to obtain the current recordtype and other method to create your lists depending on the current recordtype, then you only need to call your methods from your visualforce page, you can use the apex:actionSupport tag to call your apex oncomplete, onchange, etc.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.