# wixData.update for selected fields

**URL:** <https://forum.wixstudio.com/t/wixdata-update-for-selected-fields/40710>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [October 19, 2019, 1:35pm UTC](https://forum.wixstudio.com/t/wixdata-update-for-selected-fields/40710 "2019-10-19T13:35:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oberdan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/oberdan/32/85673_2.png) [@oberdan](https://forum.wixstudio.com/u/oberdan)\
**Post date:** [October 19, 2019, 1:35pm UTC](https://forum.wixstudio.com/t/wixdata-update-for-selected-fields/40710/1 "2019-10-19T13:35:54Z")

</div>

I am looking for a way for wixData.update to modify selected fields in the Collection without deleting every other field on the same row.

The collection has 10 fields and I am I am using this code to insert, display or update the Collection data for 3 specific fields ‘q11’, ‘q12’ and ‘q13’.

```javascript
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(()=> {
 if(wixUsers.currentUser.loggedIn){
        wixData.query("PreWorkSet1")
        .eq('_owner', wixUsers.currentUser.id)
        .find()
        .then((results)=>{
 let items=results.items;
            $w('#textBoxQ11').value=items[0].q11;
            $w('#textBoxQ12').value=items[0].q12;
            $w('#textBoxQ13').value=items[0].q13;
        })
    }
})

export function button1_click(event) {
 let userId = wixUsers.currentUser.id;
    wixData.query("PreWorkSet1")
    .eq("_owner", userId)
    .find()
    .then((results) => {
 let items = results.items[0];
 if (results.items.length === 0){
 let toInsertQOnes = {
 "q11": $w('#textBoxQ11').value,
 "q12": $w('#textBoxQ12').value,
 "q13": $w('#textBoxQ13').value
            };
            wixData.insert("PreWorkSet1",toInsertQOnes)
            .then((results2) => {
 let items2 = results2;
                wixLocation.to("/preworkset1-p2");
            })
            .catch((err) => {
 let errorMsg=err;
            })
        }
 else {
 let currentItemId = items._id;
 let toUpdateQOnes = {
 "_id": currentItemId,
 "q11": $w('#textBoxQ11').value,
 "q12": $w('#textBoxQ12').value,
 "q13": $w('#textBoxQ13').value
            };
            wixData.update("PreWorkSet1", toUpdateQOnes)
            .then((results3) => {
 let items3 = results3;
                wixLocation.to("/preworkset1-p2");
            })
        }
    }) 
}

```

The diplay part within the onReady works perfectly.

I am having problems with the part within ‘button1\_click’.

Any time a user submits the data, the three specific fields are inserted or updated perfectly, however, the other 7 fields in the same row get deleted.

How do I get the button1\_click to update the 3 fields without deleting the other 7?

---

<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 19, 2019, 5:34pm UTC](https://forum.wixstudio.com/t/wixdata-update-for-selected-fields/40710/2 "2019-10-19T17:34:41Z")

</div>

[https://www.wix.com/corvid/reference/wix-data.html#update](https://www.wix.com/corvid/reference/wix-data.html#update)  
The update() function compares the \_id property of the specified item with the \_id property values of the items in the specified collection. If an item in the collection has that \_id value, update replaced the item’s property values with the ones in the specified item. **If the existing item had properties with values and those properties were not included in the specified item, the values in those properties are lost.** The item’s \_updatedDate property is also updated to the current date.

You might be better using getCurrentItem and then setFieldValue or setFieldValues.  
[https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem](https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#getCurrentItem)  
[https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue](https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValue)  
[https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValues](https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFieldValues)

---

<div class="post-metadata">

**Author:** ![oberdan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/oberdan/32/85673_2.png) [@oberdan](https://forum.wixstudio.com/u/oberdan)\
**Post date:** [October 21, 2019, 11:28am UTC](https://forum.wixstudio.com/t/wixdata-update-for-selected-fields/40710/3 "2019-10-21T11:28:06Z")

</div>

Thank you @givemeawhisky I tried the setFieldValues, but I found it still deleted the other fields.

What I was trying to achieve turned out to be too complicated for my skills level, so I simplified the over user-experience to avoid too much coding.

I appreciate your help.

---

<div class="post-metadata">

**Author:** ![aquacruise](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/aquacruise/32/13048_2.png) [@aquacruise](https://forum.wixstudio.com/u/aquacruise)\
**Post date:** [November 1, 2019, 10:44am UTC](https://forum.wixstudio.com/t/wixdata-update-for-selected-fields/40710/4 "2019-11-01T10:44:55Z")

</div>

givemeawhisky, thanks for this - I’ve tried using getCurrentItem & setFieldValue instead of update() function, but just can’t get it to work - I’ve put a link to it here [https://www.wix.com/corvid/forum/community-discussion/async-update-vs-getcurrentitem-setfieldvalue](https://www.wix.com/corvid/forum/community-discussion/async-update-vs-getcurrentitem-setfieldvalue) please could you comment?
