1

I'd like to create a macro for Keyboard Maestro that will take a highlighted 15 char ID and use javascript to convert it to a an 18 char ID. I understand how that conversion takes place but am a beginner at Javascript. Could anyone help in the creation of the javascript that I can use in my macro?

Thanks!

6
  • I think we need more information. In order to convert a 15 char code to an 18 char code, you need to add 3 more characters. Where are those characters coming from? How do you know what 3 characters you want to add? Are you talking about padding with spaces? Commented Jun 23, 2016 at 18:37
  • I'm wanting to append the 3 character code that can be generated from the first 15. The details are explained well here: astadiaemea.wordpress.com/2010/06/21/…–-do-you-know-how-useful-unique-ids-are-to-your-development-effort/
    – Normc
    Commented Jun 23, 2016 at 18:44
  • 3
    See also: How can I convert a 15 char Id value into an 18 char Id value?
    – Adrian Larson
    Commented Jun 23, 2016 at 19:32
  • Thanks Adrian, I did see that but was looking specifically for a javascript version.
    – Normc
    Commented Jun 23, 2016 at 19:36
  • @Normc Added it there as well so that question can continue to be the canonical answer to this question. :) I cleaned it up there from the verbatim copy.
    – Adrian Larson
    Commented Jun 23, 2016 at 19:53

1 Answer 1

1

Here is one algorithm (copied verbatim below).

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;
}

Note that if you are trying to do this conversion on a Visualforce Page you can just use "{!CASESAFEID(idValue)}".

Not the answer you're looking for? Browse other questions tagged .