function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael Hedrick 2Michael Hedrick 2 

Dependency Picklist Value Issues on VF Page

Hello,
I am having a couple issue on my VF page in regard to rendering Dependant Picklist Value,
The main picklist filed is fine.  But once a value is select the dependant picklist field(Category) are not correct.
Either it does not show or it is duplicated multiple time.

Also, the Last picklist field SubCategory is not populating any values at all and I am not sure if its the VF or Apex Code.
Any help is appreciated,
M

Controller:
public class MerchandisingController {
    
    
    public string Merchandise{ get; set; }
    public string Category{ get; set; }
    public string SubCategory { get; set; }
       
    public MerchandisingController (ApexPages.StandardController controller) 
    {
        Merchandise= Category= SubCategory = null;    
    }

 public List<SelectOption> getListMerch() 
     {
        List<SelectOption> options = new List<SelectOption> { new SelectOption('','-- Choose Merchandise --') };
        for(Schema.PicklistEntry pe:Merchandising__c.Merchandise__c.getDescribe().getPicklistValues()) {
            options.add(new SelectOption(pe.getValue(),pe.getLabel()));
        }
        return options;
    }
    
    
     public List<SelectOption> getListCategory() 
     {
        List<SelectOption> options = new List<SelectOption>();
        if(Merchandise == null || Merchandise == '')
            return options;
        options.add(new SelectOption('','-- Choose Category --'));
        for(Merchandising__c cat:[select id,Merchandise_Category__c from Merchandising__c where Merchandise__c= :Merchandise]) {
            options.add(new SelectOption(cat.id,cat.Merchandise_Category__c ));
        }
        return options;
    }
    
    public List<SelectOption> getListSubCategory() 
    {
        List<SelectOption> options = new List<SelectOption>();
        if(Category==null || Category== '')
            return options;
        options.add(new SelectOption('','-- Choose SubCategory --'));
        for(Merchandising__c sub:[select id,Merchandise_SubCategory__c from Merchandising__c where Merchandise_Category__c = :Category]) {
            options.add(new SelectOption(sub.id,sub.Merchandise_SubCategory__c));
        }
        return options;
    }
    
 }

Visualforce Page
<apex:page standardController="Merchandising__c" extensions="MerchandisingController">

      <apex:form id="Merchandising">
        <apex:sectionHeader title="Choose Merchandizing"/>
        <apex:pageBlock title="Demo Page">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Merchandise</apex:outputLabel>
                    <apex:selectList size="1" multiselect="false" value="{!Merchandise}">
                        <apex:selectOptions value="{!ListMerch}"/>
                        <apex:actionSupport reRender="Merchandising" event="onchange"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Category</apex:outputLabel>
                    <apex:selectList value="{!Category}" size="1" multiselect="false">
                        <apex:selectOptions value="{!ListCategory}"/>
                        <apex:actionSupport reRender="Merchandising" event="onchange"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >SubCategory</apex:outputLabel>
                    <apex:selectList value="{!SubCategory}" size="1" multiselect="false">
                        <apex:selectOptions value="{!ListSubCategory}"/>
                        <apex:actionSupport reRender="Merchandising" event="onchange"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
 </apex:page>


 
Best Answer chosen by Michael Hedrick 2
Neha AggrawalNeha Aggrawal
Hi Michael,

May be I am missing something, but the fields Merchandise__c, Merchandise_Category__c, Merchandise_SubCategory__c all are custom fields in the Merchandising__c object, you can just display them using standardController. Is there any other logic in your extension ?
<apex:page standardController="Merchandising__c">

      <apex:form id="Merchandising">
        <apex:sectionHeader title="Choose Merchandizing"/>
        <apex:pageBlock title="Demo Page">
            <apex:pageBlockSection columns="1">
                
              <apex:inputField value="{!Merchandising__c.Merchandise__c}"/>
              <apex:inputField value="{!Merchandising__c.Merchandise_Category__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
 </apex:page>

 

All Answers

Neha AggrawalNeha Aggrawal
Hi Michael,

May be I am missing something, but the fields Merchandise__c, Merchandise_Category__c, Merchandise_SubCategory__c all are custom fields in the Merchandising__c object, you can just display them using standardController. Is there any other logic in your extension ?
<apex:page standardController="Merchandising__c">

      <apex:form id="Merchandising">
        <apex:sectionHeader title="Choose Merchandizing"/>
        <apex:pageBlock title="Demo Page">
            <apex:pageBlockSection columns="1">
                
              <apex:inputField value="{!Merchandising__c.Merchandise__c}"/>
              <apex:inputField value="{!Merchandising__c.Merchandise_Category__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
 </apex:page>

 
This was selected as the best answer
Michael Hedrick 2Michael Hedrick 2
Hello Neha,
Thank you for the reply. I need to be able to assign the VF page to a button on the Account object.  Does using apex:inputField component still allow for picklist with dependancies?
Thanks,
M
Neha AggrawalNeha Aggrawal
Yes, it does. The code I posted above, Merchandise Category field is dependent picklist field on Merchandise field.
Michael Hedrick 2Michael Hedrick 2
Perfect.  But if I keep the 'Merchandising__c' object as the Standard Controller I cannot reference it on an Account Object button correct?
Cheers,
M
Michael Hedrick 2Michael Hedrick 2
I used the URL option /apex/Visualforce page
Now just need to come up with save and cancel funtionality.
Thanks,
M