I have a module with a key field that is populated by a different dataset than the one in which the data is then saved.
I need the code to save this field in the form as well as to create the relationship between the two datasets.
I tried to use this code,
$w(“#DTS1”).setFieldValue(‘IDCode’, ($w(“#IDCode”).value));
// DTS1 is the new dataset
// IDCode is the field in the collection of DTS1 dataset
// IDCode is the field in the form that is loaded from another dataset DTS2
but it saves an empty record and says “reference is broken”
Can you please give me informations?
Thanks in advance as always
Just use getCurrentItem to get your wanted data from the dataset.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem
$w("#input1").value = $w('#dataset1').getCurrentItem();
// for all fields
$w("#input1").value = $w("#dataset1").getCurrentItem().fieldName;
// for certain field
Then use setFieldValue or setFieldValues to set the field of the current element on your page.
Dataset - Velo API Reference - Wix.com
Dataset - Velo API Reference - Wix.com
export function Submit_click(event, $w) {
$w('#dataset2').setFieldValue('fname', $w('#input1').value);
Then just save it using the save function.
Dataset - Velo API Reference - Wix.com
Hi, #givemeawhisky
Thank you for the code, unfortunately it does not seem to work when with reference field.
I tried to look for a solution in the forum for days, but I can’t find it.
Can you still give me some help?
In practice, if the field is created in the destination Dataset, the code works good, but if it is a referenced field it does not write the item.
The value that is displayed in the reference field comes from the Main field of the specific item in the referenced collection. The actual value that is stored in the database is the ID of the referenced item (in other words, the value in the ID system field of the referenced collection).
https://support.wix.com/en/article/about-reference-fields-in-database-collections
Note that you cannot connect any element directly to a multiple-item reference field.
https://support.wix.com/en/article/working-with-multiple-item-reference-fields
You can read about using reference fields here.
https://support.wix.com/en/article/about-referencing-multiple-items-in-one-field
Finally, see these previous posts for more info.
https://www.wix.com/corvid/forum/community-discussion/import-data-reference-field
https://www.wix.com/corvid/forum/community-discussion/reference-fields-in-dataset
Hi #givemeawhisky
I read what are you provided and the part of the query “include function” was useful because I had not found it yet.
However, my problem is not to get the data from the reference field (function that the query would do), but to insert it in the associated field. In practice the collection 1 has the populated field, I have to save into reference field of collection 2
I’ll give you some examples I found but I can’t able to adapt to my case, but they explain my situation:
This is the same situation of mine
https://www.wix.com/corvid/forum/community-discussion/reference-field-1
This is your solution in case of repeater, but I have only one field.
https://www.wix.com/corvid/forum/community-discussion/accessing-a-field-in-a-referenced-collection-by-code-using-itemdata
Can you help me?
Thank you
hi to everyone,
I searched for various solutions, I tried to write this code using insertReference. It does not report errors, but it does not work.
I simply have to read the IDCode present in the field of collection A (DTS2) and write it in the same field referenced in the collection named “AssociativeSpace”.
Can you help me please?
Thanks in advance as always
export function BServiceOnline_click(event) {
let CompanyCode = $w(“#DTS2”).getCurrentItem().IDCode
wixData.insertReference(“AssociativeSpace”, “IDCode”, “” , CompanyCode)
.then(() => {
console.log(“Reference inserted”);
})
}
hi #givemeawhisky,
I wrote more than 10 days ago that I still have problems with the solution proposed here.
Can you or anyone help me? Why didn’t you see me anymore?
I found that you can also use the Save command for a reference field, but is it my case?
I don’t know how to use it because the numerous attempts do not mark me an error in the code, but they do not bring results.
Firstly, remember that all of us Forum Ninjas here in this forum are not employed by Wix themselves, we are only doing this in our own time so it will vary between us all as to when we are all on and at any given time.
So all of us forum ninjas do this for free and if we are busy with our own work or away on holiday with family for example, then our heads and minds will not be thinking about this forum. As like myself with it being school half term and myself taking family away on hols, so I am not going to spend all my free time on a forum and neglecting my holiday…
Yes, as you are using the reference field etc, you need to be using the Wix Data Save function and not the Wix Dataset Save function as Wix Data lets you play with the reference fields,
https://www.wix.com/corvid/reference/wix-data.html#save
However, as you are only inserting the one item, then you can just use the Wix Data Insert instead of using the Wix insertReference.
So use https://www.wix.com/corvid/reference/wix-data.html#insert
Instead of https://www.wix.com/corvid/reference/wix-data.html#insertReference
You can see more in this previous forum post here too.
https://www.wix.com/corvid/forum/community-discussion/wixdata-insertreference-not-working-with-single-reference-field
Hi #givemeawhisky , sorry, I thought you worked for wix and not that it was volunteering. Then thank you even more for all the support.
Unfortunately I’m trying in every way, but it doesn’t work. If I use the same code for a standard field, It’s write the data, but if I use it for a reference field it always gives me the phrase “reference is broken”
Do you know how to fix the problem?
this is my code
let code = $w(“#DDTS1”).getCurrentItem().ContestCode
let toInsert = {
“CodeContest”: code,
};
wixData.insert(“SubscriptionContest”, toInsert)
.then((results) => {
let item = results;
})
. catch ((err) => {
let errorMsg = err;
});