salesforce中得到下拉列表的控制依赖值没有系统提供的方法。在网上找了一些,自己也编辑了一下。

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,Set<String>> getDependentOptions(String pObjName, String pControllingFieldName, String pDependentFieldName) {
Map<String,Set<String>> mapResults = new Map<String,Set<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].getValue(), new Set<String>());
}
//cater for null and empty
mapResults.put('', new Set<String>());
mapResults.put(null, new Set<String>()); //serialize dep entries
List<TPicklistEntry> objDS_Entries = new List<TPicklistEntry>();
objDS_Entries = (List<TPicklistEntry>)JSON.deserialize(JSON.serialize(dep_ple), List<TPicklistEntry>.class); List<Integer> validIndexes;
for (TPicklistEntry objDepPLE : objDS_Entries){ validIndexes = cnvBits(objDepPLE.validFor);
//System.Debug('cnvBits: [' + objDepPLE.label + '] = ' + validIndexes); for (Integer validIndex : validIndexes){
mapResults.get( ctrl_ple[validIndex-1].getValue() ).add( objDepPLE.value );
}
} //clear heap
objDS_Entries = null; return mapResults;
}//GetDependentOptions

//通过依赖值找到它对应的控制值
public static String getControllingValue(String pObjName, String pControllingFieldName, String pDependentFieldName, String pDependentValue)
{
String controllingValue = null;
//verify/get object schema
Schema.SObjectType pType = Schema.getGlobalDescribe().get(pObjName);
if ( pType == null ) return controllingValue;
Map<String, Schema.SObjectField> objFieldMap = pType.getDescribe().fields.getMap(); //verify field names
if (!objFieldMap.containsKey(pControllingFieldName) || !objFieldMap.containsKey(pDependentFieldName)) return controllingValue; //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; //serialize dep entries
List<TPicklistEntry> objDS_Entries = new List<TPicklistEntry>();
objDS_Entries = (List<TPicklistEntry>)JSON.deserialize(JSON.serialize(dep_ple), List<TPicklistEntry>.class); List<Integer> validIndexes;
for (TPicklistEntry objDepPLE : objDS_Entries){ if (objDepPLE.label == pDependentValue)
{
validIndexes = cnvBits(objDepPLE.validFor);
System.debug('validIndexes:::' + validIndexes);
if (validIndexes.size()>0) {
controllingValue = ctrl_ple[validIndexes.get(0) - 1].getLabel();
}
} if (objDepPLE.value == pDependentValue)
{
validIndexes = cnvBits(objDepPLE.validFor);
System.debug('validIndexes:::' + validIndexes);
if (validIndexes.size()>0) {
controllingValue = ctrl_ple[validIndexes.get(0) - 1].getValue();
}
} } objDS_Entries = null; return controllingValue;
}
05-04 11:09