Dependent Picklist fields

How to Create Dependent Picklist fields In Lightning Component

Lightning Component

In this post we are seeing that how to create custom reusable dependent picklist fields in salesforce lightning component .

Prerequisites : basic understanding of Lightning Component. And apex Programming

Dependent Picklist fields

This post was inspired by an article found here: [ Thanks to ]

Step  0 : Setup Picklist field dependency

In this post , first i created 2 custom pick list fields on Contact object

country__c AND state__c

And then set field dependency on that fields. there country is the controller field  and state is the dependent field.

Dependent Picklist fields

Step  1 : Create apex controller / Helper classes 
1. Helper class for decode the bit set for validFor.

2. Wrapper Class which is use to deserialize the JSON format.

3. Server side controller for Lightning component

you don’t have need to change anything to above apex classes. 🙂

Step  2 : Create Lightning Component



  • See code comments.
  • You just need to change the object API Name on line number 11 [DependentPicklist.cmp].
  • And change dependent field and controller field API Names on line number 7 [DependentPicklistController.js].

it’s looks like huge but actually it’s easy 🙂

Dependent Picklist fields

Like our facebook page for new post updates.? & Don’t forget to bookmark this site for your future reference.

if you have any suggestions or issue with it, you can post in comment box. ?


(Visited 14,836 times, 1 visits today)


  • Hi.. Thank you for the solution.

    I am using the same code and it is working for me but it is giving below error if i have large number of records in picklist value. it would be great if you can explain what could be the reason. Please let me know if you need more information.


  • Hi.. This code works perfect for me but if i am using this for the filed which having large number of records, this is throwing below error. Please suggest what can be the reason.

    Do let me know if you need more information. Thanks

  • The issue is on line 48 of the testBits method in the BitSet class.  Some of the “validFor” values contain a “/” instead of an alphanumeric character, at least for me.  One such example for me is a value for objJson.validFor of “5/sA”.  When it tries to de-reference that value on line 48 of the testBits method, the return is a “null” value for the “/” character.  That’s the WHY…now to find the fix…

  • Solution:  Another key/value pair needs to be added to the “alphaNumCharCodes” variable:

    ‘/’ => ’47’

    Boom!  Works like a charm now!

    • HI Adam,
      Yes Boom! Now, thanks for your most valuable feedback and this solution. i will update it in above post as well.
      thanks again for sharing it with us 🙂

  • Hi!

    I tried your code however I am not getting the proper mapping. I can only seem to get the states for US and it becomes dependent under different countries. I am only using the Account Object and the standard ShippingCountry and ShippingState


    Please help.



      • I copied your code as is and the only difference is that I put the picklistentry wrapper and the controller in one class since i will only need the method that gets the picklist values added in my existing class.

        It gets all the countries fine, however the states i am getting are only US states. and they are mapped not only to US but to some other countries as well.

        The object i used is contact but the fields i passed are the standard ones and not custom like your above example. these fields are: MailingCountryCode and MailingStateCode.

      • Hi Piyush, it is not working for me as well, I am getting wrong state mapped to other countries. Do you have any solution for this ?

  • This solution worked for me, mostly.  But a couple of selections were not mapping correclty, or at all.  The issue seems to be the characters (‘/’ and ‘+’).  Adam was on the right trail above, but I think he got the values wrong.  The values below worked for me.

    Base64CharCodes.put(‘+’, 62);
    Base64CharCodes.put(‘/’, 63);

    This was not my deduction, but found in the comments at:

    Great solution with many people contributing.


    • Hi Adam ,

      even after adding this sentence
      Base64CharCodes.put(‘+’, 62);
      Base64CharCodes.put(‘/’, 63);
      for me its not taking proper dependency values..even i tried the code of the above link . there are also i am facing some issue. Can you please help me

  • Thank for your sample.
    I have a question.
    Your code sample, is for 2 dependent picklist, if I have 3 dependent picklist or 4, how to adapt the code ?

  • Awesome!

    But Is it possible to controller multiple dependent field by one controlling field. like Country > State and country > City

      • Thanks for great post:)
        i have tried to to controller multiple dependent field by one controlling field. like Country > State and country > City in same component but am getting same values in both city and state
        ex:country =india then state =”delhi” city =”goa” need to displayed but it displaying only delhi in state and city could you please look in to this

  • Thanks for the great post 🙂

    i have tried to implement  controller multiple dependent field by one controlling field. like Country > State and country > City in same lighting component but am getting same value in both state and country ex:if country= india then state=delhi  city=goa need to be displayed but both state and city displaying as delhi could you please look in to it

  • This.IS.AMAZING! Thank you!

    I’m struggling to make this work for a three tier dependant picklist… could you help?

    Many thanks

  • Would this be possible to setup to use a .design file to set the fields/dependencies? I have a set of 8 dependent picklists and am trying to avoid creating 8 versions to pull everything off.

  • Hi Piyush,

    Thanks a ton for the awesome post. But this utility doesn’t work for all scenarios or all picklist values. I found that if the ‘valueFor’ of a picklist is 8 characters long, the Bitset class fails to return any index. So below is the class which I tried and it works for me. This class from salesforce API helped me to avoid the Bitset class


    • Hi Ashok,
      Can you please tell me what the use of class DealRegistrationAddProductsController and where is the code of this class. can you please help me out regarding this class.

  • Hi Piyush,

    Thanks for the awesome post, but the BitSet class does not return the Index value when ‘valueFor’ is 8 characters long. So I tried this and it works.
    This apex library helped me avoiding the Bitset class maps.

    Public class ApexDependentPicklistUtil
    public String validFor;
    public static Map<String, List> getDependentOptionsImpl(String objApiName, String contrfieldApiName, String depfieldApiName)
    Map<String, List> controllingInfo = new Map<String, List>();
    Schema.SObjectType objType = Schema.getGlobalDescribe().get(objApiName);
    Schema.DescribeSObjectResult describeResult = objType.getDescribe();
    Schema.DescribeFieldResult contrfieldApiNameInfo = describeResult.fields.getMap().get(contrfieldApiName).getDescribe();
    Schema.DescribeFieldResult depfieldApiNameInfo = describeResult.fields.getMap().get(depfieldApiName).getDescribe();
    List controllingValues = contrfieldApiNameInfo.getPicklistValues();
    List dependentValues = depfieldApiNameInfo.getPicklistValues();
    for(Schema.PicklistEntry currControllingValue : controllingValues)
    controllingInfo.put(currControllingValue.getLabel(), new List());

    for(Schema.PicklistEntry currDependentValue : dependentValues)
    String jsonString = JSON.serialize(currDependentValue);

    ApexDependentPicklistUtil info = (ApexDependentPicklistUtil) JSON.deserialize(jsonString, ApexDependentPicklistUtil.class);

    String hexString = EncodingUtil.convertToHex(EncodingUtil.base64Decode(info.validFor)).toUpperCase();

    Integer baseCount = 0;

    for(Integer curr : hexString.getChars())
    Integer val = 0;
    if(curr >= 65)
    val = curr – 65 + 10;
    val = curr – 48;
    if((val & 8) == 8)
    controllingInfo.get(controllingValues[baseCount + 0].getLabel()).add(currDependentValue.getLabel());
    if((val & 4) == 4)
    controllingInfo.get(controllingValues[baseCount + 1].getLabel()).add(currDependentValue.getLabel());
    if((val & 2) == 2)
    controllingInfo.get(controllingValues[baseCount + 2].getLabel()).add(currDependentValue.getLabel());
    if((val & 1) == 1)
    controllingInfo.get(controllingValues[baseCount + 3].getLabel()).add(currDependentValue.getLabel());
    baseCount += 4;
    System.debug(‘ControllingInfo: ‘ + JSON.serializePretty(controllingInfo));
    return controllingInfo;

  • Can someone please give little explanation about the logic. I am also not able to understand how ValidFor Field is produced?


    THank you

  • hi Ashok Kumar Nayak

    I am getting : Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 – 9), ‘$’, ‘_’.

    if(curr >= 65){
    val = (curr) – (65 + 10);
    val = curr – 48;


    can you advise why


    thanks in advance.


  • Hi sfdcmonkey,

    great post.

    I have want to implement this to , dependent hierarchy (field dependent on previous one) …
    field 1 ->field 2 ->field 3 – field 4

    if its one filed controlling one then i can use the fetchpicklist on Init twice etc.. but this is different scenario.

    how could i implement the logic pls ?


    • Hi
      Did you find anything on dependent hierarchy for more than 2 fields, please share if you find anything on same.

  • Hi sfdcmonkey,,

    This code is not working for large amount of values for picklist .This code is not working if we have more than 24 values.This code is working till 24 values.

    Please provide the other solution


    Thanks $ Regards,




  • Im getting the below error while executing the code

    This page has an error. You might just need to refresh it. Unable to find ‘doInit’ on ‘compound://c.DependentPicklist’. Failing descriptor: {markup://c:DependentPicklist}

    any suggestions ?


  • Got the below error while executing

    This page has an error. You might just need to refresh it.
    Unable to find ‘doInit’ on ‘compound://c.DependentPicklist’.
    Failing descriptor: {markup://c:DependentPicklist}

    any suggestions ?

  • Love this code.. I’d really like to know how to filter for values only applicable to record type?  I also have several fields that I want to use this for all of which will be data entered in a single flow and would love to be able to reuse the code for each screen field

  • This isn’t working with Standard Country and State picklist values because of large data set. Please provide some solution if any one has implemented.

Leave a Reply