How to generate Unique form ID

Hello!

How do I generate a unique ID for submitted forms? I have plans on creating a ticket management system in SharePoint and need some ideas on how to generate one.

Dear @DryChips,
You can try this - javascript - How do I create a GUID / UUID? - Stack Overflow

Hi Nikita,

I have this code which generates a random number but I've noticed from the form submissions that the number isn't being generated every time a new form is created.

/*Function will generate random Unique ID V2*/
function GenerateUniqueID() {

    //Custom Date code
    const newDate = new Date().toLocaleDateString('en-gb',{
             year:"2-digit",
             month:"2-digit"
   });

    //Remove backslash from newDate variable
    customDate = newDate.replace("/", "");

    //Unique number to be added into field
    fd.field('UniqueID0').value = 'CBF'+ Math.floor(Math.random()*100000) + customDate;

//call function on form load
GenerateUniqueID() //outcome: 123456MonthYear

Dear @DryChips,
When is the code running? Make sure you run the code inside of fd.spRendered() function:

/*Function will generate random Unique ID V2*/
function GenerateUniqueID() {
    //Custom Date code
    const newDate = new Date().toLocaleDateString('en-gb',{
             year:"2-digit",
             month:"2-digit"
   });

    //Remove backslash from newDate variable
    customDate = newDate.replace("/", "");

    //Unique number to be added into field
    fd.field('UniqueID0').value = 'CBF'+ Math.floor(Math.random()*100000) + customDate;
}
fd.spRendered(function(){
  //call function on form load
  GenerateUniqueID(); //outcome: 123456MonthYear
});

All the code is running inside of this, like so:

fd.spRendered(function(){

/*Function will generate random Unique ID V2*/
function GenerateUniqueID() {

    //Custom Date code
    const newDate = new Date().toLocaleDateString('en-gb',{
             year:"2-digit",
             month:"2-digit"
   });

    //Remove backslash from newDate variable
    customDate = newDate.replace("/", "");

    //Unique number to be added into field
    fd.field('UniqueID0').value = 'CBF'+ Math.floor(Math.random()*100000) + customDate;

//call function on form load
GenerateUniqueID() //outcome: 123456MonthYear

});

Dear @DryChips,
Is that the code how it is? I don't see where the function ends, doesn't it miss the } bracket?

Like so:

fd.spRendered(function(){

/*Function will generate random Unique ID V2*/
function GenerateUniqueID() {

    //Custom Date code
    const newDate = new Date().toLocaleDateString('en-gb',{
             year:"2-digit",
             month:"2-digit"
   });

    //Remove backslash from newDate variable
    customDate = newDate.replace("/", "");

    //Unique number to be added into field
    fd.field('UniqueID0').value = 'CBF'+ Math.floor(Math.random()*100000) + customDate;
}

//call function on form load
GenerateUniqueID() //outcome: 123456MonthYear

});

Hey Nikita,

Sorry for the confusion.

I checked Plumsail Designer and it includes the } bracket. I forgot to add this into the message above.

Really strange why this is happening...

image

Dear @DryChips,
Not sure why this could be the case, could you export the form as is, so we can analyze it?

Sure, check it out

Backup - 19.07.2023.xfds (114.9 KB)

Dear @DryChips,
Not sure what causes the issue - the code looks nice and clean. Maybe the users are clearing the field somehow? You can add an extra check before save to make sure that the field is populated:

fd.spBeforeSave(function(){
  if(!fd.field('UniqueID0').value){
    GenerateUniqueID();
  }
});

You'll need to move the GenerateUniqueID() function outside of fd.spRendered() for this to work.

1 Like

Hmm, the field is hidden on the form so users won't be aware of this field. Can this be added before the form loads or maybe get a condition to check if the field doesn't contains a value and then automatically refresh (F5) until it contains a value?

Dear @DryChips,
That would be more code intensive than the additional code above. You can also make the field required to ensure no form is saved without the ID.