// **********************************************************************************************************************************
// ********** GetDependentOptions ***************************************************************************************************
// **********************************************************************************************************************************
// Map<String,List<String>> GetDependentOptions (String pObjName, String pControllingFieldName, String pDependentFieldName)
// Returns: Map of "pControllingFieldName" picklist values and their corresponding "pDependentFieldName" dependent option values.
// **********************************************************************************************************************************

// Converts a base64 string into a list of integers representing the encoded bytes
public static List<Integer> B64ToBytes (String sIn) {
	Map<Integer,Integer> base64 = new Map<Integer,Integer>{65=>0,66=>1,67=>2,68=>3,69=>4,70=>5,71=>6,72=>7,73=>8,74=>9,75=>10,76=>11,77=>12,78=>13,79=>14,80=>15,81=>16,82=>17,83=>18,84=>19,85=>20,86=>21,87=>22,88=>23,89=>24,90=>25
														   ,97=>26,98=>27,99=>28,100=>29,101=>30,102=>31,103=>32,104=>33,105=>34,106=>35,107=>36,108=>37,109=>38,110=>39,111=>40,112=>41,113=>42,114=>43,115=>44,116=>45,117=>46,118=>47,119=>48,120=>49,121=>50,122=>51
														   ,48=>52,49=>53,50=>54,51=>55,52=>56,53=>57,54=>58,55=>59,56=>60,57=>61,43=>62,47=>63};

	List<Integer> lstOut = new List<Integer>();
	if ( sIn == null || sIn == '' ) return lstOut;
	
	sIn += '='.repeat( 4 - Math.mod( sIn.length(), 4) );

	for ( Integer idx=0; idx < sIn.length(); idx += 4 ) {
		if ( base64.get(sIn.charAt(idx+1)) != null ) lstOut.add( (base64.get(sIn.charAt(idx)) << 2) | (base64.get(sIn.charAt(idx+1)) >>> 4) );
		if ( base64.get(sIn.charAt(idx+2)) != null ) lstOut.add( ((base64.get(sIn.charAt(idx+1)) & 15)<<4) | (base64.get(sIn.charAt(idx+2)) >>> 2) );
		if ( base64.get(sIn.charAt(idx+3)) != null ) lstOut.add( ((base64.get(sIn.charAt(idx+2)) & 3)<<6) | base64.get(sIn.charAt(idx+3)) );
	}

	//System.Debug('B64ToBytes: [' + sIn + '] = ' + lstOut);
	return lstOut;
}//B64ToBytes
public static List<Integer> BlobToBytes (Blob input) {
	return B64ToBytes( EncodingUtil.base64Encode(input) );
}//BlobToBytes

// Converts a base64 string into a list of integers indicating at which position the bits are on
public static List<Integer> cnvBits (String b64Str) {
	List<Integer> lstOut = new List<Integer>();
	if ( b64Str == null || b64Str == '' ) return lstOut;

	List<Integer> lstBytes = B64ToBytes(b64Str);

	Integer i, b, v;
	for ( i = 0; i < lstBytes.size(); i++ ) {
		v = lstBytes[i];
		//System.debug ( 'i['+i+'] v['+v+']' );
		for ( b = 1; b <= 8; b++ ) {
			//System.debug ( 'i['+i+'] b['+b+'] v['+v+'] = ['+(v & 128)+']' );
			if ( ( v & 128 ) == 128 ) lstOut.add( (i*8) + b );
			v <<= 1;
		}
	}

	//System.Debug('cnvBits: [' + b64Str + '] = ' + lstOut);
	return lstOut;
}//cnvBits

public class TPicklistEntry{
	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 TPicklistEntry(){}
}//TPicklistEntry
 
public static Map<String,List<String>> GetDependentOptions(String pObjName, String pControllingFieldName, String pDependentFieldName) {
	Map<String,List<String>> mapResults = new Map<String,List<String>>();

	//verify/get object schema
	Schema.SObjectType pType = Schema.getGlobalDescribe().get(pObjName);
	if ( pType == null ) return mapResults;
	Map<String, Schema.SObjectField> objFieldMap = pType.getDescribe().fields.getMap();

	//verify field names
	if (!objFieldMap.containsKey(pControllingFieldName) || !objFieldMap.containsKey(pDependentFieldName)) return mapResults;     

	//get the control & dependent values   
	List<Schema.PicklistEntry> ctrl_ple = objFieldMap.get(pControllingFieldName).getDescribe().getPicklistValues();
	List<Schema.PicklistEntry> dep_ple = objFieldMap.get(pDependentFieldName).getDescribe().getPicklistValues();

	//clear heap
	objFieldMap = null;

	//initialize results mapping
	for(Integer pControllingIndex=0; pControllingIndex<ctrl_ple.size(); pControllingIndex++){           
		mapResults.put( ctrl_ple[pControllingIndex].getLabel(), new List<String>());
	}
	//cater for null and empty
	mapResults.put('', new List<String>());
	mapResults.put(null, new List<String>());

	//serialize dep entries        
	List<Globals.TPicklistEntry> objDS_Entries = new List<Globals.TPicklistEntry>();
	objDS_Entries = (List<Globals.TPicklistEntry>)JSON.deserialize(JSON.serialize(dep_ple), List<Globals.TPicklistEntry>.class);

	List<Integer> validIndexes;
	for (Globals.TPicklistEntry objDepPLE : objDS_Entries){

		validIndexes = cnvBits(objDepPLE.validFor);
		//System.Debug('cnvBits: [' + objDepPLE.label + '] = ' + validIndexes);

		for (Integer validIndex : validIndexes){                
			mapResults.get( ctrl_ple[validIndex-1].getLabel() ).add( objDepPLE.label );
		}
	}

	//clear heap
	objDS_Entries = null;

	return mapResults;
}//GetDependentOptions


({
getFields: function(component, helper) {
var action = component.get(“c.getFieldList”);
action.setParams({
‘fieldApiNames’: component.get(“v.fieldsAPI”),
‘fieldLabels’: component.get(“v.fieldsLabel”)
});
action.setCallback(this, function(response) {
if (response.getState() == “SUCCESS”) {
component.set(“v.fields”, response.getReturnValue());
var fields = component.get(“v.fields”);
helper.fetchPicklistValues(component, fields);
}
});
$A.enqueueAction(action);
},
fetchPicklistValues: function(component, fields) {
// call the server side function
var objApiNameList = [];
var controllingfields = [];
var dependentfields = [];
for(var i = 0; i < fields.length; i++) {
objApiNameList.push(component.get(“v.objInfo”));
controllingfields.push(fields[i].api);
dependentfields.push(fields[i].depApi);
}
var field = fields[0];
console.log(‘c.getDependentOptionsMulti’);
var action = component.get(“c.getDependentOptionsMulti”);
// pass paramerters [object name , contrller field name ,dependent field name] –
// to server side function

action.setParams({
‘objApiNameList’: objApiNameList,
‘contrfieldApiNameList’: controllingfields,
‘depfieldApiNameList’: dependentfields
});
//set callback
action.setCallback(this, function(response) {
if (response.getState() == “SUCCESS”) {
//store the return response from server (map<string,List<string>>)
var StoreResponse = response.getReturnValue();
// once set #StoreResponse to dependentFieldMap attribute
var Map = component.get(“v.dependentFieldMap”);
if(Map == undefined) {
component.set(“v.dependentFieldMap”, StoreResponse);
}
else if(Map != null && Map.length <= 0) {
component.set(“v.dependentFieldMap”, StoreResponse);
}
console.log(StoreResponse);
// create a empty array for store map keys(@@—>which is controller picklist values)
var listOfkeys = []; // for store all map keys (controller picklist values)
var ControllerField = []; // for store controller picklist value to set on ui field.
// play a for loop on Return map
// and fill the all map key on listOfkeys variable.

for (var controllingfieldname in StoreResponse) {
for (var singlekey in StoreResponse[controllingfieldname]) {
if(controllingfieldname==controllingfields[0].toLowerCase()) {
listOfkeys.push(singlekey);
}
}
}

//set the controller field value for ui:inputSelect
if (listOfkeys != undefined && listOfkeys.length > 0) {
ControllerField.push({
class: “optionClass”,
label: “— None —“,
value: “— None —”
});
}

for (var i = 0; i < listOfkeys.length; i++) {
ControllerField.push({
class: “optionClass”,
label: listOfkeys[i],
value: listOfkeys[i]
});
}
// set the ControllerField variable values
var fields = component.find(“field1”);
var optns = fields.get(“v.options”);
if(optns == undefined) {
fields.set(“v.options”, ControllerField);
}
else if(optns != null && optns.length <= 0) {
fields.set(“v.options”, ControllerField);
}
}
});
$A.enqueueAction(action);
},
fetchDepValues: function(component, ListOfDependentFields, contrFieldPos) {
// create a empty array var for store dependent picklist values for controller field)
var dependentFields = [];
var dependentFieldPos = contrFieldPos;
dependentFieldPos++;
if (ListOfDependentFields != undefined && ListOfDependentFields.length > 0) {
dependentFields.push({
class: “optionClass”,
label: “— None —“,
value: “— None —”
});
}
if (ListOfDependentFields != undefined) {
for (var i = 0; i < ListOfDependentFields.length; i++) {
dependentFields.push({
class: “optionClass”,
label: ListOfDependentFields[i],
value: ListOfDependentFields[i]
});
}
}
// set the dependentFields variable values to State(dependent picklist field) on ui:inputselect
var depfield = component.find(“field”+dependentFieldPos);
var optns = depfield.get(“v.options”);
depfield.set(“v.options”, dependentFields)
},
})

Apex Class Source

PicklistFieldController case
PicklistFieldController Closed
Apex Class 5/14/2018 5:19 PM
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
public class PicklistFieldController {

@AuraEnabled
public static Map<String,Map<String,List<String>>> getDependentOptionsMulti(List<String> objApiNameList, List<String> contrfieldApiNameList, List<String> depfieldApiNameList) {
integer i = 0;
system.debug(‘Starting getDependentOptionsMulti’);
Map<String,Map<String,List<String>>> MasterobjResults = new Map<String,Map<String,List<String>>>();
Map<integer,Set<String>> parentMapForMultiDependency = new Map<integer,Set<String>> ();
for(i = 0; i < objApiNameList.size(); i++) {
system.debug(objApiNameList + ‘##’ + contrfieldApiNameList + ‘###’ + depfieldApiNameList);
String objectName = objApiNameList[i].toLowerCase();
String controllingField = contrfieldApiNameList[i].toLowerCase();
String dependentField;
if(depfieldApiNameList.size() >= i && depfieldApiNameList[i] != null) {
dependentField = depfieldApiNameList[i].toLowerCase();
}

//get the String to sobject global map
Map<String,List<String>> objResults = new Map<String,List<String>>();
Map<String,Schema.SObjectType> objGlobalMap = Schema.getGlobalDescribe();
if(!Schema.getGlobalDescribe().containsKey(objectName)) {
System.debug(‘OBJNAME NOT FOUND –.> ‘ + objectName);
return null;
}
Schema.SObjectType objType = Schema.getGlobalDescribe().get(objectName);
if(objType == null) {
return MasterobjResults;//return objResults;
}
Bitset bitSetObj = new Bitset();
Map<String, Schema.SObjectField> objFieldMap = objType.getDescribe().fields.getMap();
//Check if picklist values exist
if(!objFieldMap.containsKey(controllingField) || !objFieldMap.containsKey(dependentField)) {
System.debug(‘FIELD NOT FOUND –.> ‘ + controllingField + ‘ OR ‘ + dependentField);
return MasterobjResults;//return objResults;
}
List<Schema.PicklistEntry> contrEntries = objFieldMap.get(controllingField).getDescribe().getPicklistValues();
List<Schema.PicklistEntry> depEntries = objFieldMap.get(dependentField).getDescribe().getPicklistValues();
objFieldMap = null;
List<Integer> controllingIndexes = new List<Integer>();
for(Integer contrIndex = 0; contrIndex < contrEntries.size(); contrIndex++) {
//need to add if logic for multi-level dependent picklists
Schema.PicklistEntry ctrlentry = contrEntries[contrIndex];
String label = ctrlentry.getLabel();
objResults.put(label, new List<String>());
controllingIndexes.add(contrIndex);
//need to add if logic for multi-level dependent picklists
}
List<Schema.PicklistEntry> objEntries = new List<Schema.PicklistEntry>();
List<PicklistEntryWrapper> objJsonEntries = new List<PicklistEntryWrapper>();
for(Integer dependentIndex = 0; dependentIndex < depEntries.size(); dependentIndex++) {
Schema.PicklistEntry depentry = depEntries[dependentIndex];
objEntries.add(depentry);
//system.debug(‘dependentIndex: ‘ + dependentIndex + ‘, depentry: ‘+ depentry);
}
objJsonEntries = (List<PicklistEntryWrapper>)JSON.deserialize(JSON.serialize(objEntries), List<PicklistEntryWrapper>.class);
List<Integer> indexes = new list<integer>();
parentMapForMultiDependency = null; //reset to recalculate
for(PicklistEntryWrapper objJson : objJsonEntries) {
if(objJson.validFor == null || objJson.validFor == ”) {
continue;
}
try {
//indexes = bitSetObj.testBits(objJson.validFor,controllingIndexes);
indexes = bitSetObj.cnvBits(objJson.validFor);
system.debug(‘success. objJson: ‘ + objJson + ‘, objJson.validFor: ‘ + objJson.validFor + ‘, controllingIndexes: ‘ + controllingIndexes + ‘,indexes: ‘+indexes);
/*
for(integer index : indexes) {
if(parentMapForMultiDependency.containskey(index)) {
parentMapForMultiDependency.get(index).add(objJson.label);
} else {
parentMapForMultiDependency.put(index,new set<string>{objJson.label});
}
}
*/
}
catch(Exception ex) {
system.debug(‘error with: ‘ + objJson + ‘ and ‘ + objJson.validFor + ‘ and ‘ + controllingIndexes + ‘, ex.getMessage(): ‘ +ex.getMessage());
}
for(Integer idx : indexes) {
String contrLabel = contrEntries[idx-1].getLabel();
objResults.get(contrLabel).add(objJson.label);
}
}
bitSetObj = null;
objEntries = null;
objJsonEntries = null;
system.debug(‘objResults—>’ + objResults);
MasterobjResults.put(controllingField,objResults); //return objResults;
}

return MasterobjResults;
}

@AuraEnabled
public static Map<String,List<String>> getDependentOptionsImpl(String objApiName, String contrfieldApiName, String depfieldApiName) {
system.debug(‘getDependentOptionsImpl’);
system.debug(objApiName + ‘##’ + contrfieldApiName + ‘###’ + depfieldApiName);
String objectName = objApiName.toLowerCase();
String controllingField = contrfieldApiName.toLowerCase();
String dependentField;
if(depfieldApiName != null) {
dependentField = depfieldApiName.toLowerCase();
}
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(!Schema.getGlobalDescribe().containsKey(objectName)) {
System.debug(‘OBJNAME NOT FOUND –.> ‘ + objectName);
return null;
}
Schema.SObjectType objType = Schema.getGlobalDescribe().get(objectName);
if(objType == null) {
return objResults;
}
Bitset bitSetObj = new Bitset();
Map<String, Schema.SObjectField> objFieldMap = objType.getDescribe().fields.getMap();
//Check if picklist values exist
if(!objFieldMap.containsKey(controllingField) || !objFieldMap.containsKey(dependentField)) {
System.debug(‘FIELD NOT FOUND –.> ‘ + controllingField + ‘ OR ‘ + dependentField);
return objResults;
}
List<Schema.PicklistEntry> contrEntries = objFieldMap.get(controllingField).getDescribe().getPicklistValues();
List<Schema.PicklistEntry> depEntries = objFieldMap.get(dependentField).getDescribe().getPicklistValues();
objFieldMap = null;
List<Integer> controllingIndexes = new List<Integer>();
for(Integer contrIndex = 0; contrIndex < contrEntries.size(); contrIndex++) {
Schema.PicklistEntry ctrlentry = contrEntries[contrIndex];
String label = ctrlentry.getLabel();
objResults.put(label, new List<String>());
controllingIndexes.add(contrIndex);
}
List<Schema.PicklistEntry> objEntries = new List<Schema.PicklistEntry>();
List<PicklistEntryWrapper> objJsonEntries = new List<PicklistEntryWrapper>();
for(Integer dependentIndex = 0; dependentIndex < depEntries.size(); dependentIndex++) {
Schema.PicklistEntry depentry = depEntries[dependentIndex];
objEntries.add(depentry);
}
objJsonEntries = (List<PicklistEntryWrapper>)JSON.deserialize(JSON.serialize(objEntries), List<PicklistEntryWrapper>.class);
List<Integer> indexes;
for(PicklistEntryWrapper objJson : objJsonEntries) {
if(objJson.validFor == null || objJson.validFor == ”) {
continue;
}
try {
//indexes = bitSetObj.testBits(objJson.validFor,controllingIndexes);
indexes= bitSetObj.cnvBits(objJson.validFor);
//system.debug(‘success with: ‘ + objJson + ‘ and ‘ + objJson.validFor + ‘ and ‘ + controllingIndexes);
}
catch(Exception ex) {
system.debug(‘error with: ‘ + objJson + ‘ and ‘ + objJson.validFor + ‘ and ‘ + controllingIndexes);
}
for(Integer idx : indexes) {
String contrLabel = contrEntries[idx-1].getLabel();
//String contrLabel = contrEntries[idx].getLabel(); //old bit formula
objResults.get(contrLabel).add(objJson.label);
}
}
objEntries = null;
objJsonEntries = null;
system.debug(‘objResults—>’ + objResults);
return objResults;
}

@AuraEnabled
public static List<FieldClass> getFieldList(String[] fieldApiNames, String[] fieldLabels) {
List<FieldClass> fields = new List<FieldClass>();
Integer cnt = 0;
for(String str : fieldLabels) {
if(cnt+1 < fieldLabels.size()) {
fields.add(new FieldClass(str, fieldApiNames[cnt], fieldApiNames[cnt+1], cnt));
}
else {
fields.add(new FieldClass(str, fieldApiNames[cnt], ‘none’, cnt));
}
cnt++;
}
system.debug(‘fields ‘+fields);
return fields;
}
}

By admin