Skip to content

Instantly share code, notes, and snippets.

@heme
Last active August 29, 2015 14:21
Show Gist options
  • Save heme/d04296f3ec9a1c18cc6d to your computer and use it in GitHub Desktop.
Save heme/d04296f3ec9a1c18cc6d to your computer and use it in GitHub Desktop.
Convert a 15 Character SalesForce ID to 18 Character ID
// Source: https://developer.salesforce.com/forums/ForumsMain?id=906F00000009BWRIA2
var id=prompt('Enter a 15 char ID:','');
id = id.replace(/\"/g, '');
if (id.length != 15) {
alert('Not 15 Characters');
} else {
var suffix = '';
for (var i = 0; i < 3; i++) {
var flags = 0;
for (var j = 0; j < 5; j++) {
var c = id.charAt(i * 5 + j);
if (c >= 'A' && c <= 'Z') {
flags += 1 << j;
}
}
if (flags <= 25) {
suffix += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(flags);
} else {
suffix += '012345'.charAt(flags-26);
}
}
prompt(id+' converts to:',id+suffix);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment