# Placing the Reference data value with code

**URL:** <https://forum.wixstudio.com/t/placing-the-reference-data-value-with-code/58607>\
**Category:** Ask the community\
**Tags:** cms, wix, question, code\
**Created:** [April 28, 2024, 1:07am UTC](https://forum.wixstudio.com/t/placing-the-reference-data-value-with-code/58607 "2024-04-28T01:07:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rebecca10202](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/rebecca10202/32/85915_2.png) [@rebecca10202](https://forum.wixstudio.com/u/rebecca10202)\
**Post date:** [April 28, 2024, 1:07am UTC](https://forum.wixstudio.com/t/placing-the-reference-data-value-with-code/58607/1 "2024-04-28T01:07:41Z")

</div>

**Question:**  
I am retrieving a referenced value from a CMS table to place in another table. I can see in the console and through a variable I’ve placed on a form that the reference \_id is accurately being capture. However, when the form is saved, the reference \_id value doesn’t seem to be storing correctly. The tag in the CMS shows ‘Undefined’ as if it knows there should be an item referenced. Do you have any suggestions?

**Product:**  
Wix Editor,

**What are you trying to achieve:**  
I am creating a new item to place in a CMS table with a referenced field. Upon entering the Add Form, I am consuming in variables retrieved from other tables. One of those variables is a Referenced field. All variables on the form are validated as correct – both in the console and by display. But, when the form saves, it doesn’t save the Referenced field correctly.

**What have you already tried:**  
I have read all information about referenced fields – get the referenced items \_id, make sure it is in text form, etc, etc. None seems to resolve the issue.

---

<div class="post-metadata">

**Author:** ![anthony](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/anthony/32/79536_2.png) [@anthony](https://forum.wixstudio.com/u/anthony)\
**Post date:** [April 29, 2024, 5:43pm UTC](https://forum.wixstudio.com/t/placing-the-reference-data-value-with-code/58607/2 "2024-04-29T17:43:25Z")

</div>

Can you share the code that isn’t working?

---

<div class="post-metadata">

**Author:** ![rebecca10202](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/rebecca10202/32/85915_2.png) [@rebecca10202](https://forum.wixstudio.com/u/rebecca10202)\
**Post date:** [April 30, 2024, 3:08am UTC](https://forum.wixstudio.com/t/placing-the-reference-data-value-with-code/58607/3 "2024-04-30T03:08:13Z")

</div>

Repasting the code to the correct entry. There are 2 pages that are a part of the user workflow here. It is the Add Activity page that the reference field is not storing correctly to the CMS.

Page 1: Second dynamic page connected to a list of Activity Items.

import wixMembers from ‘wix-members’;  
import { currentMember } from ‘wix-members-frontend’;  
import wixData from ‘wix-data’;  
import wixLocation from ‘wix-location’;  
import { session } from ‘wix-storage’;

$w.onReady(async () =\> {  
const today = new Date();  
const member = currentMember.getMember();  
const itemID = session.getItem(“itemID”);  
const itemFarm = session.getItem(“itemFarm”);  
const itemYear = session.getItem(“itemYear”);  
const itemFarmCode =session.getItem(“itemFarmCode”)  
console.log(itemFarm, itemYear);

```
await $w('#dataset1').onReady( () => {
    console.log("dataset is ready")  

    $w("#farm").text = itemFarm

    // NOTE: this code throws an error, because the variables are still Null rather than having data. Only a manual refresh on the URL works to retrieve the data.

   // NOTE: Upon refresh, the Console shows the session variables have been retrieved and the activity list for the selected farm is accurate. 

    $w("#dataset1").setFilter( wixData.filter().contains("title", itemFarm )
           .and(wixData.filter().contains("cropYear", itemYear)))

    $w("#editBtn").onClick( (event) => { 
        const $item = $w.at(event.context); 
        const itemId = event.context.itemId;
        const data = $w("#repeater1").data;
        const itemData = data.find((item) => item._id === itemId);
        const index = data.findIndex((item) => item._id === itemId);
    })

```

});  
})

Page 2: This code is attached to a custom form for entering the activity that occurred on the Farm.  
import { session } from ‘wix-storage’;  
import wixWindow from ‘wix-window’;  
import wixMembers from ‘wix-members’;  
import { currentMember } from ‘wix-members-frontend’;

$w.onReady( async () =\> {  
const today = new Date();  
const itemID = session.getItem(“itemID”);  
const itemFarm = session.getItem(“itemFarm”);  
const itemYear = session.getItem(“itemYear”);  
const itemFarmCode\_id = session.getItem(“itemFarmCode\_id”);  
const itemFarmCode = session.getItem(“itemFarmCode”);  
const itemMember = await currentMember.getMember().then( (member) =\> {  
$w(‘#myMember’).value = member.loginEmail ;  
return member })

```
   // NOTE: The itemFarmCode_id and itemFarmCode are verified in the console as the correct referenced data. Additionally, there are fields placed on the form that allow for visual reference. But when the data is updated to the CMS, the tag for the referenced field says "Undefined".

    console.log(itemFarm, itemYear, itemMember, itemFarmCode_id, itemFarmCode);

$w("#dataset1").onReady(() => {
	$w('#farm').value = itemFarm;
	$w('#year').value = itemYear;
	$w("#farmCode").text = itemFarmCode;
	$w("#itemID").text = itemFarmCode_id
	$w("#dataset1").setFieldValue("title", itemFarm);
	$w("#dataset1").setFieldValue("cropYear", itemYear);
	$w("#dataset1").setFieldValue("member", itemMember.loginEmail);
	$w("#dataset1").setFieldValue("farmCode", itemFarmCode_id);

});

```

})
