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
ashu 6112ashu 6112 

Dependent Picklist Value code

Hi folks,
Can anyone tell me how to get the dependent picklist values based on the Controlling picklist value in Apex?
I need a Optimise code for that..

Thanks,

I already have this code and it is working fine, bu i need optimise code.

public static Map<String,List<String>> GetDependentOptions(String pObjName, String pControllingFieldName, String pDependentFieldName){
        Map<String,List<String>> objResults = new Map<String,List<String>>();
        //get the string to sobject global map
        Map<String,Schema.SObjectType> objGlobalMap = Schema.getGlobalDescribe();
        if (!objGlobalMap.containsKey(pObjName))
            return objResults;
        //get the type being dealt with
        Schema.SObjectType pType = objGlobalMap.get(pObjName);
        Map<String, Schema.SObjectField> objFieldMap = pType.getDescribe().fields.getMap();
        //verify field names 
        if (!objFieldMap.containsKey(pControllingFieldName) || !objFieldMap.containsKey(pDependentFieldName))
            return objResults;     
        //get the control values   
        List<Schema.PicklistEntry> ctrl_ple = objFieldMap.get(pControllingFieldName).getDescribe().getPicklistValues();
        //get the dependent values
        List<Schema.PicklistEntry> dep_ple = objFieldMap.get(pDependentFieldName).getDescribe().getPicklistValues();
        //iterate through the values and get the ones valid for the controlling field name
        TStringUtils.Bitset objBitSet = new TStringUtils.Bitset();
        //set up the results
        for(Integer pControllingIndex=0; pControllingIndex<ctrl_ple.size(); pControllingIndex++){            
            //get the pointer to the entry
            Schema.PicklistEntry ctrl_entry = ctrl_ple[pControllingIndex];
            //get the label
            String pControllingLabel = ctrl_entry.getLabel();
            //create the entry with the label
            objResults.put(pControllingLabel,new List<String>());
        }
        //cater for null and empty
         objResults.put('',new List<String>());
         objResults.put(null,new List<String>());
        //check the dependent values
        for(Integer pDependentIndex=0; pDependentIndex<dep_ple.size(); pDependentIndex++){            
            //get the pointer to the dependent index
               Schema.PicklistEntry dep_entry = dep_ple[pDependentIndex];
               //get the valid for
            String pEntryStructure = JSON.serialize(dep_entry);                
            TStringUtils.TPicklistEntry objDepPLE = (TStringUtils.TPicklistEntry)JSON.deserialize(pEntryStructure, TStringUtils.TPicklistEntry.class);
            //if valid for is empty, skip
            if (objDepPLE.validFor==null || objDepPLE.validFor==''){
                continue;
            }
            //iterate through the controlling values
            for(Integer pControllingIndex=0; pControllingIndex<ctrl_ple.size(); pControllingIndex++){    
                if (objBitSet.testBit(objDepPLE.validFor,pControllingIndex)){                    
                    //get the label
                    String pControllingLabel = ctrl_ple[pControllingIndex].getLabel();
                    objResults.get(pControllingLabel).add(objDepPLE.label);
                }
            }
        } 
        return objResults;
    }
Jasper WallJasper Wall
Hi ashu,
That much code is not required!,
Just create an instance of that object in the constructor of your controller. let that instance will be Obj
let firstfield__c and secondfield__c are dependent fields in your org,
you can get the dependant picklist values by just referencing those fields in the <apex:inputfied> like this,
 
<apex:inputField value="{!Obj.firstfield__c}" />
<apex:inputField value="{!Obj.secondfield__c}" />

Let us know if it helps,

Thanks,
Balayesu
 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Ashu,  If you're still looking for a concise Apex implementation to get valid dependent picklist values for each possible controlling value, please refer to this blog post: https://glyntalkssalesforce.blogspot.com/2018/08/dependent-picklist-values-in-apex.html

This solves the problem in about 30 lines of Apex, and also includes test code for 100% coverage.

I hope it helps!