Creating Unique ID at the time of formSubmission

I have created a form with various fields (connected to a dataset within a data collection). When a user clicks submit all the data is stored in that collection.
However I wish to create a unique ID if any user submits the data. Can someone help with such a code?
I need it to be different unique ID to the automatically generated system unique ID.

I have followed this thread , but am having no luck at all! I just dont know what I am doing wrong! ? Id be so grateful for some help! I am not a natural coder

Do you have any code currently??
If so, paste it here…

I took this code from a previous thread but hasn’t made a difference…
//inside data.js
import wixData from ‘wix-data’ ;
export function pec200_beforeInsert ( item, context ) {
return wixData . query ( “Pec200” )
. descending ( “uniqueId” )
. limit ( 1 )
. distinct ( “uniqueId” , { “suppressAuth” : true , “suppressHooks” : true })
. then ( r => {
let lastItem = r . items [ 0 ];
let newId ;
lastItem ? newId = lastItem + 1 : newId = 1 ;
return item ;
})
}

i wondered if I needed to do anything to the dataset Fields ? Shall I screen shot the pec200 datasheet ?

Hi @gergemina and @ajithkrr … :raised_hand_with_fingers_splayed:

You can do this with a data Hook ( beforeInsert ).

import wixData from 'wix-data';
export async function collection_beforeInsert(item) {
    let id, unique;
    
    while (!id && !unique) {
        id: String(Number(Math.floor(Math.random() * 10000000000)));
        let count = await wixData.query('col').eq('id', id).find().then((result) => {
            return result.totalCount;
        })
        
        if (count > 0) { item.id = id; unique = true; }
    }
    
    return item;
}

Hope this helps~!
Ahmad

1 Like

Thanks for this, sadly it hasn’t worked, unless I am missing something?

This code must be placed on the backend JavaScript file (data.js).
I’ve updated the answer, please try again now and let me know how it goes.

Yes, I inserted it there. However it did not recognise ‘col’. My collection and dataset is called PEC200. The following does not work for me

import wixData from ‘wix-data’ ;
export async function PEC200_beforeInsert ( item ) {
let id , unique ;

while  ( ! id  && ! unique ) { 
    id :  String ( Number ( Math . floor ( Math . random ()  * 10000000000 ))); 
    let  count  =  await  wixData . query ( 'col' ). eq ( '_id' ,  id ). find (). then (( result )  =>  { 
        return  result . totalCount ; 
    }) 
    
    if  ( count  > 0 ) {  item . _id  =  id  } 
} 

return  item ; 

}

“col” is the collection/database name, in your case it must be replaced with " PEC200".

Also, update your code with my most recent updated answer above.

thanks, do I need to create a field in my database to allow for the new autogenerated ID?

Yes, it needs to called “id” for the code to work, or just replace the “id” in the orange part in my answer (item. id ) to your field key.

Should it be text field or number field?

A text field, also note that if you want to change the field key, you need to change it both from the query and from the value assignment. (Notice the orange parts).

Im so grateful for all your help so far but it still isn’


t working. Here is a screen shot of my code, also my dataset, as well as my form.

1 Like

Also unfortunately entering this code has meant that the form on my live site does not work at all. When I removed the code it started working again… I was wondering if you could identify whats going wrong here?

@ahmadnasriya

Maybe because you haven’t published the site after adding this code. Try it and let me know how it goes.

@gergemina did you find a solution? I have the exact same question…

Yes I did, I created a data.js file in the backend

and then used this code:

//inside data.js
import wixData from ‘wix-data’ ;
export function PEC200_beforeInsert ( item , context ) {
return wixData . query ( “PEC200” )
. descending ( “id” )
. limit ( 1 )
. distinct ( “id” , { “suppressAuth” : true , “suppressHooks” : true })
. then ( r => {
let lastItem = r . items [ 0 ];
let id ;
lastItem ? item . id = lastItem + 200 : item . id = 1 ;
return item ;
})
}