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
divya divyadivya divya 

Displaying Dependent Picklist in visualforce

My scenario is to diaplay the Product family field(Controlling field) and products as dependent field in visualforce page . Can anyone give me a suggestion to solve this issue. I am new to salesforce.
Thanks in Advance
 
Best Answer chosen by divya divya
Amit Chaudhary 8Amit Chaudhary 8
Hi Divya,

Please try below code.
<apex:page controller="ProductController">

<apex:form >

    <apex:pageBlock >
            <apex:pageblocksection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Product Group</apex:outputLabel>
                    <apex:selectList value="{!currentGroup}" size="1">
                        <apex:selectOptions value="{!ListGroups}"/>
                        <apex:actionSupport event="onchange" rerender="PB2" action="{!getProduct}" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
    </apex:pageBlock>

    <apex:pageBlock id="PB2">
        <apex:pageBlockTable value="{!lstProduct }" var="prod">
            <apex:column value="{!prod.Name}"/>
            <apex:column value="{!prod.family  }"/>
        
        </apex:pageBlockTable>
    </apex:pageBlock>



</apex:form>

</apex:page>
Class:-
public with sharing class ProductController 
{
        public string currentGroup {get; set;}
        public string currentProduct { get; set; }
        public List<Product2> lstProduct {get;set;}
        public ProductController () 
        {
            currentGroup = currentProduct  = null;
            lstProduct = new List<Product2>();
        }

        public List<SelectOption> getListGroups() 
        {
            List<SelectOption> options = new List<SelectOption> { new SelectOption('','-- Choose --') };
            for(Schema.PicklistEntry pe: Product2.Family.getDescribe().getPicklistValues()) 
            {
                options.add(new SelectOption(pe.getValue(),pe.getLabel()));
            }
            return options;
        }

        public void getProduct()
        {
            lstProduct.clear();
            lstProduct = [select id,name,family  from product2 where family = :currentGroup] ; 
        }  
    }
I also tested same code in my developer org which is working fine.
User-added image

Please let us know if this will help you

Thanks
Amit Chaudhary


 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Divya,

If field dependency you created on field level then please check below post for code.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_dependent_picklists.htm
<apex:page controller="sample">
    
    <apex:form >
    
    <apex:pageBlock>
        <apex:pageBlockSection columns="2">
            <apex:pageblockSectionItem>
                <apex:outputLabel value="State"/>
            </apex:pageblockSectionItem>        
            <apex:pageblockSectionItem>                
                <apex:selectList size="1" value="{!state}">
                    <apex:selectOptions value="{!states}"/>
                    <apex:actionSupport event="onchange" reRender="a"/>
                </apex:selectList>                
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem>
                <apex:outputLabel value="City"/>
            </apex:pageblockSectionItem>            
            <apex:pageblockSectionItem>
                <apex:selectList size="1" value="{!city}" id="a">
                    <apex:selectOptions value="{!cities}"/>
                </apex:selectList>
            </apex:pageblockSectionItem>            
        </apex:pageBlockSection>        
    </apex:pageBlock>

    </apex:form>

</apex:page>
public class sample
{
    public String state {get;set;}
    public String city {get;set;}

    public List<SelectOption> getStates()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- None ---'));        
        options.add(new SelectOption('TN','Tamil Nadu'));
        options.add(new SelectOption('KL','Kerala'));
        return options;
    } 
    
    public List<SelectOption> getCities()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state == 'TN')
        {       
            options.add(new SelectOption('CHE','Chennai'));
            options.add(new SelectOption('CBE','Coimbatore'));
        }
        else if(state == 'KL')
        {       
            options.add(new SelectOption('COA','Coachin'));
            options.add(new SelectOption('MVL','Mavelikara'));
        }
        else
        {
            options.add(new SelectOption('None','--- None ---'));
        }      
        return options;
    }       
}

How To Create Dynamic Dependent Picklist Of Objects Within Salesforce
1) https://www.minddigital.com/how-to-create-dynamic-dependent-picklist-of-objects-within-salesforce/
2) http://www.infallibletechie.com/2012/10/dependent-picklist-using-apex-in.html
3) https://www.sundoginteractive.com/blog/displaying-dependent-picklist-fields-on-a-visualforce-page
4) http://blog.deadlypenguin.com/blog/2012/07/09/dynamic-dependent-picklists-in-salesforce/

Let us know if this will help you

Thanks
Amit Chaudhary
divya divyadivya divya
Hi Amit Chaudhary
Thank you for you reply.  Actually my scenario is  i want to see a Product Family field as a picklist If i select any product family item it should show details of that products. I wrote a code
public with sharing class MyController {
        public string currentGroup {get; set;}
        public string currentProduct { get; set; }
        public MyController() {
            currentGroup = currentProduct  = null;
        }
        public List<SelectOption> getListGroups() {
            List<SelectOption> options = new List<SelectOption> { new SelectOption('','-- Choose --') };
            for(Schema.PicklistEntry pe:Product2.Family.getDescribe().getPicklistValues()) {
                options.add(new SelectOption(pe.getValue(),pe.getLabel()));
            }
            return options;
        }

        public List<SelectOption> getListProducts() {

            List<SelectOption> options = new List<SelectOption>();

            if(currentGroup == null || currentGroup == '')

                return options;

            options.add(new SelectOption('','-- Choose --'));

            for(Product2 p2:[select id,name from product2 where family = :currentGroup]) {
                options.add(new SelectOption(p2.id,p2.name));
            }
            return options;
        }  
    }

VF page:

public with sharing class MyController {
        public string currentGroup {get; set;}
        public string currentProduct { get; set; }
        public MyController() {
            currentGroup = currentProduct  = null;
        }
        public List<SelectOption> getListGroups() {
            List<SelectOption> options = new List<SelectOption> { new SelectOption('','-- Choose --') };
            for(Schema.PicklistEntry pe:Product2.Family.getDescribe().getPicklistValues()) {
                options.add(new SelectOption(pe.getValue(),pe.getLabel()));
            }
            return options;
        }

        public List<SelectOption> getListProducts() {

            List<SelectOption> options = new List<SelectOption>();

            if(currentGroup == null || currentGroup == '')

                return options;

            options.add(new SelectOption('','-- Choose --'));

            for(Product2 p2:[select id,name from product2 where family = :currentGroup]) {
                options.add(new SelectOption(p2.id,p2.name));
            }
            return options;
        }  
    }

 
divya divyadivya divya
Hi Amit

This is my VF page .

User-added image

I don't want to see Product as picklist i want to see details of the products.
divya divyadivya divya
Hi Amit
According to my requirement  I want product family field as controlling field then base on product family you want to see product detail on  in Tabuler formate
Amit Chaudhary 8Amit Chaudhary 8
Hi Divya,

Please try below code.
<apex:page controller="ProductController">

<apex:form >

    <apex:pageBlock >
            <apex:pageblocksection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Product Group</apex:outputLabel>
                    <apex:selectList value="{!currentGroup}" size="1">
                        <apex:selectOptions value="{!ListGroups}"/>
                        <apex:actionSupport event="onchange" rerender="PB2" action="{!getProduct}" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
    </apex:pageBlock>

    <apex:pageBlock id="PB2">
        <apex:pageBlockTable value="{!lstProduct }" var="prod">
            <apex:column value="{!prod.Name}"/>
            <apex:column value="{!prod.family  }"/>
        
        </apex:pageBlockTable>
    </apex:pageBlock>



</apex:form>

</apex:page>
Class:-
public with sharing class ProductController 
{
        public string currentGroup {get; set;}
        public string currentProduct { get; set; }
        public List<Product2> lstProduct {get;set;}
        public ProductController () 
        {
            currentGroup = currentProduct  = null;
            lstProduct = new List<Product2>();
        }

        public List<SelectOption> getListGroups() 
        {
            List<SelectOption> options = new List<SelectOption> { new SelectOption('','-- Choose --') };
            for(Schema.PicklistEntry pe: Product2.Family.getDescribe().getPicklistValues()) 
            {
                options.add(new SelectOption(pe.getValue(),pe.getLabel()));
            }
            return options;
        }

        public void getProduct()
        {
            lstProduct.clear();
            lstProduct = [select id,name,family  from product2 where family = :currentGroup] ; 
        }  
    }
I also tested same code in my developer org which is working fine.
User-added image

Please let us know if this will help you

Thanks
Amit Chaudhary


 
This was selected as the best answer
divya divyadivya divya
Hi Amit

Thank you for your reply. Its working good.