Forum Academy Marketplace Showcase Pricing Features

How to achieve Unique, Sequential Numbers Reliably?

Can you show us what that looks like?

I assume this isn’t idle curiosity …

The “one-liner” is a bit nasty, because it transforms Bubble’s list into a Javascript array, before it can do the indexOf operation, and it matches by unique id.

properties.thinglist1.get(0, properties.thinglist1.length()).map(x=>{return x.get("_id")}).indexOf(properties.thing1.get("_id"))

A working sample is at the bottom of this page: https://toolbox-example.bubbleapps.io/version-test/serverscript?debug_mode=true

Alternatively I could have a dynamic expression of unique ids and pass it in as part of the script, to make a simpler line of:

["mylist's unique id".join with "," ].indexOf("mybook's unique id")
1 Like

Thanks for the example. I prefer having coded solutions for discrete needs instead of plugins. Thanks of course to your Toolbox plugin which has been like a rock in terms of reliability!

1 Like

I am now just resisting this for another app I’m building and counting what’s in the list will not work if there are several orders being placed around the same time. As Keith has demonstrated you will end up with multiple records being assigned the same number.

Use List Shifter to add the numbers.

:boom: A WORD OF WARNING THAT SOME OF THE SOLUTIONS PEOPLE OFFERED ABOVE ARE NOT RELIABLE AND YOU MAY, IN FACT, GET DUPLICATES OR WORSE UNLESS YOU USE THE FOLLOWING METHOD AS - I HAVE TESTED THEM ALL JUST LIKE KEITH DID :boom:

Not knowing this caused me a lot of issues and setbacks so I hope it saves others. The solution that works perfectly and reliably in bubble natively is the solution buried at the end of Keith’s great video - and all the reasons are explained by him.

in summary the solution is:

Lets say you want unique, sequential order numbers that increment by 1

  1. Create a backend workflow that triggers on a new database trigger event (in my case an order)
  2. Make changes to the order “order now” where it does a search for all orders that have a creation date less than or equal to “this orders creation date” (MAKE STATIC) count +1

That’s it!
As I said, the reasons why this works (and nothing else does) is in Keith’s video if you want to know why.

Of course there are other solutions by using plugins that reference an external API but if you want a reliable, simple, native sequential order number then there you have it.

Screenshot 2020-06-11 15.51.17

16 Likes

Oh my god! When first time I tried this, and knowing this method works reliably I was shocked!
You both just made my day! Thank you thank you and thank you! @keith @morgan

1 Like

Am I correct to assume that if any records are deleted from the database it would be possible to have duplicate IDs?

Say I have 100 orders in the database now. The next order will be assigned ID #101

But if I now go and delete order with ID #3 from the database the next new order will be assigned order ID # 101 which will result in two orders with the same ID.

Is this correct?

1 Like

Without giving it much thought, the answer is yes, probably. However, if it is orders you’re talking about I would have a status field that changes to “cancelled” or something like that so there is no chance of this.

2 Likes

Hello @morgan,

Nice one! Would you mind telling me how can you format the number to have 10 digits with a constant number that starts by “4” so the number will be “4000000001, 4000000002”, etc.

Simply add the numeric code you would like to have like this, hope it helps you :slight_smile:

Untitled

1 Like

Thanks a lot @hanifyaskur,

It works! I thought it would more complicated.

Have a good day and thanks also to @morgan and @keith for the explanation.

Thank you so much man :slight_smile:

As I can see from here: Bubble.io Tutorial: Database Trigger Events - YouTube backend workflows are only available in professional plan and above. Is there any way to implement this solution in Free or Personal plans as we still prepare the project for the client ?

I’m sorry to resurrect an old topic, but going through everything, something keeps bugging me:

If you are doing a search for all orders with a creation date ≤ This Order’s Creation Date, presumably, the ‘count’ of these records, will include ‘This Order’… why then would we need to add a +1 at the end?

In this context, the +1 just creates a situation where the Serial Number is always one number higher than the actual number of records, right? Most probably not a big deal, either way. I’d just like to confirm if having the +1 is ‘redundant’.

Will appreciate any opinions.

Another thing:

Is there a reason why this should be done in the Backend Workflows, and not as part of the normal workflow process?

I generally need the unique IDs created as part of the ‘create a new thing’ process, so I can use that ID in a reference in the next part of the workflow. I can’t make this reference, when the unique ID’s creation takes place in a backend workflow.

I don’t think there’s a reliable way to implement this through regular workflow. Unless you’re sending data to the server one-by-one (and not expecting other user submits the same “thing” to the server in same time).

I never done this, but I suggest you to try the same formula “Search for x:make static:count +1”, use it as an input field prior to the data submission.

If it works, then hopefully will also answer @obvolut question. Since it will enable us to utilize the “IDs” through regular workflow as well.

It works when you do it as part of a regular workflow. I have it set up and running. Only thing is, like you mentioned: how will Bubble handle it if more than one user happens to ‘create a new thing’ at the very same second? Does anyone have any insight as to whether this is something that could realistically happen?

On the other hand, to get around this potential ‘loophole’, I’m considering switching things around a bit:

  1. Taking the process explained above, to create a unique sequential number, but setting that number as a slug. Since, slugs automatically check for duplicates, and append a -1, -2, -3, etc etc, where necessary.

  2. Then setting the slug’s value, as the thing’s ID.

Might not make sense for a lot of use cases, but in my use case, I’m creating a bunch of records, and their most important ‘unique’ feature, are their IDs. The records also use the IDs as their slug.
Having a few records here and there, that look like 10001-1, 10001-2… I could probably live with that. But I’d really rather not have to do this…

And Backend Workflows work on Personal Plans… unless something’s changed and I’m now on a ‘legacy’ plan of sorts… I only started paying in August 2020… I don’t think the terms & conditions of the Personal Plan have changed in that time…

Hi

I created a trigger custom event on the backend and gave it an event name

When I try to trigger that event in the workflow, the event name does not show in the trigger custom event name dropdown.

How do I trigger this event in the workflow?

How does this solution scale? For instance, if there are 100,000 orders that it’s searching over wouldn’t a count take a while?