How do I generate a unique ID?

Hi all,

I need to generate unique IDs. Starting with “REF” as prefix, and it has to be followed up by numbers starting from “000001” and gets increased by 1 each time. (E.g : REF000001…REF000002)

This has to be triggered by a button click every time when clicked and displayed on a pop-up like a message.

Hey! I think this can be done using storeValue.

I am curious though, are going to store these values in DB? And, do you want continuity in these IDs across different sessions?

I’ll be storing this in a DB after generating it, will not be continuing with other sessions.

Can you direct me on how I can do it through storeValue? I’m clueless as to how sequence generation are handled. Would appreciate the help.

So, there is no standard way to do this. Although here is one workaround -

  1. Create a JS Object with a init function.
  2. In this function initialize the id value like - storeValue(‘uniqueId’, 0). And set it to run on page load.
  3. Now, every time you need to generate new id, you can do it like -
getNewId: () => {
  let id = appsmith.store.uniqueId + 1;
  storeValue('uniqueId', id);
  return `REF${id}`
}

Is there a way to do this so that the ID persists across different sessions?

@vroybal If the goal here is to continue generating IDs where the last session left off: I would suggest saving the most recent value to a database when it’s generated. You could then load the value on page load to always stay up to date. I do wonder if this could cause problems in case you have more than one user using the app at a time.

Thanks! Do you have any ideas how I would initialize the first value?

@vroybal you can also use our custom JS libs feature to install the UUID library and generate a unique ID using {{UUID.generate()}}

Thanks! That is very helpful.