# Save field from dataset to another dataset

**URL:** <https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [October 16, 2019, 6:13pm UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358 "2019-10-16T18:13:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![info60119](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@info60119](https://forum.wixstudio.com/u/info60119)\
**Post date:** [October 16, 2019, 6:13pm UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/1 "2019-10-16T18:13:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [October 16, 2019, 8:01pm UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/2 "2019-10-16T20:01:26Z")

</div>

Just use getCurrentItem to get your wanted data from the dataset.  
[https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem](https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem)

```javascript
$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](https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue)  
[Dataset - Velo API Reference - Wix.com](https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValues)

```javascript
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](https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#save)

---

<div class="post-metadata">

**Author:** ![info60119](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@info60119](https://forum.wixstudio.com/u/info60119)\
**Post date:** [October 22, 2019, 5:01pm UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/3 "2019-10-22T17:01:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [October 22, 2019, 5:59pm UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/4 "2019-10-22T17:59:52Z")

</div>

The value that is displayed in the reference field comes from the [Main field](https://support.wix.com/en/article/about-database-collections#primary-fields) 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](https://support.wix.com/en/article/about-database-collections#system-fields) of the referenced collection).  
[https://support.wix.com/en/article/about-reference-fields-in-database-collections](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](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](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/import-data-reference-field)  
[https://www.wix.com/corvid/forum/community-discussion/reference-fields-in-dataset](https://www.wix.com/corvid/forum/community-discussion/reference-fields-in-dataset)

---

<div class="post-metadata">

**Author:** ![info60119](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@info60119](https://forum.wixstudio.com/u/info60119)\
**Post date:** [October 22, 2019, 6:19pm UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/5 "2019-10-22T18:19:20Z")

</div>

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](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](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

---

<div class="post-metadata">

**Author:** ![info60119](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@info60119](https://forum.wixstudio.com/u/info60119)\
**Post date:** [October 23, 2019, 3:49am UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/6 "2019-10-23T03:49:53Z")

</div>

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”);  
})  
}

---

<div class="post-metadata">

**Author:** ![info60119](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@info60119](https://forum.wixstudio.com/u/info60119)\
**Post date:** [October 28, 2019, 2:54pm UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/7 "2019-10-28T14:54:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [October 28, 2019, 8:58pm UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/8 "2019-10-28T20:58:19Z")

</div>

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](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](https://www.wix.com/corvid/reference/wix-data.html#insert)  
Instead of [https://www.wix.com/corvid/reference/wix-data.html#insertReference](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](https://www.wix.com/corvid/forum/community-discussion/wixdata-insertreference-not-working-with-single-reference-field)

---

<div class="post-metadata">

**Author:** ![info60119](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@info60119](https://forum.wixstudio.com/u/info60119)\
**Post date:** [October 29, 2019, 1:24am UTC](https://forum.wixstudio.com/t/save-field-from-dataset-to-another-dataset/41358/9 "2019-10-29T01:24:16Z")

</div>

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;  
});
