I want to genrate an Unique ID

0
"I'm working on a project where I need to generate sequential IDs like A0001, B0002, etc. Can you suggest algorithms or code examples?"  Example Questions: "Generating Alphanumeric IDs with Specific Sequence in Mendix" "How to implement an ID system with the pattern: A0001, A0002, ..., Z9999, AA0001, ..., ZZ9999?" "Help me write JavaScript code to create IDs that increment like A0001, then roll over to AA0001, and continue up to ZZ9999."
asked
2 answers
0

Hi Rutuja, 

 

Please see this forum post that suggests to use "Random Hash" from the marketplace module "Community Commons".

There's also this marketplace component, which might help.

 

If you want to follow the Javascript approach, this solution could be adapted to your needs, although I suggest to avoid it, as a low-code approach is preferable in this case (easier to read and to maintain...).

let currentId = 'A0000';

function generateUniqueId() {
    let prefix = currentId.substring(0, 1);
    let number = parseInt(currentId.substring(1));

    if (number < 9999) {
        number++;
    } else {
        let prefixCharCode = prefix.charCodeAt(0);
        if (prefixCharCode < 90) {
            prefix = String.fromCharCode(prefixCharCode + 1);
        } else {
            prefix = 'A';
        }
        number = 0;
    }

    currentId = prefix + number.toString().padStart(4, '0');
    return currentId;
}

// Example usage
for (let i = 0; i < 30; i++) {
    console.log(generateUniqueId());
}

 

Hope this helps.

answered
0

One of the possible solutions is to use substring and ParseInteger to separate your string part ("A", "BB" or whatever else) from the integer part that is incremented and then merge them back during the creation of a new object.

answered