Chuck Tomanek convert 15 character ID Strings to 18 chars


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



Copy this code and paste it in your HTML
  1. // http://www.sundog.net/sunblog/posts/a-handy-method-for-converting-15-character-id-strings-in-salesforce-to-18-c/
  2.  
  3. public string generate18CharId(string id){
  4.  
  5. // This method will take a 15 character ID and return its 18 character ID:
  6.  
  7. if (id == null){
  8. return null;
  9. }
  10. if (id.length() != 15) {
  11. return null;
  12. }
  13. string suffix = '';
  14. integer flags;
  15.  
  16. for (integer i = 0; i < 3; i++) {
  17. flags = 0;
  18. for (integer j = 0; j < 5; j++) {
  19. string c = id.substring(i * 5 + j,i * 5 + j + 1);
  20. //Only add to flags if c is an uppercase letter:
  21. if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
  22. flags = flags + (1 << j);
  23. }
  24. }
  25. if (flags <= 25) {
  26. suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
  27. }else{
  28. suffix = suffix + '012345'.substring(flags-25,flags-24);
  29. }
  30. }
  31. return id + suffix;
  32. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.