Activity Forums Salesforce® Discussions How can I get the value of the dependent picklist ?

  • Naman

    Member
    April 29, 2016 at 7:55 am

    You can use Metadata API via REST to get states. In my knowledge, we cant directly query it in apex.

    https://success.salesforce.com/ideaView?id=08730000000keCTAAY

  • Piyush

    Member
    April 29, 2016 at 8:02 am

    Thanks for the reply. I also use metadata api to fetch the related value but it give all the state name and its StateCode.

  • Satyakam

    Member
    May 25, 2016 at 1:19 pm

    Hii Piyush,

    This is the scenario for dependent picklist.When you click on country picklist and select india than you will get related states to india in states picklist field and when you ll choose any state than you will get related cities to that state.

    code is here :

    Visualforce Page:

    <apex:page controller="sample">
    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection columns="2">
    <apex:pageBlockSectionItem >
    <apex:outputLabel value="country"/>
    </apex:pageBlockSectionItem>
    <apex:selectList size="1" value="{!country}">
    <apex:selectOptions value="{!countries}"/>
    <apex:actionSupport event="onchange" reRender="a"/>
    </apex:selectList>
    <apex:pageblockSectionItem >
    <apex:outputLabel value="state"/>
    </apex:pageblockSectionItem>
    <apex:pageblockSectionItem >
    <apex:selectList size="1" value="{!state}" id="a">
    <apex:selectOptions value="{!states}"/>
    <apex:actionSupport event="onchange" reRender="b"/>
    </apex:selectList>
    </apex:pageblockSectionItem>
    <apex:pageblockSectionItem >
    <apex:outputLabel value="city"/>
    </apex:pageblockSectionItem>
    <apex:pageblockSectionItem >
    <apex:selectList size="1" value="{!city}" id="b">
    <apex:selectOptions value="{!cities}"/>
    </apex:selectList>
    </apex:pageblockSectionItem>
    </apex:pageBlockSection>
    </apex:pageBlock>

    </apex:form>

    </apex:page>

    Controller:

    public class sample
    {
    public String country{get;set;}
    public String state {get;set;}
    public String city {get;set;}

    public List<SelectOption> getCountries()
    {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('None','--- None ---'));
    options.add(new SelectOption('US','USA'));
    options.add(new SelectOption('IN','India'));
    options.add(new SelectOption('AUS','Australia'));
    return options;
    }

    public List<SelectOption> getStates()
    {
    List<SelectOption> options = new List<SelectOption>();
    if(country=='US')
    {
    options.add(new SelectOption('NYK','NewYork'));
    options.add(new SelectOption('WSTN','Washington'));
    }
    else if(country=='IN'){
    options.add(new SelectOption('DLI','Delhi'));
    options.add(new SelectOption('MUM','Mumbai'));
    }
    else if(country=='AUS'){
    options.add(new SelectOption('SYD','Sydney'));
    options.add(new SelectOption('MLB','Melburn'));
    }
    return options;
    }
    public List<SelectOption> getCities()
    {
    List<SelectOption> options = new List<SelectOption>();
    if(country == 'US')
    {
    if(state == 'NYK'){
    options.add(new SelectOption('C1','City1'));
    options.add(new SelectOption('C2','City2'));
    }
    else if(state == 'WSTN')
    {
    options.add(new SelectOption('C3','City3'));
    options.add(new SelectOption('C4','City4'));
    }
    }
    else if(country == 'IN')
    {
    if(state== 'DLI'){
    options.add(new SelectOption('C5','City5'));
    options.add(new SelectOption('C6','City6'));
    }
    else if(state=='MUM'){
    options.add(new SelectOption('C7','City7'));
    options.add(new SelectOption('C8','City8'));
    }
    }
    else if(country=='AUS'){
    if(state=='SYD'){
    options.add(new SelectOption('C9','City9'));
    options.add(new SelectOption('C10','City10'));
    }
    else if(state=='MLB'){
    options.add(new SelectOption('C11','City11'));
    options.add(new SelectOption('C12','City12'));
    }
    }

    return options;
    }
    }

    i hope this code will help you.

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos