// ********************************************************************************************************************************** // ********** 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
<aura:component controller=”PicklistFieldController”>
<!–init function which is call on component load –>
<aura:handler name=”init” value=”{!this}” action=”{!c.doInit}” />
<!–Using Attributes–>
<aura:attribute name=”fieldsAPI” type=”string[]” description=”field API names.” />
<aura:attribute name=”fieldsLabel” type=”string[]” description=”field labels.” />
<aura:attribute name=”fields” type=”string[]” description=”field class.” />
<aura:attribute name=”fieldSelections” type=”string[]” description=”field selections.” />
<aura:attribute name=”objInfo” type=”string” description=”store object Info.” />
<aura:attribute name=”counter” default=”0″ type=”integer”/>
<aura:attribute name=”dependentFieldMap” type=”map” description=”store controller(key) –> dependent(values) picklist value as a map.” />
<aura:attribute name=”style” type=”string” default=”color: #FFFFFF !important;”/>
<aura:registerEvent name=”newEvent” type=”c:DependentPicklistEvent”/>
{!v.body}
</aura:component>
({
doInit: function(component, event, helper) {
//call the helper function with pass [component, Controller field and Dependent Field] Api name
helper.getFields(component, helper);
var fieldsAPI = component.get(“v.fieldsAPI”);
var fieldsLabel = component.get(“v.fieldsLabel”);
var dependentFieldMap = component.get(“v.dependentFieldMap”);
var style = component.get(“v.style”);
var fieldsAPI = component.get(“v.fieldsAPI”);
// Array of components to create
var newComponents = [];
for (var i = 0; i < fieldsAPI.length; i += 1) {
var currentposition = i+1;
newComponents.push([“aura:html”, {
tag: “div”,
“HTMLAttributes”: {
“class”: “slds-form-element”,
“id”:”outerdiv” + currentposition
}
}]);
if(currentposition<fieldsAPI.length) {
newComponents.push([“aura:html”, {
“tag”: “div”,
“aura:id”: “rad”+i,
“HTMLAttributes”: {
“id”:”innerdiv” + currentposition,
“data-fieldpos”:currentposition,
“data-depfield”:fieldsAPI[currentposition],
}
}]);
} else {
newComponents.push([“aura:html”, {
“tag”: “div”,
“aura:id”: “rad”+i,
“HTMLAttributes”: {
“id”:”innerdiv” + currentposition,
//”style”:”color:white !important;”,
“data-fieldPos”:currentposition,
}
}]);
}
newComponents.push([“ui:inputSelect”,{
“label” : fieldsLabel[i],
“labelClass”:”stylelabels”,
“aura:id” : “field”+currentposition,
“class” : “rad” + currentposition + ” ” + fieldsAPI[currentposition],
“change” : component.getReference(“c.onControllerFieldChange”),
“HTMLAttributes”: {
“id”:”inputselect” + currentposition,
//”style”: style,
“fieldPos”:i
}
}]);
}
console.log(“new style is>>> “+ style);
$A.createComponents(
newComponents,
function(components, status, errorMessage){
if (status === “SUCCESS”) {
var pageBody = component.get(“v.body”);
for (var i = 0; i < components.length; i += 3) {
var div1 = components[i];
var label1 = components[i+1];
var div2 = components[i+2];
var uiinput = components[i+3];
var div2body = div2.get(“v.body”);
div2body.push(uiinput);
div2.set(“v.body”,div2body);
var label1body = label1.get(“v.body”);
label1body.push(div2);
label1.set(“v.body”,label1body);
var div1body = div1.get(“v.body”);
div1body.push(label1);
div1.set(“v.body”,div1body);
pageBody.push(div1);
}
component.set(“v.body”,pageBody);
}
else if (status === “INCOMPLETE”) {
console.log(“No response from server or client is offline.”)
// Show offline error
}
else if (status === “ERROR”) {
console.log(“Error: ” + errorMessage);
// Show error message
}
}
);
},
// function call on change tha controller field
onControllerFieldChange: function(component, event, helper) {
// get the selected value
var controllerValueKey = event.getSource().get(“v.value”);
var fieldSelections = component.get(“v.fieldSelections”);
fieldSelections.push(controllerValueKey);
component.set(“v.fieldSelections”, fieldSelections);
var evt = $A.get(“e.c:DependentPicklistEvent”);
evt.setParams({“optionsByEvent”: component.get(“v.fieldSelections”)});
evt.fire();
// get the map values
var Map = component.get(“v.dependentFieldMap”);
var depField;
var fieldPos;
var eventTarget = event.getSource();
fieldPos = eventTarget.getLocalId().substring(eventTarget.getLocalId().length-1,eventTarget.getLocalId().length);
var elements = component.find(“field”+fieldPos);
var fieldsAPI = component.get(“v.fieldsAPI”);
if(fieldPos<=fieldsAPI.length)
depField = fieldsAPI[fieldPos];
var elementsLength = elements.length;
// check if selected value is not equal to None then call the helper function.
// if controller field value is none then make dependent field value is none
if (controllerValueKey != ‘— None —‘) {
// get dependent values for controller field by using map[key].
if(fieldPos!=fieldsAPI.length) {
var ListOfDependentFields = Map[fieldsAPI[fieldPos-1].toLowerCase()][controllerValueKey];
helper.fetchDepValues(component, ListOfDependentFields, fieldPos);
}
}
else {
var defaultVal = [{
class: “optionClass”,
label: ‘— None —‘,
value: ‘— None —‘
}];
var field = component.find(“field”+fieldPos);
var optns = field.get(“v.options”);
if(optns == undefined) {
field.set(“v.options”, defaultVal);
}
else if(optns != null && optns.length <= 0) {
field.set(“v.options”, defaultVal);
}
}
}
})
({
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)
},
})
public class BitSet {
public Map<String, Integer> alphaNumCharCodes {get;set;}
public Map< String, Integer> base64CharCodes {get;set;}
public BitSet() {
LoadCharCodes();
}
//Method loads the character codes for all letters
public void LoadCharCodes() {
AlphaNumCharCodes = new Map<String,Integer>{
‘A’=>65,’B’=>66,’C’=>67,’D’=>68,’E’=>69,’F’=>70,’G’=>71,’H’=>72,’I’=>73,’J’=>74,
‘K’=>75,’L’=>76,’M’=>77,’N’=>78,’O’=>79,’P’=>80,’Q’=>81,’R’=>82,’S’=>83,’T’=>84,
‘U’=>85,’V’=> 86,’W’=>87,’X’=>88,’Y’=>89,’Z’=>90
};
Base64CharCodes = new Map<String, Integer>();
//lower case
Set<String> pUpperCase = AlphaNumCharCodes.keySet();
for(String pKey : pUpperCase){
//the difference between upper case and lower case is 32
AlphaNumCharCodes.put(pKey.toLowerCase(),AlphaNumCharCodes.get(pKey)+32);
//Base 64 alpha starts from 0 (The ascii charcodes started from 65)
Base64CharCodes.put(pKey,AlphaNumCharCodes.get(pKey) – 65);
Base64CharCodes.put(pKey.toLowerCase(),AlphaNumCharCodes.get(pKey) – (65) + 26);
}
//numerics
for (Integer i=0; i<=9; i++){
AlphaNumCharCodes.put(string.valueOf(i),i+48);
//base 64 numeric starts from 52
Base64CharCodes.put(string.valueOf(i), i + 52);
}
//Symbols
Base64CharCodes.put(‘+’, 62);
Base64CharCodes.put(‘/’, 63);
}
public List<Integer> testBits(String pValidFor,List<Integer> nList){
List<Integer> results = new List<Integer>();
List<Integer> pBytes = new List<Integer>();
Integer bytesBeingUsed = (pValidFor.length() * 6)/8;
Integer pFullValue = 0;
if (bytesBeingUsed <= 1)
return results;
for(Integer i=0;i<pValidFor.length();i++){
pBytes.Add((base64CharCodes.get((pValidFor.Substring(i, i+1)))));
}
for (Integer i = 0; i < pBytes.size(); i++)
{
Integer pShiftAmount = (pBytes.size()-(i+1))*6;//used to shift by a factor 6 bits to get the value
pFullValue = pFullValue + (pBytes[i] << (pShiftAmount));
}
Integer bit;
Integer targetOctet;
Integer shiftBits;
Integer tBitVal;
Integer n;
Integer nListSize = nList.size();
for(Integer i=0; i<nListSize; i++){
n = nList[i];
bit = 7 – (Math.mod(n,8));
targetOctet = (bytesBeingUsed – 1) – (n >> bytesBeingUsed);
shiftBits = (targetOctet * 8) + bit;
tBitVal = ((Integer)(2 << (shiftBits-1)) & pFullValue) >> shiftBits;
if (tBitVal==1)
results.add(n);
}
return results;
}
/*
public List<Integer> testBits(String pValidFor, List<Integer> nList) {
List<Integer> results = new List<Integer>();
List<Integer> pBytes = new List<Integer>();
Integer bytesBeingUsed = (pValidFor.length() * 6) / 8;
Integer pFullValue = 0;
if(bytesBeingUsed <= 1) {
return results;
}
for(Integer i = 0; i < pValidFor.length(); i++) {
pBytes.Add((base64CharCodes.get((pValidFor.Substring(i, i + 1)))));
}
for(Integer i = 0; i < pBytes.size(); i++) {
Integer pShiftAmount = (pBytes.size() – (i + 1)) * 6; //used to shift by a factor 6 bits to get the value
pFullValue = pFullValue + (pBytes[i] << (pShiftAmount));
}
Integer bit;
Integer targetOctet;
Integer shiftBits;
Integer tBitVal;
Integer n;
Integer nListSize = nList.size();
for(Integer i = 0; i < nListSize; i++) {
n = nList[i];
bit = 7 – (Math.mod(n, 8));
targetOctet = (bytesBeingUsed – 1) – (n >> bytesBeingUsed);
shiftBits = (targetOctet * 8) + bit;
tBitVal = ((Integer)(2 << (shiftBits – 1)) & pFullValue) >> shiftBits;
if(tBitVal == 1) {
results.add(n);
}
}
System.Debug(‘>>>>>>>> ‘+pValidFor+’ —— ‘+results);
return results;
}
*/
// 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 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
}
Apex Class
PicklistFieldController
Apex Class Source |
PicklistFieldController | case | ||
PicklistFieldController | Closed | ||
Apex Class | 5/14/2018 5:19 PM |
|