function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AnkitAgarwalSFAnkitAgarwalSF 

How to add controlling and dependent picklist values in apex test class

I have written apex code to get the controlling field values and corresponding field values but to test that I need to add some values in both controlling and dependent fields and map them in the test class. Most of the posts tell how to get get the mapped values but they dont say how to add the values and map them.
Ajay GautamAjay Gautam
See this one:
http://mysalesforceescapade.blogspot.in/2015/03/getting-dependent-picklist-values-from.html
Having a Util method for scenario like these will help you get the Controlling and Dependent values in Apex.

Regarding Test class: 
PicklistEntry falls in definition Metadata type thus may not be created from Apex. Ideally you should refer to existing fields to test your class.

@isTest public class PicklistFieldControllerTest {
static testMethod void getDependentOptionsImplTest(){
PicklistFieldController controller = new PicklistFieldController();
Map<String,List<String>> valueMap = controller.getDependentOptionsImpl('Account','ShippingCountryCode','ShippingStateCode');
for(String contr : valueMap.keySet()){
System.debug('CONTROLLING FIELD : ' + contr);
System.debug('DEPENDENT VALUES ... : ' + valueMap.get(contr));
}
}
}