Create an unique _id for each form submission

Hi, Wix currently creates unique _id for each entry on the database, but i need to display my own ID that increments with + 1 each time a submission is made on a form as this will need to be displayed on Dynamic page at a later stage.

Is there a example or tutorial I can follow?
Help would really be appreciated.

Hi Daniel,
Are you using a “Submit” action of a button of code ?
It is possible with code. you need to get the last id of an item in the database collection and to insert the new item with plus one value
Roi

Hi Roi,

Yes, my form has 3 stages, and after the first submission I want the ID to be added to the Table so it can be displayed on the second stage. can you possibly give me example code for this?

Kind Regards

Hi Daniel,
Before moving on,
Lets say you have 50 items in your collection.
If you decide to erase one of them, you will have duplications.
Will it matter ?

Roi

Hi, the data will not be deleted, so it does not matter.
Thanks

Ok,
In that case you case use Hooks (Before insert)
In the Hook function, query your database collection length:

let numberOfItems;
wixData.query("yourDatabaseCollectionName")
.find()
.then((results) => {
    numberOfItems = results.items.length // this represents the number of items in the database collection.
    });

Then create “_id” field to “item” object with the numberOfItems plus one.
Good luck!
Roi

Hi Roi,

Thank you so much.

Roi Bendet wrote:

Hi Daniel,

Are you using a “Submit” action of a button of code ?

It is possible with code. you need to get the last id of an item in the database collection and to insert the new item with plus one value

Roi

However, this is not safe, unless there is only one user. In a multi-user enviroment, there are possible race-conditions.

Hi Roi,

“Then create “_id” field to “item” object with the numberOfItems plus one.”

I have tried a few, but cant seem to get it working, can you give me example code for the “_id” field to “Item” object?

Hi Daniel, Can you share the link to your site ? Its would be easier to inspect

Roi

Hi

https://bassondaniel2.wixsite.com/prophetsa/

https://bassondaniel2.wixsite.com/prophetsa/job-card

Thanks

Hi Daniel,
Check this out:
import wixData from ‘wix-data’;

export function Jobcard_beforeInsert(item, context) { // item contains the inputFields from the form
	return wixData.query("Jobcard")
		.limit(1)
		.descending('_createdDate') // descending() sorts the results in descending order by property, i choose "_createdDate"
		.find()
		.then((results) => {
			const lastItemInCollection = results.items[0];
			item._id = `${Number(lastItemInCollection._id) + 1}`; // "jobcardid" should be "_id". should define the first item as 0 in an empty collection.
			return item; // beforeInsert function returns the item
		});
}

Good luck!
Roi

Hi Roi,

Thank you, I really appreciate it!

Kind Regards

Hi Roi,
My website is a platform for job search… I had created dynamic profile pages for employers…students will be able to see the job opportunities and can apply too… here comes the problem…I can’t figure out for which particular job a student is applying for…can you help me to fix this?
my website:
https://www.fillmypocket.net/

Hi,
Can you be more specific ?
Please elaborate what you are trying to do and isn’t working.
Roi

When a student likes a job on he/she applies by clicking on the button" apply" on this page- https://www.fillmypocket.net/Employer-Profile/33443cd2-68d7-4f9f-86be-de303235a31a. This is a unique url with unique ID which was created for employer…after clicking " apply" the student will be directed to this link- https://www.fillmypocket.net/student-application-student-code. This link is a link to application form where student can submit his /her detail.
Now, problem is with - How will I be able to know for which specific job student has applied. I wanted a job profile ID to connect with student application. How can I do that?

Hi Roi, can we use you previous exemple code to return image fields values without modifications ?

your previous exemple :

export function MATCH_SUMMARY_beforeInsert(item, context) {
return wixData.query(“MATCH_SUMMARY”)
.limit(1)
.descending(‘_createdDate’)
.find()
.then((results) => {
const lastItemInCollection = results.items[0];
item.event = ${Number(lastItemInCollection.event) + 1};
return item; // beforeInsert function returns the item
});
}

I tried this code, without success :

wixData.query(“MATCH_SUMMARY”)
.descending(“_createdDate”)
.find() .then((results) => {
let lastRecordInserted = results.items[0];

wixData.insert("MATCH_SUMMARY", lastRecordInserted);  

})
.catch((error) => {
let errorMsg = error.message;
});

Thanks!

Hi,
I want to create case id and mention in the email when user submits the form. I am using automation to trigger a mail when someone submits the form. But how to include unique case id in the response mail. Please help.
My site : https://www.bvc.com.hk/warranty-policy