Skip to content

Instantly share code, notes, and snippets.

@scolladon
Last active September 23, 2016 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scolladon/16f37a1b9c4e512e976475d51872ee2d to your computer and use it in GitHub Desktop.
Save scolladon/16f37a1b9c4e512e976475d51872ee2d to your computer and use it in GitHub Desktop.
Apex ID 15 to 18
public static string generate18CharId(final string id){
final String abecedair = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345';
if (String.isBlank(id) || (id.length() != 15 && id.length() != 18)) {
return null;
}
if (id.length() == 18) {
return id;
}
string suffix = '';
for (integer i = 0; i < 3; i++) {
integer flags = 0;
for (integer j = 0; j < 5; j++) {
string c = id.substring(i * 5 + j,i * 5 + j + 1);
if (c.isAllUpperCase() && c >= 'A' && c <= 'Z') {
flags += (1 << j);
}
}
suffix += abecedair.substring(flags,flags+1);
}
return id + suffix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment