+ Start a Discussion
khillan bhardwajkhillan bhardwaj 

Getting dependent picklist values by Apex


Hi,

I want to retrieved dependent picklist value.I am able to do it by javascript. but i need to use in trigger. In trigger you know we can not excute javascript.
If any one have sample code to get depenedent picklist(either Schema or MetaData Api) values please let me know.It is urgent.

Thanks
NagaNaga (Salesforce Developers) 
Dear Khillan,

Please see the below sample code:

And also follow this link :

http://blog.deadlypenguin.com/blog/2012/07/09/dynamic-dependent-picklists-in-salesforce/


public with sharing class ProductUtils {
static public List<Product__c> getAllProducts(Boolean includeEOL) {
//This is done since the formula field cannot return a boolean
Integer currentlySupported = (includeEOL) ? 0 : 1;
 
return [
select Name
from Product__c
where Currently_Supported__c >= :currentlySupported
order by Name
];
}
 
public static List<Product__c> getAllProducts() {
return getAllProducts(false);
}
 
public static List<Version__c> getAllVersions(Id productId, Boolean includeEOL) {
Integer currentlySupported = (includeEOL) ? 0 : 1;
 
return [
select Name,
Product__c
from Version__c
where Currently_Supported__c >= :currentlySupported and
Product__c = :productId
order by Name
];
}
 
public static List<Version__c> getAllVersions(Id productId) {
return getAllVersions(productId, false);
}
}

Best Regards
Naga Kiran
AshwaniAshwani
It is hard to find depndencies but here http://titancronus.com/blog/2014/07/03/acquiring-dependent-picklists-in-apex-contd/ has written the way to do this.
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
I realize that I'm 3 years too late to answer the original poster, but I have developed a very concise implementation for determining the valid dependent picklist values for every controlling value.  The solution consists of a single method of about 30 lines of Apex, and I've included test code that provides 100% coverage and full functional testing (with asserts).  You can find my solution here: https://glyntalkssalesforce.blogspot.com/2018/08/dependent-picklist-values-in-apex.html

I hope this helps you -- the developer who found this forum question in a search!