Generate random combination of letters and numbers fith fixed length

Hi, Bubblers.
In my project I have a thing called “Booking”. I want evety booking to have a code with several restrictions:

  1. It should be unique;
  2. It should consist of random combination of letters and numbers;
  3. It should be with fixed lentgh (for example, 8 symbols);

Can this be achieved with Bubble?

Yes. Use API connection with Blockspring > webservices > random string.

2 Likes

Actually this is now built in in bubble. Check the ‘calculate formula’ option in the drop down.

7 Likes

Exactly what I need. Thank you!

even better option.

@emmanuel How can we make sure the random string is unique and nobody else in your database has the same number?

No we can’t do that at the generation level, but you can do this at the workflow level, with a condition.

1 Like

@mobibuilder, @emmanuel Wanted to comment on this thread a little further.

I want to create a random combination with letters and numbers, but I want a specific naming convention. For example, all of the code numbers should the letter “R” and the rest of code can be 6 numbers. So it should read R098182

Just trying to force the letter R before the random number string.

You can absolutely do this. If you use a simple convention like in your example, then just add the static content you want before the calculation.

8 Likes

This thread is a amazing. Is it possible to do something like what @jonaspalmqvist recommended, but with dynamic data?

What I am trying to build is a referral link to our app. I was thinking that I might want to incorporate something personal, like a first name. Or possibly, just the first 3 letters of a name. The name should be pretty easy, just point to the ‘thing’ in the database. What about restricting the length to just 3 letters?

Or possibly the first letter of the first name +3 letters of the last + random 5 digit string. Something like: jdoe532X9A4.

Related to this, we want to create some customer ID numbers. Is there a way to make the string sequential for this purpose?

1 Like

How is it that we can do this I’m trying to figure it all out now and i can’t figure out how to check the generated text to the field values in a selected thing

What I am trying to build is a referral link to our app. I was thinking that I might want to incorporate something personal, like a first name. Or possibly, just the first 3 letters of a name. The name should be pretty easy, just point to the ‘thing’ in the database. What about restricting the length to just 3 letters?

Or possibly the first letter of the first name +3 letters of the last + random 5 digit string. Something like: jdoe532X9A4.

Sure you can. Remember you can use multiple expressions when using dynamic data to get what you want. Example of your suggestion, only with first 3 letter of first name + random 3 digit string.

result =

Related to this, we want to create some customer ID numbers. Is there a way to make the string sequential for this purpose?

I’m pretty sure you could make the ID sequential. It would however require some more building but the logics would probably be something like a default value defined in a data field in your DB. Then on top of that a +1 function when your [customer] is created. You can find incremental functions on the forum by searching a little.

3 Likes

Implementing this.

The first example makes sense. But the ‘trimmed from end’ works backwards. So it starts and the end and cuts the front of till it is only the last letters showing. But, there is a ‘truncated’ option that works as described.

Unfortunately, the 2nd part regarding the sequential ID numbers isn’t working so well. I tried to do a search for customer id’s (the name of the field to modify) to yield the ‘max’. Then added +1 as you described. I have another user as the ‘anchor’ at 999, so the next user should be 1,000. However, it just ends up being 1. Here are screens of the set up below:

I tried with ‘last item’ as described on this post as well, still no luck. Same result.

The first example makes sense. But the ‘trimmed from end’ works backwards. So it starts and the end and cuts the front of till it is only the last letters showing. But, there is a ‘truncated’ option that works as described.

yeah, oops. You can choose the one you like there :slight_smile:

Unfortunately, the 2nd part regarding the sequential ID numbers isn’t working so well. I tried to do a search for customer id’s (the name of the field to modify) to yield the ‘max’.

Hmm, I get it to work with your argument.

with both :last item and :max. Could you run through it again to see what’s up? I thought it had something to do with your thing to change but also works for me. I don’t know what your workflows look like before this one though.

It’s not a refresh DB issue?

2 Likes

oh wow. I don’t know. I’ll have to play with it a bit more.

I know that I had data imported with different customer IDs. that field I set to ‘text’. But I created another field (the one in the screenshot) which is set as a number. so that is not it. And I can’t see how it is a DB refresh issue since the ‘+1’ was added, but the ‘search for’ didn’t yield any result. thus the ‘1’.

I noticed that yours was called ‘customer IDnumber’ That shouldn’t matter as I am sure that is just how you labeled the field…

hmmm… I’ll have to play around a bit more.

thank you!

This is possible, if for example you recently added the field, or manually edited the “999” it wouldn’t have given the UI’s cached database values a reason to update.

Incrementing the max ID is a vulnerable method; when two users are created at roughly the same time they might return the same max ID, and caching makes the window of error even larger.

Note that each row already has a unique ID, that looks like:
1477501458054x757140743779018500

I take it you want something useful for people to read though!

Ideally it would be possible to use a database sequence, or PostgreSQL’s serial number type, to guarantee a unique number, perhaps the Bubble developers can make it available?

Edit: there is a workaround posted here, still allows collisions but less likely than if sequential:

Edit again: Could use Bubble’s random number generator function instead of calling the API.

1 Like

Correct. I tried both text format and number. FYI, :max applies to numbers only. :last you can use for both number and text. Your format should be number I assume. I noticed you also had that format defined.

Considering I got it to work in both cases, I’m thinking it must be other error. I’m uncertain of the rest of your setup ofc.

If you’re not going to use the sequential number for something that “requires” you to, I’d much rather just use the “random string generator”, like @mishav says. By doing so, you’re pretty safe from errors.

1 Like

So, @mishav may have hi the nail on the head. I I manually entered the field recently, and added the 999. So perhaps that is it. However, if I sign up another user, it still ends up being ‘1’ as well, instead of ‘2’. Not sure if that affects the theory at all.

Yeah, this is interesting. For our convenience, it would be easier to have it sequential, just for knowing how many customers we have. And later on, how many orders have been fulfilled.

Maybe I am misunderstanding how bubble arguments work, but I don’t think we could use the unique ID any way, as I would need to use this information for dynamic data. So I couldn’t use the unique ID as a customer ID, because I then wouldn’t be able to get workflows to add this to invoices later, correct?

The problem I am seeing with ‘random strings’ is duplication. Wouldn’t I have to set up a bunch of other workflows to check that it is unique?

While checking for uniqueness would be a bit more complicated that anything else I’ve done with bubble so far, I could see away. Just search for the ID, if it is found, repeat the function. I guess I don’t see how to keep this cycle going until it finds one the is unique, if many cycles happen to be needed.

The nicest solution is to assign the creation of the unique ID to a single source, which usually means the database as that’s where the value will be stored anyway.

The creation of the ID could be delegated to somewhere else, and retrieved via an API, for example using the database API to an external database which returns the next value of a sequence.

Allowing collisions and retrying is a valid solution, but the logic is messier to handle the collision situations and possible multiple retries.

1 Like