Salesforce Convert 15-char to 18-char IDs in Apex


/ Published in: Other
Save to your folder(s)

Credits to apex-lang package


Copy this code and paste it in your HTML
  1. global static String to18(String the15charID){
  2. //a0BA0000000L2ZC => a0BA0000000L2ZCMA0
  3. //the15charID = 'a0BA0000000L2ZC'
  4. final String BASE ='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456';
  5.  
  6. the15charID = StringUtils.trim(the15charID);
  7. if(StringUtils.length(the15charID) != 15){
  8. return the15charID;
  9. }
  10. String result = '';
  11. List<String> chars = null;
  12. for(integer i = 0; i < 3; i++){
  13. chars = StringUtils.toCharArray(StringUtils.reverse(the15charID.substring(i*5,i*5+5)));
  14. String binary = '';
  15. for(String ch : chars){
  16. binary += Character.isUpperCase(ch) ? '1' : '0';
  17. }
  18. result += StringUtils.charAt(BASE,NumberUtils.parseInt(binary,2));
  19. }
  20. return the15charID + result;
  21. }

URL: http://apex-lang.googlecode.com/svn/trunk/eclipse-project/src/classes/SystemUtils.cls

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.