I have a form the submits to a data collection. How can I get the id that is associated with the submitted record so I can query the collection and use the data in a calculation. for example if I were posting to a sql server database I would insert the record and then call Select SCOPE_IDENTITY(). Is there a way to do this will form submission to a data collection?
If the user is a current user and logged into the website, then you can just get their user id through code whilst they are on. https://www.wix.com/corvid/reference/wix-users.User.html
https://www.wix.com/corvid/reference/wix-users.html#currentUser
Otherwise you can just use the Wix Data Query with the eq function to find a particular user and pull their details from that dataset. https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html
Something basic like this with the user id…
.eq("_id", userId)
As you are talking about SQL Servers too, I assume that you have read about connecting Wix to external datasets?
Velo: Integrating External Databases with Your Wix Site | Help Center | Wix.com
Velo: Adding and Deleting an External Database Collection | Help Center | Wix.com
https://www.wix.com/corvid/reference/external-database-collections.html
@givemeawhisky thanks for some reason I was not notified about your post. I am not looking for a userid. I want to query the form data the user just submitted and was added to the data collection so I can show them what the just submitted to check for errors. I am using a wix data collection not an external SQL Server. A data collection has a key field called id. I’d like to get that id field so I can query for it and show the data on a new page.
Yes you are talking about the ID (_id) field that is the userid.
https://support.wix.com/en/article/about-your-database-collection-fields#system-fields
Get the ID of the current user if they are logged in currently and query your dataset for that ID.
https://www.wix.com/corvid/reference/wix-users.html#currentUser
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html
// rest of code...
wixData.query("Dataset")
.eq("_id", userId)
.find();
// rest of code...
Then just use getCurrentItem to get the data.
If you want to get a specific field’s data then you will simply need to put the field name at the end of the getCurrentItem.
Hi - I’m trying to do this exact thing but for a user that has not logged in or created an account yet. Is there a way to access the just submitted form data for use in a chart format?
I have tried doing the following:
$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.
FYI - formDataset and readDataset point to the same collection, but formDataset is write only and readDataset is read only.
@liz did you manage to make it work?