# Creating Unique ID at the time of Submission

**URL:** <https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537>\
**Category:** Ask the community\
**Tags:** code, wix-stores, question\
**Created:** [November 23, 2019, 3:08pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537 "2019-11-23T15:08:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![info38197](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/info38197/32/11256_2.png) [@info38197](https://forum.wixstudio.com/u/info38197)\
**Post date:** [November 23, 2019, 3:08pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/1 "2019-11-23T15:08:52Z")

</div>

Hi Guys,

So I have a form create with various field (connected to database). When a user clicks all the data is stored in database.  
However I wish to create a unique ID if any user submits the data. I understand that in order to do so a program needs to sort the data in descending order (by date) and then add +1 to the latest ID.  
Can someone help with such a code?

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [November 23, 2019, 4:58pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/2 "2019-11-23T16:58:12Z")

</div>

I don’t understand the question.  
When you insert a record to your collection, the system automatically creates a unique ID (This field is hidden by default when you open the collection but you can make it visible).  
So what are you trying to do? Do you need a different unique ID? Something else?

---

<div class="post-metadata">

**Author:** ![info38197](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/info38197/32/11256_2.png) [@info38197](https://forum.wixstudio.com/u/info38197)\
**Post date:** [November 23, 2019, 6:53pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/3 "2019-11-23T18:53:22Z")

</div>

Yes I need a different unique ID as i need to share these with client for followup of the sales. It shall go into the subject line of the email to the client for the order they have placed.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [November 23, 2019, 7:07pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/4 "2019-11-23T19:07:27Z")

</div>

So if you want to create a running number as an ID, you can make a beforeInsert hook,something like:

```javascript
//inside data.js
import wixData from 'wix-data';
export function myCollectionName_beforeInsert(item, context) {
return wixData.query("myCollectionName")
 .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;
 })
}

```

For more details about hooks:

> **[wix-data - Velo API Reference - Wix.com](https://www.wix.com/velo/reference/wix-data)**
>
> The wix-data module contains functionality for working with data in collections.

beforeInsertHook:

> **[Hooks - Velo API Reference - Wix.com](https://www.wix.com/velo/reference/wix-data/hooks)**
>
> Hooks that can be added to wix-data operations.

---

<div class="post-metadata">

**Author:** ![gergemina](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gergemina/32/8366_2.png) [@gergemina](https://forum.wixstudio.com/u/gergemina)\
**Post date:** [September 29, 2020, 6:46pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/5 "2020-09-29T18:46:43Z")

</div>

Thanks for this @J.D. I think this will achieve what I am hoping to do to, but where would you normally insert this code?

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [September 29, 2020, 7:22pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/6 "2020-09-29T19:22:56Z")

</div>

Like J.D´s example said:

```javascript
//inside data.js

```

---

<div class="post-metadata">

**Author:** ![gergemina](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gergemina/32/8366_2.png) [@gergemina](https://forum.wixstudio.com/u/gergemina)\
**Post date:** [October 6, 2020, 8:54pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/7 "2020-10-06T20:54:03Z")

</div>

I am still a bit confused as to how to execute this @J.D. I have created the hook for the data collection, but doesnt seem to be working? Do I need to add a field in the dataset called uniqueId?

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [October 7, 2020, 6:09pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/8 "2020-10-07T18:09:26Z")

</div>

Show the code, then we can help better.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [October 7, 2020, 6:27pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/9 "2020-10-07T18:27:05Z")

</div>

In the code above instead of setting newId set it as item.uniqueId (or whatever fieldkey you use):

```javascript
lastItem ? item.uniqueId = lastItem + 1 : item.uniqueId = 1;

```

---

<div class="post-metadata">

**Author:** ![gergemina](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gergemina/32/8366_2.png) [@gergemina](https://forum.wixstudio.com/u/gergemina)\
**Post date:** [October 7, 2020, 6:45pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/10 "2020-10-07T18:45:46Z")

</div>

Tried this, still no luck

 ![](https://us1.discourse-cdn.com/wix/original/3X/8/5/85f7a3d14b11758fc58cf701fc3aa660ae5ba932.png)

 ![](https://us1.discourse-cdn.com/wix/original/3X/a/a/aa8c450e1a7138f98aab23f5979b98e01bc1e040.png)

 ![](https://us1.discourse-cdn.com/wix/original/3X/1/a/1ae7b4f89610a579bc56162ae41b293dc7601ad8.png)

---

<div class="post-metadata">

**Author:** ![gergemina](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gergemina/32/8366_2.png) [@gergemina](https://forum.wixstudio.com/u/gergemina)\
**Post date:** [October 7, 2020, 7:36pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/11 "2020-10-07T19:36:41Z")

</div>

This is working in that an id is automatically generated, but the ID does not seem to change for each new entry, it remains as ‘1’ for every entry.  
Ie. shouldnt it go up in increments?

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [October 7, 2020, 7:36pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/12 "2020-10-07T19:36:45Z")

</div>

That’s because you forgot to change the field key in the query.  
You still have there: . descending ( “uniqueId” ) and . distinct ( “uniqueId”  
even though you decided to name it “id”. + you haven’t changed the item itself.  
It should be:

```javascript
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];
  lastItem ? item.id = lastItem + 1 : item.id = 1;
  return item;
 })
}

```

---

<div class="post-metadata">

**Author:** ![gergemina](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gergemina/32/8366_2.png) [@gergemina](https://forum.wixstudio.com/u/gergemina)\
**Post date:** [October 7, 2020, 7:46pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/13 "2020-10-07T19:46:28Z")

</div>

thank you, should the field id be numerical?

---

<div class="post-metadata">

**Author:** ![gergemina](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gergemina/32/8366_2.png) [@gergemina](https://forum.wixstudio.com/u/gergemina)\
**Post date:** [October 7, 2020, 7:46pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/14 "2020-10-07T19:46:49Z")

</div>

what if i wanted to include text in the unique id?

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [October 7, 2020, 7:53pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/15 "2020-10-07T19:53:43Z")

</div>

The code above is for id that is based on running numbers, so it should be numeric.  
If you want to include text, you;ll have to make adjustments based on what exactly you want to do.

---

<div class="post-metadata">

**Author:** ![gergemina](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gergemina/32/8366_2.png) [@gergemina](https://forum.wixstudio.com/u/gergemina)\
**Post date:** [October 7, 2020, 8:01pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/16 "2020-10-07T20:01:43Z")

</div>

Im so grateful thank you its working good now. If I was to have the following PEC200-2020-1 and then going up in increments of +1 what would i need to do? Not to worry if this is too complicated!

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [October 7, 2020, 8:36pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/17 "2020-10-07T20:36:43Z")

</div>

the easiest way will be to have another field “idNumber” make it numeric and then:

```javascript
import wixData from 'wix-data';
export function PEC200_beforeInsert(item, context) {
return wixData.query("PEC200")
 .descending("idNumber")
 .limit(1)
 .find({"suppressAuth": true, "suppressHooks": true})
 .then(r => {
  let lastItem = r.items[0];
  lastItem.idNumber ? item.idNumber = lastItem.idNumber + 1 : item.idNumber = 1;
  item.id = "PEC200-2020-" + item.idNumber;
  return item;
 })
}

```

---

<div class="post-metadata">

**Author:** ![gergemina](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gergemina/32/8366_2.png) [@gergemina](https://forum.wixstudio.com/u/gergemina)\
**Post date:** [October 7, 2020, 8:52pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/18 "2020-10-07T20:52:08Z")

</div>

thank you!

---

<div class="post-metadata">

**Author:** ![jana9340](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jana9340/32/26327_2.png) [@jana9340](https://forum.wixstudio.com/u/jana9340)\
**Post date:** [February 21, 2021, 4:52pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/19 "2021-02-21T16:52:51Z")

</div>

@jonatandor35 May I jump in and ask for help with this same task?

I’ve created a dataset (allPets) where I want an distinct ID separate from the one the system creates. The data is user-generated via a form, so the numbering needs to be done via a hook.

I’ve added the following code to the backend:

 ![](https://us1.discourse-cdn.com/wix/original/3X/d/0/d0b9a7735ab80cbe7204b3a16b39a63f11cce598.png)

The petID field is numeric.

After I published and tested with a new entry into the dataset, the result was a blank field vs a number (below) instead of the expected 20004. I am not starting the petID numbers at 1, but since it’s sorting in descending order and adding 1 to the last item, I don’t think that’d cause the issue.

 ![](https://us1.discourse-cdn.com/wix/original/3X/2/2/22ab52be58f87e34669b1cb859bf3e7ba5244fe9.png)

PS no errors in the console log.

It seems simple enough, but I have to be missing something somewhere.

Thanks!

---

<div class="post-metadata">

**Author:** ![jana9340](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jana9340/32/26327_2.png) [@jana9340](https://forum.wixstudio.com/u/jana9340)\
**Post date:** [February 21, 2021, 8:31pm UTC](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537/20 "2021-02-21T20:31:21Z")

</div>

O.M.G. I found the error. I

had started my site with a canned dynamic page from Wix and built the dataset from there. Although I changed the dataset name throughout to allPets, when I used the hover function to add a hook to it, the hook came back with the original “Team” name for the dataset instead.

I guess I can stop banging my head against a wall now. 🙂

[Next page](https://forum.wixstudio.com/t/creating-unique-id-at-the-time-of-submission/35537.md?page=2)
