0

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

5
  • Not sure what you mean.
    – Patlatus
    May 7, 2015 at 9:02
  • What kind of multiselect you want? Can you share some screenshot or mockup of what you want to implement?
    – Patlatus
    May 7, 2015 at 9:03
  • Hi Patlatus.. I have updated my question.. May 7, 2015 at 10:20
  • Is your screenshot about what you would like to see or what do you currently see?
    – Patlatus
    May 7, 2015 at 10:50
  • Currently i am displaying them individually using dynamic apex, but i want Sub customer should get populated according value selected in Category.. May 7, 2015 at 10:53

2 Answers 2

1

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;
}
3
  • I have updated the code that i am using to display the values. But i need a logic where i can make it dependent.. May 7, 2015 at 10:58
  • Hi Patlatus.. I am bit consfused as the above method will only display the picklist values of SubCust__c. Can u please help me out in this.. May 7, 2015 at 11:19
  • Are you asking about getDept() \ getContFieldOptions() to retrieve option for controller field? You may still use your unaltered version of your method, I didn't include it before since I didn't have to make any changes inside of it. Now I have updated my answer to include it as well.
    – Patlatus
    May 8, 2015 at 7:41
0

GetPicklistValues() doesn't return dependent picklist values; it returns all the values under that picklist. Your current approach will not work.

One solution for this is via API which is described in detail here: Get lists of dependent picklist options in Apex

Another Option: I had a similar issue wherein I needed to get picklist values dependent on record types. I used the solution described here:http://bobbuzzard.blogspot.in/2012/01/record-type-picklist-values.html See if you can modify this to work for dependent picklists.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .