Get dynamic page id while adding content to a wix code form by user.

Hey Wix team, Hi Guys,

How can I get the dynamic page id of a wix dataset item before saving it?

Use case:
I need to use the wix triggered email and sent the user a email with the link of the dynamic page that correspond to the form he submitted. But for that I need to get the dynamic page url and save it to the database so that i can link it with the wix automation variable.

I am using datasetname.new() to create the dataset item and I am saving the dataset using the properties window of a submit button. So saving the dynamic page url should happen between these two steps.

My idea is to use something like this

let postLink=wixLocation.baseUrl+“/DYNAMICPAGE_PREFIX/”+DYNAMICPAGE_ID;

What I don’t know and need help with :
Get the DYNAMICPAGE_ID while the user is adding info to the form and then save it to the database before the user clicks the submit button.

Thanks for your help

Hi @thrishtilabs ,

If I understand correctly, you’re using database item ID (_id field) as dynamic page ID.

When a new dataset item is created, its ID is available from the very beginning:

$w.onReady(() => {
  const dataset = $w("#dataset1");
  dataset.onReady(async () => {
    await dataset.new();
    const { _id } = dataset.getCurrentItem();
    console.log(_id);
  });
});

By the way, the recommended approach for form data submission is to use a write-only dataset. With a write-only dataset new item doesn’t need to be created explicitly — it’s already created:

$w.onReady(() => {
  const dataset = $w("#dataset1");
  dataset.onReady(() => {
    const { _id } = dataset.getCurrentItem();
    console.log(_id);
  });
});

Also dataset submission handler takes the item before save and the item after save as the arguments (wix-dataset - Velo API Reference - Wix.com):

export function dataset1_afterSave(itemBeforeSave, itemAfterSave) {
  console.log(itemBeforeSave._id);
  console.log(itemAfterSave._id);
}

@yevhen-pavliuk ,
Thanks It worked…
I was always getting the id of previous item whenever i used getcurrentitem . But when i used you async await code, i was able to get the currentitem when the new() is called. thanks…

@thrishtilabs
You are welcome!