Created
May 24, 2011 21:39
-
-
Save fractastical/989794 to your computer and use it in GitHub Desktop.
Chuck Tomanek convert 15 character ID Strings to 18 chars
This file contains hidden or 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
// http://www.sundog.net/sunblog/posts/a-handy-method-for-converting-15-character-id-strings-in-salesforce-to-18-c/ | |
public string generate18CharId(string id){ | |
// This method will take a 15 character ID and return its 18 character ID: | |
if (id == null){ | |
return null; | |
} | |
if (id.length() != 15) { | |
return null; | |
} | |
string suffix = ''; | |
integer flags; | |
for (integer i = 0; i < 3; i++) { | |
flags = 0; | |
for (integer j = 0; j < 5; j++) { | |
string c = id.substring(i * 5 + j,i * 5 + j + 1); | |
//Only add to flags if c is an uppercase letter: | |
if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') { | |
flags = flags + (1 << j); | |
} | |
} | |
if (flags <= 25) { | |
suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1); | |
}else{ | |
suffix = suffix + '012345'.substring(flags-25,flags-24); | |
} | |
} | |
return id + suffix; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment