# Saving Fields Data in smart view (2)

**URL:** <https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895>\
**Category:** Help me!\
**Created:** [June 24, 2022, 9:03am UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895 "2022-06-24T09:03:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcoGarcia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/marcogarcia/32/2715_2.png) [@MarcoGarcia](https://community.forestadmin.com/u/MarcoGarcia)\
**Post date:** [June 24, 2022, 9:03am UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/1 "2022-06-24T09:03:47Z")

</div>

## Expected behavior

Hello, I’m creating a new topic similar to this one which was closed without answer [Saving Fields Data in smart view](https://community.forestadmin.com/t/saving-fields-data-in-smart-view/3836)

I’m wondering if there is any documentation around saving or updating fields within a smart view? Any help on this would be much appreciated

## Context

- Project name: mytraiteur
- Team name : Operations
- Environment name: Development | marco2
- Agent type & version:
- forest-express-sequelize version: 8.5.3
- express version: 4.17.1
- sequelize version: 5.15.1

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [June 24, 2022, 1:18pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/2 "2022-06-24T13:18:40Z")

</div>

Smart Views are more meant to replace the table-view by something which is content-specific for specific use-cases than to build full-featured forms and so on.

To modify data from a smart-view, the simpler way is to trigger smart actions (you may use “smart action forms”).

- [Smart Views - [Beta] Developer guide](https://docs.forestadmin.com/beta-developer-guide-agents-v2/frontend-customization/smart-views#triggering-a-smart-action)
- [https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions](https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions)

If you want to edit the records directly, you may get away with using ember-data directly.  
Under the hood, a smart-view is simply an ember component which is getting injected in the application.

- [Creating, Updating and Deleting - Ember Data - Ember Guides](https://guides.emberjs.com/v3.28.0/models/creating-updating-and-deleting-records/)

---

<div class="post-metadata">

**Author:** ![MarcoGarcia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/marcogarcia/32/2715_2.png) [@MarcoGarcia](https://community.forestadmin.com/u/MarcoGarcia)\
**Post date:** [June 24, 2022, 1:45pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/4 "2022-06-24T13:45:45Z")

</div>

I’m trying to update smart field, so I’m trying not to duplicate code that is already in  
I’m talking about `set` of smart field :

```javascript
{
  field: 'field',
  type: 'String',
  get: async (row) => {
    // doesn't matter
  },
  set: async (row) => {
    // code that I don't want to duplicate
  },
}

```

Is there a way to use the default `put` route ?

```javascript
router.put('/table/:recordId', permissionMiddlewareCreator.details(), async (req, res, next) => {
 // I would like to access this route from smart view
})

```

I want to trigger this when clicking on

```nohighlight
<BetaCheckbox @value={{someValue}} @small={{true}} @disabled={{false}} @onChange={{fn this.updateRecord this.currentRecord}} />

```

or

```nohighlight
<BetaRadioButton @onChange={{fn this.updateRecord this.currentRecord}} @class='c-beta-radio-button-duration' @namePath='label' @options={{options}} @value={{someValue}} @valuePath='value' />

```

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [June 24, 2022, 2:03pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/5 "2022-06-24T14:03:16Z")

</div>

Looking at the [ember-data documentation](https://guides.emberjs.com/v3.28.0/models/creating-updating-and-deleting-records/), would that work?

```javascript
import { action } from '@ember/object';

class MyComponent {
  [...]

  @action
  async updateRecord(record) {
    record.yourField = 'something';
    await record.save();
  }

```

---

<div class="post-metadata">

**Author:** ![MarcoGarcia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/marcogarcia/32/2715_2.png) [@MarcoGarcia](https://community.forestadmin.com/u/MarcoGarcia)\
**Post date:** [June 24, 2022, 2:28pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/6 "2022-06-24T14:28:09Z")

</div>

Yes it’s working but as I said i’m trying to update smart field. When I update smart fields that I’m using inside the smart view I’m not really updating a record. I’m updating a value inside redis :

```javascript
 {
  field: 'One of my smart field',
  type: 'Enum',
  enums: ['NA', 'À faire', 'À préparer', 'Envoyé', 'Validé', 'Modifié'],
  get: async function (order) {
    const equipment = await Redis.get(`order:${order.id}:equipment`)
    if (!equipment) {
      await Redis.set(`order:${order.id}:equipment`, order.equipment ? 'À faire' : 'NA')
    }
    return Redis.get(`order:${order.id}:equipment`)
  },
  set: (order, equipment) => {
    Redis.set(`order:${order.id}:equipment`, equipment)
    return order
  },
}

```

And I’m importing redis like this :

```javascript
const Redis = require('../config/redis')

```

And I can’t find a way to import my redis conf inside the component of my smart view

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [June 24, 2022, 2:30pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/7 "2022-06-24T14:30:38Z")

</div>

From the smart-view point of view, a smartfield is just another field, so this should not cause issues.

Are you having an error?

---

<div class="post-metadata">

**Author:** ![MarcoGarcia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/marcogarcia/32/2715_2.png) [@MarcoGarcia](https://community.forestadmin.com/u/MarcoGarcia)\
**Post date:** [June 24, 2022, 3:29pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/8 "2022-06-24T15:29:34Z")

</div>

Oh ok I didn’t get that.

I have this inside my template handlebar :

```javascript
<BetaCheckbox @value={{false}} @small={{true}} @disabled={{false}} @onChange={{fn this.updateRecord this.currentRecord type supplier}} />

```

I’m doing this in my component :

```javascript
  @tracked currentRecord = null

  @action
  selectRecord(record) {
    this.currentRecord = record
  }

  @action
  async updateRecord(record, type, supplier, value) {
    record.productOrder = { type, supplier, value }
    await record.save()
    return record
  }

```

with this smart field :

```javascript
 {
  field: 'productOrder',
  type: 'String',
  get: async function (order) {
    // some stuff
  },
  set: (logistic, { type, supplier, value }) => {
    console.log(logistic.id)
    console.log(type)
    console.log(supplier)
    console.log(value)

    return logistic
  },
}

```

and this route :

```javascript
router.put('/logistics/:recordId', permissionMiddlewareCreator.details(), async (req, res, next) => {
  const { body, params, query, user } = req
  console.log(body)
  // logic for update
})

```

But log of the body doesn’t contain productOrder. So it doesn’t go inside the set of my smart field

---

<div class="post-metadata">

**Author:** ![MarcoGarcia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/marcogarcia/32/2715_2.png) [@MarcoGarcia](https://community.forestadmin.com/u/MarcoGarcia)\
**Post date:** [June 24, 2022, 3:51pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/9 "2022-06-24T15:51:01Z")

</div>

That’s weird, I tried with this another action =\>

```javascript
<BetaRadioButton @onChange={{fn this.updateShipping this.currentRecord}} @class='c-beta-radio-button-duration' @namePath='label' @options={{this.shipping}} @value={{this.currentRecord.forest-Shipping}} @valuePath='value' />

```

```javascript
  @action
  async updateShipping(record, value) {
    record.shipping = value
    await record.save()
  }

```

And I do have the attributes logged :

```javascript
{
  data: {
    id: '7652',
    attributes: { Shipping: 'NA' },
    relationships: { admin: [Object] },
    type: 'logistics'
  }
}

```

I will continue to investigate

---

<div class="post-metadata">

**Author:** ![MarcoGarcia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/marcogarcia/32/2715_2.png) [@MarcoGarcia](https://community.forestadmin.com/u/MarcoGarcia)\
**Post date:** [June 24, 2022, 4:17pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/10 "2022-06-24T16:17:17Z")

</div>

Ok it seems that this line is useless

```javascript
  @action
  async updateShipping(record, value) {
    record.shipping = value // this line is useless
    await record.save()
  }

```

The attributes seems to be directly recovered from the template. So I need to do the same for checkboxes :

```javascript
@value={{this.currentRecord.forest-productOrder}}

```

on my

```javascript
<BetaCheckbox @value={{this.currentRecord.forest-productOrder}} @small={{true}} @disabled={{false}} @onChange={{fn this.updateRecord this.currentRecord type supplier}} />

```

It’s working but it doesn’t send what I want. Only true or false depending on the checkbox. And I need more info =\> `{ type, supplier, value }`. I also have multiple checkboxes on the same view that are linked to productOrder field. So with this @value they are connected and I don’t want that.

I need multiple checkboxes that update the same smart field with different value depending on the checkbox. Do you think there is a way to do that ? (I can’t make multiple smart field because checkboxes are made dynamically depending on the content of my record)

---

<div class="post-metadata">

**Author:** ![MarcoGarcia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/marcogarcia/32/2715_2.png) [@MarcoGarcia](https://community.forestadmin.com/u/MarcoGarcia)\
**Post date:** [June 24, 2022, 4:26pm UTC](https://community.forestadmin.com/t/saving-fields-data-in-smart-view-2/4895/11 "2022-06-24T16:26:31Z")

</div>

It was just

```javascript
  @action
  async updateRecord(record, type, supplier, value) {
    record['forest-productOrder'] = JSON.stringify({ type, supplier, value })
    await record.save()
  }

```

Thanks for the help !
