# Calling smart actions from smart views

**URL:** <https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535>\
**Category:** Help me!\
**Created:** [May 20, 2021, 3:03pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535 "2021-05-20T15:03:14Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![veritas1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/veritas1/32/1566_2.png) [@veritas1](https://community.forestadmin.com/u/veritas1)\
**Post date:** [May 20, 2021, 3:03pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/1 "2021-05-20T15:03:14Z")

</div>

Using forest\_liana gem in a rails 6 app we have created a smart action that excepts an array of integers as a parameter.

We need to call this from javascript code in a smart view, and the current implementation overrides the `triggerSmartAction` function, e.g:

```javascript
  @action
  triggerSmartAction(...args) {
    args[2] = [1,2,3,4];
    return triggerSmartAction(this, ...args);
  }

```

The rails controller action will then receive that data in the parameters:

`Parameters: {"data"=>{"attributes"=>{"collection_name"=>"MyCollection", "values"=>{}, "ids"=>[1,2,3,4], etc...}`

This is a hack though. I have not seen any other valid alternatives??  
Anyway, this does work, but only if the smart view is accessed directly from that collection.

If it is accessed through a parent collection, the parameters received become e.g:

`Parameters: {"data"=>{"attributes"=>{"collection_name"=>"MyCollection", "values"=>{}, "ids"=>[], "parent_collection_name"=>"MyCollectionParentCollection", "parent_collection_id"=>"1", etc...}`

And the data we need from the `ids` param is no longer present.

Is there a workaround for this?

Many thanks

## Context

Rails 6.1.3.1  
Forest liana gem 6.3.1

---

<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:** [May 20, 2021, 3:45pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/2 "2021-05-20T15:45:43Z")

</div>

Hi @veritas1,

Thank you for reaching us with your concern.

Helping you crafting a more suited solution might require a bit more of context. What kind of parameters will replace the `[1,2,3,4]` in your example? Is that a static value? If so you could just provide it to the action directly in its definition on the rails implementation of the smart action to have it anyhow.

---

<div class="post-metadata">

**Author:** ![veritas1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/veritas1/32/1566_2.png) [@veritas1](https://community.forestadmin.com/u/veritas1)\
**Post date:** [May 20, 2021, 3:58pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/3 "2021-05-20T15:58:14Z")

</div>

Hi @anon37102731

No, the values are dynamically generated within the forest admin client.

The hardcoded values in the example are just for illustration purposes.

---

<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:** [May 20, 2021, 4:02pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/4 "2021-05-20T16:02:27Z")

</div>

@veritas1, could you explain to me what kind of dynamic data it is?

So maybe I can come up with an idea on how to make it work 🙂

---

<div class="post-metadata">

**Author:** ![veritas1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/veritas1/32/1566_2.png) [@veritas1](https://community.forestadmin.com/u/veritas1)\
**Post date:** [May 20, 2021, 4:49pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/5 "2021-05-20T16:49:46Z")

</div>

It’s an array of resource IDs e.g [“246”, “534”, “102”]

---

<div class="post-metadata">

**Author:** ![anon90145840](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon90145840](https://community.forestadmin.com/u/anon90145840)\
**Post date:** [May 21, 2021, 8:39am UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/6 "2021-05-21T08:39:05Z")

</div>

Hi @veritas1,

Where do those IDs come from? Are those the IDs of the selected records on which you apply the smart action?

---

<div class="post-metadata">

**Author:** ![veritas1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/veritas1/32/1566_2.png) [@veritas1](https://community.forestadmin.com/u/veritas1)\
**Post date:** [May 21, 2021, 8:59am UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/7 "2021-05-21T08:59:17Z")

</div>

@anon90145840 They are IDs iof the records. They are not selected though. The smart view has a drag n drop ui to sort the records, creating an array of resource IDs in their new order.

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [May 24, 2021, 9:42am UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/8 "2021-05-24T09:42:41Z")

</div>

Hi @veritas1,

Can you share you’re **code that dynamically generated the values**? It would be appreciated as the answer can be there. 🙂

Thanks in advance.  
Morgan

---

<div class="post-metadata">

**Author:** ![veritas1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/veritas1/32/1566_2.png) [@veritas1](https://community.forestadmin.com/u/veritas1)\
**Post date:** [May 24, 2021, 10:36am UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/9 "2021-05-24T10:36:35Z")

</div>

Hi @morganperre

I don’t think so to be honest. Essentially, the question is this:

If I have a smart action that has fields, defined as e.g:

```
action 'My Smart Action', type: 'global', fields: [
  {
    field: 'My Field',
    type: ['String']
  }
]

```

Then how do I call this smart action (passing values for the field `My Field`) from a smart view javascript component?

I was overriding `triggerSmartAction` as I did not find any other alternative. Is there one that I have missed/maybe not documented?

Thanks

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [May 24, 2021, 11:42am UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/10 "2021-05-24T11:42:01Z")

</div>

Ok, I didn’t get your problem at first glandes.

I will let an other member of our team give you a right answer on the subject.

Regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![Guillaume\_Cisco](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaume_cisco/32/918_2.png) [@Guillaume\_Cisco](https://community.forestadmin.com/u/Guillaume_Cisco)\
**Post date:** [May 24, 2021, 1:48pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/11 "2021-05-24T13:48:41Z")

</div>

Hello @veritas1,

I know the array of resource Ids is dynamic in your case, but I’d like we test something.  
Could you try with static values, ex `args[2] = [1,2,3,4]`, save your smart view. Then refresh your browser, and execute your smart action from your smart view.  
Do you correctly see `"ids"=>[1,2,3,4]` in both case?

I’m suspecting a cache issue, but I need you to help me on this one 😉

---

<div class="post-metadata">

**Author:** ![veritas1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/veritas1/32/1566_2.png) [@veritas1](https://community.forestadmin.com/u/veritas1)\
**Post date:** [May 24, 2021, 2:18pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/12 "2021-05-24T14:18:57Z")

</div>

Hi @Guillaume_Cisco

I experience the same still.

If the smart view is accessed on the owning collection, I get `"ids"=>[1,2,3,4]`

If the smart view is accessed through a relationship, e.g a **parent** collection, I get `"ids"=>[]`

---

<div class="post-metadata">

**Author:** ![Guillaume\_Cisco](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaume_cisco/32/918_2.png) [@Guillaume\_Cisco](https://community.forestadmin.com/u/Guillaume_Cisco)\
**Post date:** [May 24, 2021, 2:28pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/13 "2021-05-24T14:28:22Z")

</div>

Thank you @veritas1,

I’m experiencing the same behaviour by setting `args[2] = [1,2,3,4]`.  
What is going on, is that you should not modify the `args[2]`, this way as for `parent_collection`, it will behave differently.  
If you keep `records` as your third parameter for `args`:

- when accessing the owning collection, you will have a list of ids “ids”=\>[1,2,3,4]
- from a relationship, with a parent collection, you will have an empty list of id, BUT a `parent_collection_id` set to the id related to the initial record. You will also have `parent_collection_name` and `parent_association_name` filled.

In definitive, you should not override the `args[2]` parameter as it is strongly binded to your real records.

---

<div class="post-metadata">

**Author:** ![veritas1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/veritas1/32/1566_2.png) [@veritas1](https://community.forestadmin.com/u/veritas1)\
**Post date:** [May 24, 2021, 2:45pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/14 "2021-05-24T14:45:01Z")

</div>

Thanks @Guillaume_Cisco

Makes sense.

But, any solution? Is there no native support within Forest Admin for calling smart actions in this way?

---

<div class="post-metadata">

**Author:** ![Guillaume\_Cisco](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaume_cisco/32/918_2.png) [@Guillaume\_Cisco](https://community.forestadmin.com/u/Guillaume_Cisco)\
**Post date:** [May 24, 2021, 4:15pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/15 "2021-05-24T16:15:10Z")

</div>

Right now, it will be impossible for you to pass record ids in the case of a parent collection as it is not yet supported.  
Only selected related records from the table view would be sent, but in the case of a smart view, you “cannot select records” as the smart view overrides the default table view.

We will see with the product team if we allow to support this case (I have a good feeling we will).  
I’m sorry to tell you there is no workaround yet.

I will keep you updated 🙂

---

<div class="post-metadata">

**Author:** ![Guillaume\_Cisco](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaume_cisco/32/918_2.png) [@Guillaume\_Cisco](https://community.forestadmin.com/u/Guillaume_Cisco)\
**Post date:** [May 26, 2021, 12:40pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/16 "2021-05-26T12:40:30Z")

</div>

Hello @veritas1,

We released a new version of our front application.  
You should now be able to override the records directly from your smart view, it will be available in related data too 🎉

Let me know if it solves your issue 😉

---

<div class="post-metadata">

**Author:** ![veritas1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/veritas1/32/1566_2.png) [@veritas1](https://community.forestadmin.com/u/veritas1)\
**Post date:** [June 1, 2021, 12:22pm UTC](https://community.forestadmin.com/t/calling-smart-actions-from-smart-views/2535/17 "2021-06-01T12:22:05Z")

</div>

Hi @Guillaume_Cisco

Works! Many thanks 👍
