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
- Both the Dependent Picklist must be present inside Salesforce and Have all the required values added in it.
Advantages to this Solution
- Can create the thousands of Field Dependency within minutes
- 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
- 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).
- 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.
- 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.
- 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’}}
- 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
- The first Row in the CSV file contains the Object API Name however we are not using this in Apex.
- 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.
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.
How to create Metadata Service?
The code is already there in the blog post.
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?
1200K unique picklist values? Are you serious? Salesforce will not let you to have that much picklist inside a single field.But yes the above code will work
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.
If you already have picklist values available then you can use this approach and it will work perfectoly
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
Is this only an error or what you get in the debug log? Turn your debug log on and then check what is the error there.
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.
You do not need to migrate the code. Yes you can migrate dependencies using change set. You just need to add both picklists in the same change set in order to transfer field dependencies from org to another. Check this link – https://www.simplysfdc.com/2013/07/salesforce-field-dependencies-deployment.html
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.
parseCSVFile is the method. This is strange as it should either display the success message or error message.
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
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
I have given the class for the metadata service in my repo. Please use that