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}`
}