Alphanumeric counter (auto generating)
Question
Application Type
Reactive
Service Studio Version
11.10.18 (Build 41211)

Hello,

I am looking for a way to generate alphanumeric numbers. Numbers (0-9) and capital letters (A-Z) may be used. The generated numbers should be stored in a database. The numbers should not be generated randomly but in ascending order. Each number may occur only once. The user may define how many digits the number may have.

Example (4 digits): 145A
Example (10 digits): A123456789

I have not found a corresponding solution in the Forge. Does anyone have an idea how to implement something like this?

Thanks a lot for your help.
Michael


Hi Michael,

i made an example of how you could do this.  

This is just server actions and a database, but I'm sure you can take it from there and wrap api services around it, and add necessary security and things like that.

Some design choices you could make different :

  • use the name of the key sequence as id instead of an autogenerated number (that's just one of my go to things to always use autonumber keys)
  • allow user to pass in a 'seed value' where to start from, my example just starts with a sequence of n times the lowest value
  • don't have a public operation to set up and delete the key, only to produce a new value.  Then you'd probably have an admin screen for this, that way you would have a bit more control, only allowing adminstrators to bring into life new key generators
  • make a many to many relationship between key generator and CharacterSequence, especially if you want to reuse the same sequence for many key generators, might save some database space and maybe speed.

See attached omls

KeyGenerator.zip

Hi Michael,

you can try this JavaScript snippet. 

var result           = '';

var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

var charactersLength = characters.length;

for ( var i = 0; i < 4; i++ ) {

  result += characters.charAt(Math.floor(Math.random() * charactersLength));

}

console.log(result);

you can try this console first. if it serves your purpose you can create a client action and return the output.


Hello Mukesh, thank you very much for your solution.

In principle, I get an alphanumeric number with the Snipet - as requested. Also the number of digits can be adjusted. However, the number is created randomly. I am looking for a solution with which the alpahanumeric number is incremented, i.e. each time the function is called again with the same number of digits, the next possible previously not generated alphanumeric number is generated.

Do you have an idea?

Many thanks and best regards
Michael

Yes Michael,

if you don't want duplicate, after generating sequence you can check with your database if the entry exists. if it exists you can generate again.


Okay, that's a possibility, of course. But I'm afraid that with alphanumeric numbers with several digits and with an almost exhausted number space the generation takes a very long time until by chance a not yet generated combination is found.


Hi Michael,

can you explain more about what you need this for ?  Is this just for the purpose of trying something out or do you have a real requirement in a real app.  What exactly is that requirement, if it's just creating uniqueness, why do you insist on letters playing a role, why not have all numerics in case of for example autonumber id on entities.  And why insist on things being in ascending order, why not generate a unique GUID or something ?  I'm not really seeing where you are going with this.

That being said, if you want an alphanumeric string being in ascending order, you'll first have to define what exact characters are in your domain and what you mean with "ascending order".  is a '1' larger or smaller than an 'A' in your solution, for example.  One thing you could choose is maybe a similar order like ASCII characters ?  Is that what you mean ?

As for generating the next value in that ascending order of yours : to do this, you'll need to store in some form the last used value.  If this has to persist, I would imagine you would have some sort of entity for that, maybe.

Without knowing more about what you are trying to achieve, there's not much more to be said.

Dorine


Hi Dorine, thank you very much for your feedback. The idea is to have a web application to generate unique IDs. The IDs are used among other things to label samples by hand. The goal is to get a maximum of unique IDs with the shortest possible notation. In addition, the IDs should be easy to read and less error-prone. This currently results in the use of the numbers "0-9" and the capital letters "A-Z". Already generated IDs should be stored with the date of generation, so that it is known which IDs have already been generated and the number of IDs still available is known. The user only has to specify the number of digits and can then generate the next possible free ID. In addition, the generation of an ID should also be possible via a Rest API. Thanks a lot, Michael


Hi Michael,

i made an example of how you could do this.  

This is just server actions and a database, but I'm sure you can take it from there and wrap api services around it, and add necessary security and things like that.

Some design choices you could make different :

  • use the name of the key sequence as id instead of an autogenerated number (that's just one of my go to things to always use autonumber keys)
  • allow user to pass in a 'seed value' where to start from, my example just starts with a sequence of n times the lowest value
  • don't have a public operation to set up and delete the key, only to produce a new value.  Then you'd probably have an admin screen for this, that way you would have a bit more control, only allowing adminstrators to bring into life new key generators
  • make a many to many relationship between key generator and CharacterSequence, especially if you want to reuse the same sequence for many key generators, might save some database space and maybe speed.

See attached omls

KeyGenerator.zip

I see you are also mentioning a requirement to know how many are still available, you could add a few attributes in the datamodel and update this on every key being given out.

An entirely other approach could be to create a full list of all values at the time of setting up a key generator, each with a sequence number, a status of unused and a date used of nulldate, and on every call for the next value, just read the next unused, and update it's status to used and the timestamp used.  Then you could at any time query how many still have status unused.



Hi Dorine, wow - thanks so much for your help. You have "pushed" me in the right direction with this. Perfect. Thank you very much.

mvp_badge
MVP

Hi,

Maybe you can utilize this

https://www.outsystems.com/forge/component-overview/4534/characterrangesdemo 

Regards,

Daniel


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.