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
Nayana Pawar 5Nayana Pawar 5 

Is it possible to make dependent picklist work in VisualForce page

Hello All,

Need help to display a dependent picklist in Visualforce page.
I have two picklist fields which are dependent fields. I want to show them on VF page and make picklist dependent.

Is there any way to do this.
Please help me.
Alexander TsitsuraAlexander Tsitsura
Hi Nayana,

Try to use apex:inputField for this

Thanks,
Alex
Nayana Pawar 5Nayana Pawar 5
I have used apex:inputField in my VF page still It's not working for me.
Vijay NagarathinamVijay Nagarathinam
Hi Nayana,

Yes it is possible to display the dependent picklist in the page, But you need to use standardcontroller in your visualforce page. If you are not using standardcontroller then you need to write the own logic for setup dependent picklist in visualforce page.

Thanks,
Vijay
Nayana Pawar 5Nayana Pawar 5
I am using Standard controller. Please check my code below.


            <apex:page standardController="Opportunity" extensions="CtrlDealerspricesPerModel" sidebar="false" >
            <apex:sectionHeader title="Quote Prices" subtitle="Select Model"/>
                 <apex:form id="theform" >
                     <apex:pageBlock id="theblock" >
                    
                         <div style="width:700px; margin:0 auto;">
                             <apex:outputText value="Select Make : "/> &nbsp;
                             <apex:selectList value="{!selectedItemValue}" multiselect="false" size="1">
                                 <apex:actionSupport event="onchange"  action="{!changeRec}" status="thestatus" reRender="theform" />
                                 <apex:selectOptions value="{!ownershipOptions}"/>
                             </apex:selectList>  &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
                             <apex:outputText value="Select Model: "/>
                             <apex:selectList value="{!selectedItemValueModel}" multiselect="false" size="1">
                                 <apex:actionSupport event="onchange"  action="{!gotoModel}" status="thestatus" reRender="theform" />
                                 <apex:selectOptions value="{!modelOptions}"/>
                             </apex:selectList>
                         </div>
                        
                     </apex:pageBlock>
                     <apex:actionStatus id="thestatus" startText="Please Wait It's Loading....."></apex:actionStatus>
                    
                     <apex:pageBlock >
                            <apex:pageBlockTable value="{!dealerQlist}" var="lst">
                            <apex:column title="Name Of Dealership" headerValue="Name Of Dealership">
                             <apex:outputLink value="https://cs6.salesforce.com/{!lst.Dealer__r.Id}">
                                  {!lst.Dealer__r.Name_Of_Manager__c}
                                </apex:outputLink>
                              </apex:column>
                             <apex:column value="{!lst.Stamp_Duty__c}" headerValue="Stamp Duty"/>
                            <apex:column value="{!lst.List_Price__c}" headerValue="List Price"/>
                            <apex:column value="{!lst.Dealer_Charges__c}" headerValue="Dealer Charges"/>
                            </apex:pageBlockTable>
                    </apex:pageBlock>      
                 </apex:form>


            </apex:page>

            public class CtrlDealerspricesPerModel{

                public string selectedItemValue{get;set;}
                public string selectedItemValueModel{get;set;}
                public Id recId{get;set;}
              public String makeOptions { get; set; }
                
                public List<Dealer_Quote__c> dealerQlist{get;set;}
               
                public Opportunity opp{get;set;}
                public CtrlDealerspricesPerModel(ApexPages.StandardController controller) {

                }
               // to add 10 contacts to dropdown list
                public List<Selectoption> getItems(){
                    List<Selectoption> opts = new List<Selectoption>();
                    opts.add(new selectoption('','--None--'));
                   
                    for(Opportunity con : [Select Id,Make__c From Opportunity]){   
                            opts.add(new selectoption(con.Id,con.Make__c));
                    }
              
                   return opts;
                }
               
               public pageReference changeRec()
                {
                    System.debug('===selectedItemValue==='+selectedItemValue);
                              dealerQlist = [select id,Dealer__r.Mobile_Phone_Of_Manager__c,Dealer__r.Name_Of_Dealership__c,Dealer__r.Name_Of_Manager__c,(select Id, Name__c,Price__c from Car_Factory_Acessory_Options__r), Name,Fleet_Discount__c,Stamp_Duty__c,List_Price__c,Dealer_Charges__c,Registration_CTP_and_Plate_fee__c, Opportunity__c from Dealer_Quote__c where Opportunity__r.Make__c=:selectedItemValue];
                    try
                    {
                      dealerQlist = [select id,Dealer__r.Mobile_Phone_Of_Manager__c,Dealer__r.Name,Dealer__r.Name_Of_Manager__c,(select Id, Name__c,Price__c from Car_Factory_Acessory_Options__r), Name,Fleet_Discount__c,Stamp_Duty__c,List_Price__c,Dealer_Charges__c,Registration_CTP_and_Plate_fee__c, Opportunity__c from Dealer_Quote__c where Opportunity__r.Make__c=:selectedItemValue];
                    }
                    catch(Exception e)
                    {
                    }
                    
                    return null;
                }

                public List<SelectOption> getownershipOptions()   
                {   
                    List<SelectOption> options =  new List<SelectOption>();   
                    options.add(new selectOption('None','--- None ---'));   
                    Schema.DescribeFieldResult fieldResult = Opportunity.Make__c.getDescribe();   
                    List<Schema.picklistEntry> ple = fieldResult.getPicklistValues();   
                    for(Schema.picklistEntry f:ple)   
                    {   
                        options.add(new selectOption(f.getLabel(),f.getValue()));                   
                    }   
                    return Options;   
                }
                public List<SelectOption> getmodelOptions()   
                {   
                    List<SelectOption> options =  new List<SelectOption>();   
                    options.add(new selectOption('None','--- None ---'));   
                    Schema.DescribeFieldResult fieldResult = Opportunity.Model__c.getDescribe();   
                    List<Schema.picklistEntry> ple = fieldResult.getPicklistValues();   
                    for(Schema.picklistEntry f:ple)   
                    {   
                        options.add(new selectOption(f.getLabel(),f.getValue()));                   
                    }   
                    return Options;   
                }   
                
                 public pageReference gotoModel()
                {
                    System.debug('===selectedItemValue==='+selectedItemValue);
                            
                    try
                    {
                      dealerQlist = [select id,Dealer__r.Mobile_Phone_Of_Manager__c,Dealer__r.Name,Dealer__r.Name_Of_Manager__c,(select Id, Name__c,Price__c from Car_Factory_Acessory_Options__r), Name,Fleet_Discount__c,Stamp_Duty__c,List_Price__c,Dealer_Charges__c,Registration_CTP_and_Plate_fee__c, Opportunity__c from Dealer_Quote__c where Opportunity__r.Model__c=:selectedItemValueModel];
                    }
                    catch(Exception e)
                    {
                    }
                    
                    return null;
                }
            }