I have a form on a page which accepts user input, connected to a dataset called #dataset1.
#dataset1 writes data to a Collection called Blocks
Upon submit, that data is inserted correctly into the collection.
Within the collection I have one column called BlockStatus that is a Reference Field.
It is connected to another Table called StatusTable which contains 1 column called Status.
StatusTable has just 2 records called Active and Inactive.
I want that upon button submit (or before insert into the collection), “Active” is written to BlockStatus. How can I achieve this considering that BlockStatus column is a reference field?
You can generate an onBefore-Save-Hook…
$w(“#myDataset”).onBeforeSave( () => {
start_myFunction();
console.log(“Continuing save”);
} );
function start_myFunction() {
//write here what should happen, before the data gets saved.
//here you can use wix-data to get the referenced data from your other database.
}
Another option could be…
https://www.wix.com/velo/reference/wix-data/insertreference
Hi Ninja,
Thanks for the tip on using “onBeforeSave”, I was using a BeforeInsert statement but I copied my code to onBeforeSave and got a step further. I experitmented with entering info into the Collection on the fly using “onBeforeSave” and all works well, but still having trouble with the reference field.
In the below image, the first record is what happened when I used code to insert the status of type “Pending”. The second record is what the result should be, but this was a record entered manually. I am using the below code. I guess that passing the string value to the reference field is not the right way, but I’m not sure how to pass it even after going through API docs.
function saveReferenceFields ( ) {
let issueStatus = “Pending” ;
let insertDate = new Date ();
console . log ( "saveReferenceFields() function called" );
let toSave = {
"name" : $w ( "#input1" ). value , //Works well!
"issueDescription" : $w ( "#textBox1" ). value , //Works well!
"emailAddress" : $w ( "#input2" ). value , // Works well!
"insertDate" : insertDate , //Works well!
"repotedIssueStatusTitle" : issueStatus //Gives me "Untitled" in the database instead of "Pending".
};
wixData . insert ( "colReportedIssues" , toSave )
. then (( results ) => {
let item = results ; // see item below
})
. catch (( err ) => {
let errorMsg = err ;
});
}

Like i already said, i do not use the reference-stuff, i have my own created QUERY-FUNCTION, which do all that magic on it’s on.
You can try to find out, if this one here, would help you out…
https://www.wix.com/velo/reference/wix-data/queryreferenced?_gl=116i12cw_ga*MTQzMjg0MDY5MC4xNjc5NzQ1NzI2