# Smart Action form field autofill from collection

**URL:** <https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520>\
**Category:** Help me!\
**Tags:** smart-action\
**Created:** [June 24, 2020, 2:50pm UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520 "2020-06-24T14:50:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergior](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sergior/32/73_2.png) [@sergior](https://community.forestadmin.com/u/sergior)\
**Post date:** [June 24, 2020, 2:50pm UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/1 "2020-06-24T14:50:51Z")

</div>

## Expected behavior

When creating a new action form with some fields that require be filled in, is it currently possible to show any sort of suggested values read from a collection? For example, if the form has a field “product\_id”, I would like to see the suggestions for all the possible products and select one of them to submit the form action.

## Context

- Package Version: 5.1.0
- Rails Version: 6.0.3.2
- Database Dialect: Postgresql

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [June 24, 2020, 3:32pm UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/2 "2020-06-24T15:32:12Z")

</div>

Hi @sergior,

I think you should be able to achieve this using the `fields` options of the smart actions definition, described [here](https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions#available-smart-action-options). You should then be able to display a list of product to choose from by specifying the field you want with [this syntax](https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions/use-a-smart-action-form#handling-input-values).

Hope that helps !

---

<div class="post-metadata">

**Author:** ![sergior](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sergior/32/73_2.png) [@sergior](https://community.forestadmin.com/u/sergior)\
**Post date:** [June 24, 2020, 6:00pm UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/3 "2020-06-24T18:00:39Z")

</div>

Hi @anon39848301, thanks for the prompt reply.

I had a look at [handling-input-values](https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions/use-a-smart-action-form#handling-input-values) and I’ve tried to define it as `product.id` as suggested by the documentation. Still when I look at the interface I see a gray field (not editable) with the following message underneath it: “The search on the referenced collection is not authorized.”

Also, I don’t want all the products, I need to scope them with something like `Product.available`. Is this possible?

---

<div class="post-metadata">

**Author:** ![sergior](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sergior/32/73_2.png) [@sergior](https://community.forestadmin.com/u/sergior)\
**Post date:** [June 24, 2020, 6:45pm UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/4 "2020-06-24T18:45:25Z")

</div>

After a bit of debugging I was able to determine why it was not allowing me to edit it: I had to make sure that the Product settings in ForestAdmin had the Reference field configured. I’m now able to see a text field that I can search for a specific Product.

I still have two issues with it though:

- The first one was the one I mentioned above, which is that I don’t want all the Products, but just a subset of it based on a filter/scope
- I’ve tried to configure the default values for the form action following these [docs](https://docs.forestadmin.com/documentation/v/inapp/reference-guide/actions/create-and-manage-smart-actions#prefill-a-form-with-default-values) but with the reference field I’m not able to pre-filled with a default value.

---

<div class="post-metadata">

**Author:** ![anon37102731](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@anon37102731](https://community.forestadmin.com/u/anon37102731)\
**Post date:** [June 25, 2020, 8:26am UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/5 "2020-06-25T08:26:15Z")

</div>

Hi @sergior,

Could I take a look at your smart action definition?

That might be of some help to understand your struggle and try to find a workaround.

---

<div class="post-metadata">

**Author:** ![sergior](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sergior/32/73_2.png) [@sergior](https://community.forestadmin.com/u/sergior)\
**Post date:** [June 25, 2020, 10:33am UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/6 "2020-06-25T10:33:38Z")

</div>

```ruby
# collection

module ForestLiana
  module Collections
    class Item
      include ForestLiana::Collection

      collection :Item
 
      action(
        'Clone item',
        type: 'single',
        endpoint: '/forest/items/clone',
        fields: [
          {
            field: 'product_id',
            type: 'String',
            reference: 'Product.available.id', # <<< the `available scope seems to be ignore all together
            description: 
            is_required: true
          }
        ]
      )
    end
  end
end

```

```ruby
   # controller

    class ItemsController < ForestLiana::ApplicationController
      def clone_values
        context = get_smart_action_context
        item = Item.find(context[:id])

        preset_values = {
          product_id: item.product.id,
        }
        render serializer: nil, json: preset_values, status: :ok
      end
    end

```

```ruby
 # routes

 forest_items_clone_values POST /forest/items/clone/values(.:format)
 forest_items_clone POST /forest/items/clone(.:format)

```

- If it helps, I’ve managed to pre-fill the values when I’m not using the `reference`.
- The `Product.available.id` seems to ignore the `available` scope and just fetch all the Products when I’m typing in the field. Is there any way I can use this scope to limit the results I get?

---

<div class="post-metadata">

**Author:** ![anon37102731](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@anon37102731](https://community.forestadmin.com/u/anon37102731)\
**Post date:** [June 25, 2020, 1:57pm UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/7 "2020-06-25T13:57:00Z")

</div>

As for your first issue, ForestAdmin does not support default values for reference fields in their smart actions forms. If that’s really something you need, I can make an entry in our board for future priorization.

Same goes for filtered references. The reference field only state the target collection’s primary key, no scopes are taken into account in the process. I can also make an entry for that.

However, if you really need to scope this reference field select, I may have two workarounds (both having drawbacks):

- The first one consist of making the comparison after the validation on the `forest_items_clone` function. That way you could send back a message stating that this entry isn’t a valid one. But as stated the drawback here is that you check it after pressing the `Clone item` button, which isn’t a great flow.
- The second one would be to define your own [smart collection](https://docs.forestadmin.com/documentation/v/inapp/reference-guide/collections/create-a-smart-collection) which would be a scoped copy of the targeted table (by filtering directly in the get). That way, making the reference points to this newly created smart collection, you would ensure a scoped list.

However, let’s be transparent, this last workaround will not be easy to implement and probably hard to maintain too.

Anyway, I hope my answer will help you!

---

<div class="post-metadata">

**Author:** ![arnaud](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud/32/32_2.png) [@arnaud](https://community.forestadmin.com/u/arnaud)\
**Post date:** [June 30, 2020, 3:07pm UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/8 "2020-06-30T15:07:42Z")

</div>

> [@anon37102731](#):
>
> As for your first issue, ForestAdmin does not support default values for reference fields in their smart actions forms.

I confirm 😔

About the products subset, you might be able to override the products retrieval controller (that, by default, searches in all products) to search for available products only.  
You can use `Referer` request header to distinguish a search from the main products list and a search from your Smart Action form.

More info about routes override available here:  
[https://docs.forestadmin.com/documentation/v/inapp/reference-guide/routes](https://docs.forestadmin.com/documentation/v/inapp/reference-guide/routes)

---

<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:** [June 19, 2023, 11:19am UTC](https://community.forestadmin.com/t/smart-action-form-field-autofill-from-collection/520/9 "2023-06-19T11:19:52Z")

</div>

Hey @sergior,

I didn’t look into details, but aren’t you looking for [Smart action hooks](https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions/use-a-smart-action-form#making-a-form-dynamic-with-hooks) ?
