Hi Guys, i’m looking a way to help my wix custom form to auto-input incremental serial number for each form submitted.
My wix form is a custom form of report to allow our technician team to update their service report accordingly.
Currently, we face a problem with inserting an auto increment serial number depending on submission.
Example of process : Users enter the details for the report, once click submit then the data will write into our dataset collection. Then during the process of saving, we will be able to auto insert incremental serial number into dataset.
but what we are trying to do is to add a running incremental number for each submission.
not sure that’s possible to work with index.
like for example a form submitted, then in the text field of collection will automatically input the said number like ABC00001, then on next form ABC00002
It is possible.
First add a serialId field to your collection.
Then make an index with this field and set it as Unique index (not regular index).
Once you set it as unique, the system will not let you enter the same number twice, even if 2 users make a submissions at the very same moment.
Now after you added this field, you can do something like this:
In the backend data.js file, add an beforeInsert hook like this.
//backend/data.js
///top line:
import wixData from 'wix-data';
///
export async function CollctionName_beforeInsert(item, context){
let prevSerialId = 'ABC00000';
if(!item.serialId){
const prevItemQuery = await wixData.query('CollctionName').isNotEmpty('serialId').limit(1).descending('serialId').distinct('serialId');
const items = prevItemQuery.items;
if(items.length){
prevSerialId = items[0];
}
} else {
prevSerialId = item.serailId;
}
const nextNumber = Number(prevSerialId.replace('ABC', '')) + 1;//this one will remove the 'ABC' and process the number itself and will add plus one;
item.serailId = 'ABC' + nextNumber.toString().padStart(5,'0');
try {
return item;
} catch(err) {
return err.errorCode === 'WDE0123' ? CollctionName_beforeInsert(item, context) : Promise.reject(err)
}
}
This part is for the rare cases where several users have submitted at the very first moment, so one of the serailId got rejected for duplication (error code WDE123), in this case it try running the function again (and again) with with an incrementing number .
I didn’t do anything with other unexpected errors just rejected the submission. You can handle it as you want.
P.S. I wrote the answer right here and not in the editor so there might be typos, erros etc… + I haven’t tried it.
I’ve tried using the code, but it seems to prompt dataset validation error continuously on preview mode.
Have yet to try on live site yet, but what might be the cause of validation error if all field has been assigned correctly?
Any requirement to set the serialID, for now i’ve set to “Text” field
P.S. take into account that because you defined it to have only 5 digits (at the end), one you got 100k submissions, it will stop working correctly.
So if you plan to have more than 100k, add another zero.
Hi! Total noob to Wix and came across this thread because I need a field that autoincrements and I’d like to set the starting value. But this link from last year isn’t working. I’m also not a developer so how badly will i break things if I attempt this?