Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.37 KB | None | 0 0
  1. //----------------------------------- GETTING DEPENDANT PICKLISTS -----------------------------------//
  2.     //copy-pasted from http://titancronus.com/blog/2014/07/03/acquiring-dependent-picklists-in-apex-contd/ from Paul N's comment.
  3.     // **********************************************************************************************************************************
  4.     // ********** GetDependentOptions ***************************************************************************************************
  5.     // **********************************************************************************************************************************
  6.     // Map<String,List<String>> GetDependentOptions (String pObjName, String pControllingFieldName, String pDependentFieldName)
  7.     // Returns: Map of "pControllingFieldName" picklist values and their corresponding "pDependentFieldName" dependent option values.
  8.     // **********************************************************************************************************************************
  9.  
  10.  
  11.     /*
  12.      * @Summary: Entity to represent a json version of a picklist entry
  13.      * so that the validFor property becomes exposed
  14.      */
  15.     public class TPicklistEntry {
  16.         public string active { get; set; }
  17.         public string defaultValue { get; set; }
  18.         public string label { get; set; }
  19.         public string value { get; set; }
  20.         public string validFor { get; set; }
  21.         public TPicklistEntry() {
  22.  
  23.  
  24.         }
  25.     }
  26.  
  27.  
  28.     // Converts a base64 string into a list of integers representing the encoded bytes
  29.     public static List<Integer> B64ToBytes (String sIn) {
  30.         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
  31.                                                                ,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
  32.                                                                ,48=>52,49=>53,50=>54,51=>55,52=>56,53=>57,54=>58,55=>59,56=>60,57=>61,43=>62,47=>63};
  33.  
  34.  
  35.         List<Integer> lstOut = new List<Integer>();
  36.         if ( sIn == null || sIn == '' ) return lstOut;
  37.        
  38.         sIn += '='.repeat( 4 - Math.mod( sIn.length(), 4) );
  39.  
  40.  
  41.         for ( Integer idx=0; idx < sIn.length(); idx += 4 ) {
  42.             if ( base64.get(sIn.charAt(idx+1)) != null ) lstOut.add( (base64.get(sIn.charAt(idx)) << 2) | (base64.get(sIn.charAt(idx+1)) >>> 4) );
  43.             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) );
  44.             if ( base64.get(sIn.charAt(idx+3)) != null ) lstOut.add( ((base64.get(sIn.charAt(idx+2)) & 3)<<6) | base64.get(sIn.charAt(idx+3)) );
  45.         }
  46.  
  47.  
  48.         //System.Debug('B64ToBytes: [' + sIn + '] = ' + lstOut);
  49.         return lstOut;
  50.     }//B64ToBytes
  51.     public static List<Integer> BlobToBytes (Blob input) {
  52.         return B64ToBytes( EncodingUtil.base64Encode(input) );
  53.     }//BlobToBytes
  54.  
  55.  
  56.     // Converts a base64 string into a list of integers indicating at which position the bits are on
  57.     public static List<Integer> cnvBits (String b64Str) {
  58.         List<Integer> lstOut = new List<Integer>();
  59.         if ( b64Str == null || b64Str == '' ) return lstOut;
  60.  
  61.  
  62.         List<Integer> lstBytes = B64ToBytes(b64Str);
  63.  
  64.  
  65.         Integer i, b, v;
  66.         for ( i = 0; i < lstBytes.size(); i++ ) {
  67.             v = lstBytes[i];
  68.             //System.debug ( 'i['+i+'] v['+v+']' );
  69.             for ( b = 1; b <= 8; b++ ) {
  70.                 //System.debug ( 'i['+i+'] b['+b+'] v['+v+'] = ['+(v & 128)+']' );
  71.                 if ( ( v & 128 ) == 128 ) lstOut.add( (i*8) + b );
  72.                 v <<= 1;
  73.             }
  74.         }
  75.  
  76.  
  77.         //System.Debug('cnvBits: [' + b64Str + '] = ' + lstOut);
  78.         return lstOut;
  79.     }//cnvBits
  80.      
  81.     public static Map<String,List<String>> GetDependentOptions(String pObjName, String pControllingFieldName, String pDependentFieldName) {
  82.         Map<String,List<String>> mapResults = new Map<String,List<String>>();
  83.  
  84.  
  85.         //verify/get object schema
  86.         Schema.SObjectType pType = Schema.getGlobalDescribe().get(pObjName);
  87.         if ( pType == null ) return mapResults;
  88.         Map<String, Schema.SObjectField> objFieldMap = pType.getDescribe().fields.getMap();
  89.  
  90.  
  91.         //verify field names
  92.         if (!objFieldMap.containsKey(pControllingFieldName) || !objFieldMap.containsKey(pDependentFieldName)) return mapResults;    
  93.  
  94.  
  95.         //get the control & dependent values  
  96.         List<Schema.PicklistEntry> ctrl_ple = objFieldMap.get(pControllingFieldName).getDescribe().getPicklistValues();
  97.         List<Schema.PicklistEntry> dep_ple = objFieldMap.get(pDependentFieldName).getDescribe().getPicklistValues();
  98.  
  99.  
  100.         //clear heap
  101.         objFieldMap = null;
  102.  
  103.  
  104.         //initialize results mapping
  105.         for(Integer pControllingIndex=0; pControllingIndex<ctrl_ple.size(); pControllingIndex++){          
  106.             mapResults.put( ctrl_ple[pControllingIndex].getLabel(), new List<String>());
  107.         }
  108.         //cater for null and empty
  109.         //mapResults.put('', new List<String>());
  110.         //mapResults.put(null, new List<String>());
  111.  
  112.  
  113.         //serialize dep entries        
  114.         List<SFDescribeHelper.TPicklistEntry> objDS_Entries = new List<SFDescribeHelper.TPicklistEntry>();
  115.         objDS_Entries = (List<SFDescribeHelper.TPicklistEntry>)JSON.deserialize(JSON.serialize(dep_ple), List<SFDescribeHelper.TPicklistEntry>.class);
  116.  
  117.  
  118.         List<Integer> validIndexes;
  119.         for (SFDescribeHelper.TPicklistEntry objDepPLE : objDS_Entries){
  120.  
  121.  
  122.             validIndexes = cnvBits(objDepPLE.validFor);
  123.             //System.Debug('cnvBits: [' + objDepPLE.label + '] = ' + validIndexes);
  124.  
  125.  
  126.             for (Integer validIndex : validIndexes){                
  127.                 mapResults.get( ctrl_ple[validIndex-1].getLabel() ).add( objDepPLE.label );
  128.             }
  129.         }
  130.  
  131.  
  132.         //clear heap
  133.         objDS_Entries = null;
  134.  
  135.  
  136.         return mapResults;
  137.     }
  138.  
  139.  
  140.     //----------------------------------- OVER GETTING DEPENDANT PICKLISTS -----------------------------------//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement