1

Trying to create Dependent picklist as for some business requirement and salesforce limitation I cannot use standard Dependent picklist provided by salesforce.

I have two picklist fields on my visual force page. Picklist a & Picklist b. Picklist a has values 1,2,3,4 and b has values a,b,c

Now upon selection on picklist a value I want to filter Picklist B . For example if Picklist A has value 1 selected than only display b,c in picklist B

I have achieved this by following code .

My VF Page

    <apex:pageBlockSectionItem Id="ype">
                        <apex:outputLabel value="Type"/>                    

                            <apex:actionRegion >                              
                                <apex:inputField value="{!opp.Type__c}"  id="OppType"  > 
                                  <apex:actionSupport event="onchange" reRender="Rentinfo" immediate="True" />
                                </apex:inputField> 
                             </apex:actionRegion> 

                    </apex:pageBlockSectionItem>     
 <apex:pageBlockSection columns="1" title="Rentinfo" id="RentInfo">

                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Type" for="OppType"/>                 

                            <apex:selectList id="OppType" value="{!opp.Type__c}" size="1" >
                                <apex:selectOptions value="{!TypeValues}" />
                            </apex:selectList>

                    </apex:pageBlockSectionItem>

My Controller

 public List<SelectOption> TypeValues {get;set;}

public void setType() { 

        TypeValues = new List<SelectOption>();


        DescribeFieldResult fieldResult = Schema.Opportunity.Type__c.getDescribe();
        List<Schema.PicklistEntry> ple  = fieldResult.getPicklistValues();

        if(opp.L_Type__c == '1')        {
         TypeValues .add(new SelectOption('B', 'B'));
         TypeValues s.add(new SelectOption('C', 'C'));
        }
        else{
           TypeValues.add(new SelectOption('A', 'A'));
           TypeValues.add(new SelectOption('B', 'B'));
           TypeValues.add(new SelectOption('C', 'C'));  

        }
    }

Above code working absouloutly fine when select Picklist value in Field A and save that after coming back it only show filtered value, However as per requirement it needs same time filtering instead of save and come back.

Can some one please guide me on how to achieve that?

2

I got bored... and did the below, have a look, on change of the selectList it will auto update the second selectList... is this what you wanted?

VF Page:

<apex:page controller="SelectListController">
    <apex:form>
        <apex:selectList value="{!field1}" multiselect="false" size="1">
            <!-- apex:actionSupport will handle the changing of the selectList -->
            <apex:actionSupport event="onchange" action="{!getSecondaryPickVals}" reRender="field2" />
            <apex:selectOptions value="{!selectList}" />
        </apex:selectList>
        <apex:selectList id="field2" value="{!field2}" multiselect="false" size="1">
            <apex:selectOptions value="{!secondarySelectList}" />
        </apex:selectList>
    </apex:form>
</apex:page>

Class:

public class SelectListController {
    public String field1                         { get; set; }
    public String field2                         { get; set; }
    public List<SelectOption> selectList         { get; set; }
    public List<SelectOption> secondarySelectList{ get; set; }
    public Map<String, List<String>> picklistMap { get; set; }

    public SelectListController(){
        setupPickValMap();
        getPrimaryPickVals();
    }

    public void setupPickValMap(){
        picklistMap = new Map<String, List<String>>();
        picklistMap.put('1', new List<String>{'A', 'B'});
        picklistMap.put('2', new List<String>{'A', 'B', 'C'});
        picklistMap.put('3', new List<String>{'A', 'C'});
        picklistMap.put('4', new List<String>{'A', 'B', 'C', 'D', 'E'});
    }

    public void getPrimaryPickVals(){
        selectList = new List<selectOption>();

        for(String s : picklistMap.keySet())
            selectList.add(new SelectOption(s, s));
    }

    public void getSecondaryPickVals(){
        if(field1 != null){
            secondarySelectList = new List<selectOption>();

            for(String s : picklistMap.get(field1))
                secondarySelectList.add(new SelectOption(s, s));
        }
    }
}
  • hey I am trying this now. I appreciate your work. And sorry about this long explanation that make you feel bored. I have one question on your controller you don't seem like using Field2 to store value on and on your VF page your refering that field? how would that get values? – Ricky Jan 15 '16 at 17:46
  • @Ricky I didn't know what you wanted to do next... so just left it as is :) if you want to use it in another method you just reference it by the "field2" – Tim Jan 15 '16 at 21:18
  • @Timothy: I am just trying the same and adding just 1 more input field just before the </apex:form> tag. But I am not getting the dependent value. <apex:inputfield value="{!acc.name}"/> – RAHUL SRIVASTAVA Sep 18 '19 at 5:46
  • If you add in more input fields then you will need to cal methods to be able to prep the additional lists or data. – Tim Sep 19 '19 at 10:50

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.