1

I have below javascript code which is used to convert 15 digit salesforce ID to 18 digit salesforce Id:

document.getElementById("convert").addEventListener("click", myFunction);
function myFunction(){
    var digit = document.getElementById("15digitId").value;

    var suffix = '';
    var flags;
    var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",
    isUppercase = function(c) {
                return c >= "A" && c < = "Z";
            };
    alert(digit.length);

    for(var i=0;i < 3; i++){
        alert(".. 1st loop.. ");
        flags=0;
        for(var j=0;j < 5; j++){
            alert(digit.substring(i*5+j,i*5+j+1));
            var c = digit.substring(i*5+j,i*5+j+1);

            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); 
        }
        }
    }
    document.getElementById('18digitId').value = digit + suffix;
}

This code is working till var c = digit.substring(i*5+j,i*5+j+1); line and after that it stopped working

2
  • yes it's duplicate but it's not working for me Jul 29, 2016 at 16:01
  • If you want help with Javascript, Stack Overflow may be a more appropriate site...also not working is about as vague as you can possibly be.
    – Adrian Larson
    Jul 29, 2016 at 16:05

1 Answer 1

3

This function should output the correct result:

function myFunction() {
    var idParts = document.getElementById("15digitId").value.match(/(.{5})(.{5})(.{5})/)
    var base36 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('')
    var output = []
    var outer, inner, subparts, buffer

    for(outer = 1; outer <= 3; outer++) {
        subparts = idParts[outer].split('')
        buffer = 0
        for(inner = 4; inner >= 0; inner--) {
            buffer = (buffer << 1) | (subparts[inner].match(/[A-Z]/) ? 1 : 0)
        }
        output.push(base36[buffer])
    }
    document.getElementById('18digitId').value = value+output.join('')
}
4
  • Hi @sfdcfox it's working fine thanks for your help... but could you tell me where in my code I was doing mistake Jul 29, 2016 at 15:56
  • @salesforceDeveloper Your code has a few syntax errors; Chrome won't even compile your function when copy-pasted. Assuming those were fixed, I wouldn't expect the line that you mentioned as a point of failure.
    – sfdcfox
    Jul 29, 2016 at 16:10
  • I used alert line by line and it stopped working after the line I mentioned Jul 29, 2016 at 16:12
  • @salesforceDeveloper The error's in the next line after var c ...; there's no method on String named equals, so the code crashes at that point.
    – sfdcfox
    Jul 29, 2016 at 16:27

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