1

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.

        <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;
            }
        }
0

As far as I know they work in VF pages, as long as standard controller is used in page controller.

  • No it's not working. I am using standard controller in my VF page. – Nayana Oct 27 '15 at 9:51
  • @Nayana, it would be better if you add your code to show where is an issue – kurunve Oct 27 '15 at 9:56
  • I have added my code here. Please check. – Nayana Oct 27 '15 at 10:55
0

I tried standardController and Yes its working.

VF Page:

    <apex:form >

        <apex:pageBlock mode="edit">

            <apex:pageBlockButtons >

                <apex:commandButton action="{!save}" value="Save"/>

            </apex:pageBlockButtons>

            <apex:pageBlockSection title="Dependent Picklists" columns="2">

            <apex:inputField value="{!account.industry}"/>

            <apex:inputField value="{!account.Subcategories__c}"/>

            </apex:pageBlockSection>

        </apex:pageBlock>

    </apex:form>

</apex:page>

Please let me know if you want more clarification and if you like this answer please mark it as a best answer :)

  • Hi Suyash, Thanks for reply. I am uploading my code here please check. – Nayana Oct 27 '15 at 10:41
  • Nayana please take a look on my code which i provided above you just need to create a dependent picklist in account object and with industry as a controlling picklist and create one subcategory it won't take time .I went through your code but i need to create custom objects and ques for verify the code. – Suyash Oct 27 '15 at 11:02

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.