# Insert reference field by code

**URL:** <https://forum.wixstudio.com/t/insert-reference-field-by-code/29648>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [January 16, 2020, 10:56am UTC](https://forum.wixstudio.com/t/insert-reference-field-by-code/29648 "2020-01-16T10:56:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![urs](https://avatars.discourse-cdn.com/v4/letter/u/958977/32.png) [@urs](https://forum.wixstudio.com/u/urs)\
**Post date:** [January 16, 2020, 10:56am UTC](https://forum.wixstudio.com/t/insert-reference-field-by-code/29648/1 "2020-01-16T10:56:06Z")

</div>

Hi everyone,  
I have a common issue with the insert of reference fields into a collection. I get always the error “reference is broken”.  
Unfortunately there are many unsolved posts concerning this topic in the forum and I wonder if there is no one out there who knows the answers?  
With one answer you are able to make many people happy 🙂

[https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-2](https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-2)  
[https://www.wix.com/corvid/forum/community-discussion/reference-field-broken](https://www.wix.com/corvid/forum/community-discussion/reference-field-broken)  
[https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-3](https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-3)  
[https://www.wix.com/corvid/forum/community-discussion/reference-field-1](https://www.wix.com/corvid/forum/community-discussion/reference-field-1)  
[https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-1](https://www.wix.com/corvid/forum/community-discussion/reference-is-broken-1)

I have two collections  
1: GuestPhotos  
photoid, photo, year, month\_id, motif\_id, uploadedby, description

2: Month  
month\_id, landuage\_id, month\_d, month\_f

Now I’d like to insert a record with a new photo to the GuestPhotos collection.  
Without referenced fields I make that this way:

```javascript
let dataRecord = {
photoid : newPhotoId,
uploadedby : userName,
year : Number($w('#ddYear').value),
month_id : $w('#ddMonth').value,
motif_id : $w('#ddMotif').value,
description : $w('#tbDescription').value,
photo : imageUrl
};
// Speichern des Datensatzes in der Collection
const insertResults = await wixData.insert("GuestPhotos", dataRecord)

}

```

I’ve already studied the wixdata.insertReference() method, but I can’t find the right way to use it.

---

<div class="post-metadata">

**Author:** ![urs](https://avatars.discourse-cdn.com/v4/letter/u/958977/32.png) [@urs](https://forum.wixstudio.com/u/urs)\
**Post date:** [January 16, 2020, 2:44pm UTC](https://forum.wixstudio.com/t/insert-reference-field-by-code/29648/2 "2020-01-16T14:44:53Z")

</div>

I just found the solution! I’ve kept the normal insert statement, but I changed the values of the dropdowns.  
I queried the field “\_id” of the collection Month (not month\_id!) and Motif, and filled the values as value for the corresponding dropdowns. Now the array with the data to insert looks like that:

1. “18”  
photoid:

2. “Urs Luginbuehl”  
uploadedby:

3. 2019  
year:

4. " 6b3a0e41-46ea-4ce7-8364-eba92bbccdfe"  
month\_id:

5. “3ae3e4ca-72da-4f75-b626-75b8876c2346”  
motif\_id:

6. “Description”  
description:

7. “image://v1/246733\_b27a21ac571a48499482fe588099220a~mv2.jpg/1400\_787/246733\_b27a21ac571a48499482fe588099220a~mv2.jpg”  
photo:

8. “50d1b71a-1d7c-444f-a397-d744ef5bc8eb”  
\_id:

9. “246733f0-e5a4-4235-bd27-11e9ca5a946b”  
\_owner:

10. “2020-01-16T14:32:20.794Z”  
\_createdDate:

11. “2020-01-16T14:32:20.795Z”  
\_updatedDate:

[4] and [5] are referenced fields \_id’s .

Perhaps you can explain the topic more clear than I.  
But anyway thanks for trying to help us.
