Be Good & Do Good!

Algorithm for converting 15 digit Salesforce Id to 18 digit

Following is an algorithm I found on Salesforce Community. It converts a 15 digit salesforce Id to the corresponding 18 digit Salesforce Id. This is pretty helpful in onclick JavaScript codes and S-Controls where merge fields normally return only 15 digits. Also, although the code is in JavaScript, it can be easily converted to any other coding language code.

function normaliseSforceID( id) { // fluff up a 15 char id to return an 18 char id
if (id == null) return id;
id = id.replace(/\"/g, ''); // scrub quotes from this id
if (id.length != 15) {
//print('well, id is not 15, bye' + id + ' ' + id.length);
return null;
}
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); } } return id + suffix; }

2 Comments

  1. David Engel

    I tried your script and it didn’t work.

    Here’s a much easier way to convert 18 digit salesforce Ids without programming by using Google Spreadsheets: http://www.engeljournal.com/tools/convert-15-to-18-digit-salesforce-ids-with-google-spreadsheets/

Leave a Reply

Your email address will not be published. Required fields are marked *