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
swain 10swain 10 

Test Class for dependent picklist

How to write Test Class for dependent picklist methods?
  
 @AuraEnabled
    public static List<String> getDepStates(String objectType, String parentField, String childField,String search){    
        List<String> optionStates = new List<String>();
        PicklistFieldController controller = new PicklistFieldController();
        Map<String,List<String>> valueMap = PicklistFieldController.getDependentOptionsImpl(objectType,parentField,childField);
        for(String contr : valueMap.keySet()){
           // System.debug('CONTROLLING FIELD : ' + contr);
            //System.debug('DEPENDENT VALUES ...  : ' + valueMap.get(contr));
            if(search==contr){
                optionStates=valueMap.get(contr);
            }
        }
        return optionStates;
    }
    @AuraEnabled
    public static List<String> getPickList(String field,String objectName){    
        List<String> lstPickvals=new List<String>();
        Schema.SObjectType targetType = Schema.getGlobalDescribe().get(objectName);
        Sobject Object_name = targetType.newSObject();
        Schema.sObjectType sobject_type = Object_name.getSObjectType(); 
        Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); 
        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 
        List<Schema.PicklistEntry> pick_list_values = field_map.get(field).getDescribe().getPickListValues(); 
        for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
            lstPickvals.add(a.getValue());//add the value  to our final list
        }        
        return lstPickvals;
    }
   
}
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

Sample test class for you
@isTest 
public class ClassName_TEST 
{
    static testMethod void testMethod1() 
	{
				
		List<String> lstPick1=CLASSNAME.getDepStates('Account','CONTROLLING_FIELD_NAME','CHILD_FIELD_NAME','SearchVal');
		List<String> lstPick = CLASSNAME.getPickList('Type','Account');
		
    }
}
NOTE:- Please pass field API name

Let us know if this will help you
 
swain 10swain 10
Thanks Amit