[SalesForce] Dynamic Apex for display dependent picklist in vf page

I have two picklist fields Category__c and SubCust__c(dependent on Category__c field). How to display this field values in vf page as multiselect from a Apex Class.

Currently both multiselect are published as static using dynamic apex.

Need help how to display as dependent.

enter image description here

public List<SelectOption> getDept()
    {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult departfield =  Activity_Tracker__c.Category__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){      
        options.add(new SelectOption(f.getLabel(), f.getValue()));
        }    
        return options;
    }



public List<SelectOption> getDept()
    {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult departfield =  Activity_Tracker__c.SubCust__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){      
        options.add(new SelectOption(f.getLabel(), f.getValue()));
        }    
        return options;
    }

Regards

Best Answer

Basically what you are asking about, it is called as dependent\controlling picklist fields, you need to specify on field definition level that second picklist is dependent and select values available for each values of controlling field.

So you may use either apex:inputField or apex:selectList. If you want to allow users to select several values at a time you may set multiselect="true"


UPD.

You have the same name for two methods, you need have different method names. You may use something like this

<apex:actionRegion >
    <apex:selectList value="{!controllingField}">
        <apex:selectOptions value="{!contFieldOptions}"/>
        <apex:actionsupport event="onchange"  rerender="depFieldParent" />
    </apex:selectList>
</apex:actionRegion>
<apex:outputPanel id="depFieldParent">
    <apex:selectList value="{!dependentField}">
        <apex:selectOptions value="{!depFieldOptions}"/>
    </apex:selectList>
</apex:outputPanel>

In the controller you may add more logic like this

public List<SelectOption> getDepFieldOptions() {
   List<SelectOption> options = new List<SelectOption>();
    Schema.DescribeFieldResult departfield =  Activity_Tracker__c.SubCust__c.getDescribe();
    List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
    for( Schema.PicklistEntry f : picklist){
        if ( controllingField != null ) { // you may want to add more logic here
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }
    }    
    return options;
}

UPD: You may still use your unaltered version of getDept() method as getContFieldOptions to retrieve option for controller field:

public List<SelectOption> getContFieldOptions() {
    List<SelectOption> options = new List<SelectOption>();
    Schema.DescribeFieldResult departfield =  Activity_Tracker__c.Category__c.getDescribe();
    List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
    for( Schema.PicklistEntry f : picklist){      
    options.add(new SelectOption(f.getLabel(), f.getValue()));
    }    
    return options;
}
Related Topic