# How to add a reference in an enum field in SmartAction Forms?

**URL:** <https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422>\
**Category:** Help me!\
**Created:** [August 29, 2024, 7:44am UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422 "2024-08-29T07:44:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marksrr](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@marksrr](https://community.forestadmin.com/u/marksrr)\
**Post date:** [August 29, 2024, 7:44am UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422/1 "2024-08-29T07:44:08Z")

</div>

I am trying to add a dropdown to the smart action form that shows a list of names of any model, in this example rooms. In the handler method I then want to assign the selected room to a house. I only find examples where the “Room” is mapped onto an enum field, using the rooms name. How can I forward the room ID instead to the handler method, but display the room name in the dropdown?

For example, I might have a house with 3 bedrooms, so every room name is “Bedroom”. Since “name” is not a unique identifier, it is not a reliable way to look up the model in the database.

I would like to do something like mapping the rooms to a label/value pair. The given example only shows {object Object} in the dropdown for rooms.

```javascript
collection('house', {
  actions: [
    {
      name: 'Add a room',
      type: 'single',
      endpoint: '/forest/actions/add-room',
      fields: [
        {
          field: 'room',
          type: 'Enum',
          enums: [],
          isRequired: true
        },
      ],
      hooks: {
        load: async ({ fields, request }) => {
          const rooms = await models.room.findAll({ attributes: ['id', 'name'] })
          const roomField = fields.find(field => field.field === 'room')
          roomField.enums = rooms.map(r => ({
            value: r.id.toString(), 
            label: r.name 
          }))
          return fields
        }
      }
    }
})

async function addRoomHandler (req, res) {
  const { query, user } = req
  const recordsGetter = new RecordsGetter(house, user, query)
  const houseIds = await recordsGetter.getIdsFromRequest(req)
  // how to get selected room id here?
}

```

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 29, 2024, 8:30am UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422/2 "2024-08-29T08:30:06Z")

</div>

Hi @marksrr,

It’s pretty simple to do it on our latest agent ([https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions/forms-static#references-to-records](https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions/forms-static#references-to-records)).  
But you are clearly not on it. Which version are you using ?

Normally you can do something like on forest-express-sequelize:

```javascript
        {
          field: 'room',
          type: 'String', // Or number if your id is a Number
          // Notice this line, it will tell us that this is referencing actually another collection
          // It should be `name_of_my_collection.primary_key_name`
          reference: 'room.id',
          isRequired: true
        },
```

---

<div class="post-metadata">

**Author:** ![marksrr](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@marksrr](https://community.forestadmin.com/u/marksrr)\
**Post date:** [August 29, 2024, 8:39am UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422/3 "2024-08-29T08:39:43Z")

</div>

Hey Vince,

thanks for your response.

unfortunately this project is running on forest-express-sequelize. Do you see any chance to implement it there?

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 29, 2024, 9:14am UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422/4 "2024-08-29T09:14:52Z")

</div>

The code I shared is for foret-express-sequelie. Sorry it was not explicit enough 😅

---

<div class="post-metadata">

**Author:** ![marksrr](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@marksrr](https://community.forestadmin.com/u/marksrr)\
**Post date:** [August 29, 2024, 9:30am UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422/5 "2024-08-29T09:30:34Z")

</div>

😉

But that creates the normal searchable relation field, which I find a bit inconvenient if we only have like 5 instances for example. Changing the type to Enum does not work in that case. Can I modify the field from typeahead to dropdown in the smart action form as well somehow?

And maybe you can quickly point me to another thing: When I add a “search” key to a field to pre-filter the resources, I get “The search on the referenced collection is not authorized.”. From what I found I have to allow it somewhere in the settings, but I could not find the right place.

Your help is highly appreciated thank you

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 29, 2024, 9:35am UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422/6 "2024-08-29T09:35:50Z")

</div>

Could you try to do the following, I’m not sure it will work. It’s well supported on the new agent but I’m not sure about forest-express-sequelize.

```javascript
        {
          field: 'room',
          type: 'String', 
          reference: 'room.id',
          widget: 'dropdown', // Widget here might modify what is displayed
          isRequired: true
        },
```

---

<div class="post-metadata">

**Author:** ![marksrr](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@marksrr](https://community.forestadmin.com/u/marksrr)\
**Post date:** [August 29, 2024, 9:38am UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422/7 "2024-08-29T09:38:44Z")

</div>

That did the trick, thank you Vince!

> [@marksrr](#):
>
> And maybe you can quickly point me to another thing: When I add a “search” key to a field to pre-filter the resources, I get “The search on the referenced collection is not authorized.”. From what I found I have to allow it somewhere in the settings, but I could not find the right place.

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [August 29, 2024, 12:21pm UTC](https://community.forestadmin.com/t/how-to-add-a-reference-in-an-enum-field-in-smartaction-forms/7422/8 "2024-08-29T12:21:21Z")

</div>

You can do that by accessing your roles settings.

- Go to your project settings by clicking on the top left corner on the name of your project.
- Then click on Roles
- Click on the role you want to edit
- In the collections permissions, you need to have the `Read (list)` permissions checked for the collections you are searching on
