Created
August 8, 2019 18:45
-
-
Save sudikrt/897f93718528f6737bd2127f10d44c8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DependentPickListUtil { | |
public static Map<String, List<String>> getDependentMap(sObject objDetail, string contrfieldApiName,string depfieldApiName) { | |
String controllingField = contrfieldApiName.toLowerCase(); | |
String dependentField = depfieldApiName.toLowerCase(); | |
Map<String,List<String>> objResults = new Map<String,List<String>>(); | |
Schema.sObjectType objType = objDetail.getSObjectType(); | |
//System.debug('objType' + objType); | |
if (objType==null){ | |
return objResults; | |
} | |
Map<String, Schema.SObjectField> objFieldMap = objType.getDescribe().fields.getMap(); | |
if (!objFieldMap.containsKey(controllingField) || !objFieldMap.containsKey(dependentField)){ | |
return objResults; | |
} | |
Schema.SObjectField theField = objFieldMap.get(dependentField); | |
Schema.SObjectField ctrlField = objFieldMap.get(controllingField); | |
List<Schema.PicklistEntry> contrEntries = ctrlField.getDescribe().getPicklistValues(); | |
List<PicklistEntryWrapper> depEntries = wrapPicklistEntries(theField.getDescribe().getPicklistValues()); | |
List<String> controllingValues = new List<String>(); | |
for (Schema.PicklistEntry ple : contrEntries) { | |
String label = ple.getLabel(); | |
objResults.put(label, new List<String>()); | |
controllingValues.add(label); | |
} | |
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(){ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment