1

I have a visualforce page and two picklist fields. Field 1 contains values as A,B and C. Field 2 contains values as X and Y.

I want to have both X and Y in field 2 when option A/B is selected in field 1 and populate Y when option C is selected.

I tried doing this using field dependency.But Y is not getting populated.It always populates the default value.

Your help is aprreciated.

1

You can do this with <apex:actionSupport as below. The second <apex:selectList is populated based on the value selected in First <apex:selectList. Hope this helps.

VF Page

<apex:page controller="picklistController">
    <apex:form id="form">
        <apex:selectList id="selected_list" value="{!dropdown1}" required="false" size="1">
            <apex:selectOption itemvalue="None" itemLabel="--None--"/>
            <apex:selectOption itemvalue="A" itemLabel="A"/>
            <apex:selectOption itemvalue="B" itemLabel="B"/>
            <apex:selectOption itemvalue="C" itemLabel="C"/>
            <apex:actionSupport event="onchange" reRender="form" action="{!updateDropdown}"/>
        </apex:selectList>
        <apex:selectList id="dropdown2"  required="false" size="1">
            <apex:selectOption itemvalue="None" itemLabel="--None--"/>
            <apex:selectOptions value="{!items}" />
        </apex:selectList>        
    </apex:form>
</apex:page>

Controller

public class picklistController {
    public String dropdown1{get; set;}
    public List<SelectOption> options;
    public void updateDropdown() {
        if(dropdown1 == 'A' || dropdown1 == 'B' ){
            options = new List<SelectOption>();
            options.add(new SelectOption('X','X'));
            options.add(new SelectOption('Y','Y'));
        }else if(dropdown1 == 'C'){
            options = new List<SelectOption>();
            options.add(new SelectOption('Y','Y'));
        }    
    }
    public List<SelectOption> getItems() {
        return options;
    }    
}
1

I would have done it using selectList for the dependent pickList populated with a dynamic apex.

Visual force page

<apex:page standardController="MyCustomObject__c" extensions="PickListExtension">
<apex:form title="Title book">
    <apex:pageBlock title="Title">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}"  value="Save"/>
        </apex:pageBlockButtons>

        <apex:pageBlockSection columns="2">
            <apex:selectList multiselect="false" value="{!MyCustomObject__c.Field1__c}" size="1">
                  <apex:selectOptions value="{!Field1Items}"/>
                  <apex:actionSupport event="onchange" reRender="depfield"/>
            </apex:selectList>
            <apex:outputPanel id="depfield">
                <apex:selectList value="{!MyCustomObject__c.DepField__c}" size="1">
                  <apex:selectOptions value="{!DepItems}" />
                </apex:selectList>
            </apex:outputPanel>

        </apex:pageBlockSection>

  </apex:pageBlock>

Controller

public class PickListExtension{
ApexPages.StandardController stdCont;
MyCustomObject__c myObject;


//Map Dependency. 
//I would like to know how to build this dynamically
Map<String, Set<String>> dependencyMap=new Map<String, Set<String>>();

public PickListExtension (ApexPages.StandardController stdCont){
    this.stdCont=stdCont;
    this.myObject=(MyCustomObject__c)stdCont.getRecord();

    //Pupulating dependency map
    //It would be interesting if this could be done dynamically

    Set<String> depItems1=new Set<String>();
    depItems1.add('X');
    depItems1.add('Y');
    dependencyMap.put('A', depItems1);
    dependencyMap.put('B', depItems1);

    Set<String> depItems2=new Set<String>();
    depItems2.add('Y');
    dependencyMap.put('C', depItems2);


}

public List<SelectOption> getField1Items(){
    List<SelectOption> options=new List<SelectOption>();
    Schema.DescribeFieldResult field1=MycustomObject__c.Field1__c.getDescribe();
    List<PickListEntry> pickVals=field1.getPickListValues();
    for(PickListEntry pickVal: pickVals){
        options.add(new SelectOption(pickVal.getLabel(), pickVal.getValue()));
    }
    return options;
}

public List<SelectOption> getDepItems(){
    List<SelectOption> options=new List<SelectOption>();
    Schema.DescribeFieldResult field1=Mycustomobject__c.DepField__c.getDescribe();
    List<PickListEntry> pickVals=field1.getPickListValues();
    Set<String> AcceptableValues=dependencyMap.get(book.Field1__c);
    for(PickListEntry pickVal: pickVals){
        if(AcceptableValues ==NULL  || AcceptableValues.contains(pickVal.getValue())){
            options.add(new SelectOption(pickVal.getLabel(), pickVal.getValue()));
        }
    }
    return options;
}  
}

But it remain one problem: How to apply the the depency dynamicly on the dependent pickList to take the value of the first pickList into consideration

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.