Last active
December 30, 2015 17:49
-
-
Save anupj/7863362 to your computer and use it in GitHub Desktop.
Convert Salesforce's 15 character id to 18 character Id
See here for context: http://salesforce.stackexchange.com/questions/1653/what-are-salesforce-ids-composed-of
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Converts 15 char salesforce id to 18 char id | |
static String convertto18CharId(String original15charId){ | |
if(original15charId == null || original15charId.isEmpty()) return null; | |
original15charId = original15charId.trim(); | |
if(original15charId.length() != 15) return null; | |
final String BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456"; | |
StringBuilder result = new StringBuilder(); | |
try { | |
for(int i = 0; i < 3; i++){ | |
StringBuilder tempString = new StringBuilder(original15charId.substring(i*5, i*5+5)); | |
tempString.reverse(); | |
String binary = ""; | |
for(char ch: tempString.toString().toCharArray()) { | |
binary += Character.isUpperCase(ch) ? '1' : '0'; | |
} | |
result.append(BASE.charAt(Integer.parseInt(binary,2))); | |
} | |
} catch(Exception ex) { | |
ex.printStackTrace(); | |
} | |
if(result.length() == 0) return null; | |
return original15charId+result.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment