How to Create and Update dependent picklist values using Apex – Metadata API?

on

|

views

and

comments

Scenario

We need to create a dependent picklist between 2 picklists into Salesforce Org, you all might be thinking that you do not need to write apex and it can be done using out of functionality but in my case, we have more than 1K values in both picklists and can be added/removed in future in the short dynamic dependent picklist. In final we have to create 4 dependent picklists on the different objects.

Solution

On the first attempt, I thought to accomplish this manually hahaha I knew that it is a time-consuming process.  After spending hours on this and doing brainstorming I came with the below solution.

Final Solution

We decided to write the apex code to do the same which also can be used to create dependent picklists for any object in the future.

Prerequisite to use this solution

  1. Both the Dependent Picklist must be present inside Salesforce and Have all the required values added in it.

Advantages to this Solution

  1. Can create the thousands of Field Dependency within minutes
  2. If you have Multi Level-Dependent Picklist then You can easily create the dependency using the above solution All you need to do is prepare the CSV file in the correct format.

Special Thanks to Avi Rai for helping me in this solution.

Steps

  1. Create both picklists into the Object with the actual values into them and also define the dependency between the fields(which is controlling and which one is dependent).
  2. Create a Custom Setting OR CustomMetdata Types to store Controlling picklist as Name and dependent values in a separate field so that it can be used in the class can be easily managed in the future.
  3. OR if you do not want to create then prepare the .csv file with two columns 1) Controlling Picklist Value and 2) Dependent Picklist Value.
  4. All we need to do is to prepare the Map with Dependent Picklist value as Key and List of Controlling field values as Value. For Example – Map<String, List<String>> {‘ACT’ => {‘Austria’, ‘Another Country’}}
  5. Copy VF and Class code from here and see code comments!

Save and Run VF page to create or update field dependent picklist values.

For this tutorial, I did have used the .csv and added the appropriate comments in the code to use the .csv file.

Tips to Prepare CSV file

  1. The first Row in the CSV file contains the Object API Name however we are not using this in Apex.
  2. In the Second Row, First Column will be the Parent Field API Name in My case “AccountSource” & the Second Column value will be the Field API Name of the Dependent Picklist in My Case “Active__c”

Screenshot for the sample .csv file

Link to Code that I have used -> https://github.com/amitastreait/Picklist-Field-Dependency

Here is the VF Outcome

Here is the Sample File that I have used for the demo

Here is the output from the above code

That’s it folks, happy reading

#Salesforce #Salesforce #DeveloperGeeks

If you have any queries or issue please come up in comment section.

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to start your AI Journey?

Table of Contents Introduction Are you tired of the same old world? Do you dream of painting landscapes with your code, composing symphonies with data, or...

The Secret Weapon: Prompt Engineering: 🪄

Table of Contents Introduction Crafting the perfect prompt is like whispering secret instructions to your AI muse. But there's no one-size-fits-all approach! Exploring different types of...

How to Singup for your own Production org?

Let's see how we can have our salesforce enterprise Salesforce Org for a 30-day Trial and use it as a Production environment. With the...

Recent articles

More like this

18 COMMENTS

  1. Hi, sorry for being dense, I’m not an experienced developer, but how do you adapt for multi-level dependencies? Like what if you are working with up two four levels of dependent picklists?

    • Hello, What you need to do is start working on one dependency at one level at a time and once the first level dependency is completed then start working on the second level dependency and so on. Hope this will help you to achieve multi-level dependency.

  2. Hi @sfdc panther,

    Here i am also having a scenario, where i am having dependent picklist consists of 1200K values , and parent picklist is having 450+ values, is still can be achieved?
    Do i need to add all the values to be existed in the picklist before i proceed to implement the code?

  3. Yes, We had extended the limit of picklist values by raising a case. What if i have mapped few values and others need to be mapped. But few values i already inserted through workbench but did not mapped.

  4. Hello, I have followed all the steps, but I receive this error:

    Web service callout failed: Failed to get next element
    Error is in expression ‘{!updatePicklistField}’ in component in page uploadpicklistdependant: Class.MetadataService.MetadataPort.updateMetadata: line 13255, column 1
    Class.MetadataPicklistController.updatePicklistField: line 72, column 1

    Could you please help me?
    Thanks in advance.
    Regards,
    Rosario

  5. Hi, does this code need to be migrated into production for this to work once it has gone live? Or can this all be done in a Dev environment and then you just migrate the dependency as you usually would? Just keen to keep the code down in Production.

  6. Hi, how do you prepare the map in step 4? When I run the VF, I never get confirmation that picklist values have been updated.

  7. Hi!! Thanks for the tutorial!!

    Just a couple of questions:
    – This worked flawlessly if the controlling and dependent picklists have their own values, but what if they get the values for a Global Picklist? In that case, I get this error in the debug logs: “|DEBUG|Cannot set picklist values when a global picklist is referenced on the field.”. Should then the code be extended to not only call the Metadata API with the CustomField, but with the GlobalValueSet as well?
    – Also, another question. Can this code be extended to also autonomously create beforehand picklist values in Salesforce if in the CSV appear some values that do not exist in the picklist field /Global Value Set in Salesforce?

    Finally, is there any reference in the code or how to user Custom Setting/Metadata instead of CSV?

    Thanks again for the help and the idea!

    • Hello Josemi,

      1. Currently the code only uses the picklist values which are being reference from the field not from Global Picklist
      2. No, we are not creating any picklist values.

      At present this code only uses CSV not custom metadata/settings

  8. Hi thanks for the tutorial !!
    Getting an error regarding “Web service callout failed: Unable to find apex schema info” Can you please help me out with that my requirement is on urgent basis .
    Thank you

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential