Get Id of inserted record

Hi,

I have user input form and need to retrieve id of currently inserted record upon submit button is clicked, then store it as reference filed in another collection. How to achieve it with coding ? Thanks.

wixData.insert() is a promise that returns the data of your inserted item including the id. So you would do:

wixData.insert('yourCollectionName', {'your items to insert'}).then(ins => {
    //ins._id is your new item's id
})
.catch(err =>{
//handle errors
});
3 Likes

What if your form is submitted by a user clicking the submit button at the end of clicking through several connected elements? E.g., no code is used to insert the data into the collection. But I want to retrieve the ID of the record that has been inserted when the user clicks the submit button, so I can use on another page?

Hey liz,

I personally don’t think it’s possible to do that without manually connecting your elements to data through code cuz the .then function won’t occur without a script

But

I recommend trying this
First add a new dataset, then connect it to your collection, create an onA fterSave event function (from the properties and event panel) for your other dataset connected to the elements

Then use this code below:

export function dataset1_afterSave() {
 // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
 // Add your code for this event here: 
 let id = $w('#dataset2').getCurrentItem._id()
 //"id" is the id of the recent added item
 //Then do whatever with the id
}

The logic I used here was after the person clicks on submit (onAfterSave) the other dataset (with the same collection) will get the current item’s id which is the last item in the collection, since it already saved it should be the inserted id that’ll be retrieved

I haven’t tried this yet on my code but I think it should work for you :slight_smile:

If you have any questions please askkk lol

Good luck
DJ bon26

Hi DJbon26, thank you for your idea! I tried it and it almost worked, so close.

Here’s what I did:

$w( ‘#formDataset’ ).onAfterSave(() => {
console.log( “After save” );
let id = $w( ‘#readDataset’ ).getCurrentItem()._id;
let currentItem = $w( ‘#readDataset’ ).getCurrentItem();
console.log(id);
console.log(currentItem);
})

But this unfortunately gets the second-to-last submitted item in the collection, not the latest submitted item. So it’s not what I need. Any other ideas on how to get the latest submitted item?

FYI - formDataset and readDataset point to the same collection, but formDataset is write only and readDataset is read only.

1 Like

Ok - I think it’s solved now. I did the following:

$w( ‘#formDataset’ ).onAfterSave((AfterSaveHandler) => {
console.log(AfterSaveHandler);
console.log(AfterSaveHandler._id);
})

AfterSaveHandler has the item object the user submitted and I can get the _id from there. I also tried doing the following:

$w(‘#formDataset’).onAfterSave((itemBeforeSave, itemAfterSave) => {
console.log(itemBeforeSave);
console.log(itemAfterSave);
})

And that seemed to work well too. I don’t really understand what the syntax portion of that .onAfterSave documentation means (below), but since the first code is simpler and seems to work I’m going to go with that.

function onAfterSave ( handler : AfterSaveHandler ): void
handler : function AfterSaveHandler ( itemBeforeSave : Object , itemAfterSave : Object ): void

1 Like

Maybe try refreshing the other datasetttt haha thats trueee,
If you don’t refresh it it won’t update

but since you said it kinda already worked for you, don’t worry about this.

All I added was just refreshing the other dataset before getting the current id

export function dataset1_afterSave() {
 // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
 // Add your code for this event here: 
 $w('#dataset2').refresh()
 let id = $w('#dataset2').getCurrentItem._id()
 //"id" is the id of the recent added item
 //Then do whatever with the id
}

Mybad
DJ bon26