0

I am trying to get the two values of two picklist values for one controlling field. Example if Country is Controlling field then State Names and State Code code should be fetched but I am getting only one dependent values for state names picklist field.

   public class QuickCreateOppCtr {

   @AuraEnabled 
    public static Map<String, List<String>> getDependentMap(sObject objDetail, string contrfieldApiName,string depfieldApiName, string depfieldApiName2) {
        String controllingField = contrfieldApiName.toLowerCase();
        String dependentField = depfieldApiName.toLowerCase();

        Map<String,List<String>> objResults = new Map<String,List<String>>();

        //Schema.sObjectType objType = objDetail.getSObjectType().getDescribe().getName();
        /*if (objType==null){
            return objResults; 
        }*/
        Map<String, Schema.SObjectField> objFieldMap = objDetail.getSObjectType().getDescribe().fields.getMap();

        if (!objFieldMap.containsKey(controllingField) || !objFieldMap.containsKey(dependentField)){
            return objResults;     
        }

        Schema.SObjectField theField = objFieldMap.get(dependentField);
        System.debug('theField --->' +theField);
        Schema.SObjectField ctrlField = objFieldMap.get(controllingField);

        List<Schema.PicklistEntry> contrEntries = ctrlField.getDescribe().getPicklistValues();
        System.debug('contrEntries ----->'+contrEntries);
        List<PicklistEntryWrapper> depEntries = wrapPicklistEntries(theField.getDescribe().getPicklistValues());
        System.debug('depEntries ----> '+depEntries);
        List<String> controllingValues = new List<String>();

        for (Schema.PicklistEntry ple : contrEntries) {
            String label = ple.getLabel();
            objResults.put(label, new List<String>());
            controllingValues.add(label);
        }
        System.debug('controllingValues --->'+controllingValues);

        for (PicklistEntryWrapper plew : depEntries) {
            String label = plew.label;
            String validForBits = base64ToBits(plew.validFor);
            for (Integer i = 0; i < validForBits.length(); i++) {
                String bit = validForBits.mid(i, 1);
                if (bit == '1') {
                    objResults.get(controllingValues.get(i)).add(label);
                }
            }
        }
        system.debug(objResults+'****objResults');
        return objResults;

    }


    public static String decimalToBinary(Integer val) {
        String bits = ''; 
        while (val > 0) {
            Integer remainder = Math.mod(val, 2);
            val = Integer.valueOf(Math.floor(val / 2));
            bits = String.valueOf(remainder) + bits;
        }
        return bits;
    }

    public static String base64ToBits(String validFor) {
        if (String.isEmpty(validFor)) return '';

        String validForBits = '';

        for (Integer i = 0; i < validFor.length(); i++) {
            String thisChar = validFor.mid(i, 1);
            Integer val = base64Chars.indexOf(thisChar);
            String bits = decimalToBinary(val).leftPad(6, '0');
            validForBits += bits;
        }

        return validForBits;
    }

    private static final String base64Chars = '' +
        'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
        'abcdefghijklmnopqrstuvwxyz' +
        '0123456789+/';


    private static List<PicklistEntryWrapper> wrapPicklistEntries(List<Schema.PicklistEntry> PLEs) {
        return (List<PicklistEntryWrapper>)
            JSON.deserialize(JSON.serialize(PLEs), List<PicklistEntryWrapper>.class);
    }

    public class PicklistEntryWrapper{
        public String active {get;set;}
        public String defaultValue {get;set;}
        public String label {get;set;}
        public String value {get;set;}
        public String validFor {get;set;}
        public PicklistEntryWrapper(){            
        }

    }


}
0

You haven't written the code to do that. Your method accepts the parameter depfieldApiName2 and then does nothing with it, instead only locating the picklist values for depfieldApiName.

You'd need to implement this logic, using the functionality you already have, and then change your return data structure to effectively represent the picklist values for more than one field API name - possibly by adding another layer of Map to return a Map<String, Map<String, List<String>>>, where the outermost key is the API name of the dependent field.

Alternately, you could factor your existing functionality into a private method that takes exactly one API name, and wrap it in a public method that calls it more than once:

public getDependentMapByAPIName(String sobjectName, String controllingField, List<String> apiNames) {
    Map<String, Map<String, List<String>>> depMap = new Map<String, Map<String, List<String>>>();

    for (String a : apiNames) {
        depMap.put(a, getDependentMap(sobjectName, controllingField, a));
    }

    return depMap;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.