# Dynamically generate watch actions using Painless to support multiple actions?

**URL:** https://discuss.elastic.co/t/dynamically-generate-watch-actions-using-painless-to-support-multiple-actions/193527
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [August 2, 2019, 12:02pm UTC](https://discuss.elastic.co/t/dynamically-generate-watch-actions-using-painless-to-support-multiple-actions/193527 "2019-08-02T12:02:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ypid-geberit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ypid-geberit/32/35149_2.png) [@ypid-geberit](https://discuss.elastic.co/u/ypid-geberit)
#### Post date: [August 2, 2019, 12:02pm UTC](https://discuss.elastic.co/t/dynamically-generate-watch-actions-using-painless-to-support-multiple-actions/193527/1 "2019-08-02T12:02:15Z")

</div>

Yet another rehash of [Multiple actions in the watcher yet?](https://discuss.elastic.co/t/multiple-actions-in-the-watcher-yet/136925), I am also aware of the workaround with Logstash, however, I would be interested in a solution for this limitation and would like to propose the following:

How about the `ctx.payload` or a new attribute like `ctx.actions` would just be dynamically interpreted as the watch action definition when no actions are statically defined or even in addition? This would allow to generate the actions dynamically using Painless. From the implementation side, it should be simple, but it might also require changes to Kibana to properly show the watch status. The downside is of course that because of the dynamic nature of actions here, transformation issues will result in potentially no action being taken at all. This can be mitigated by tested the watches properly with integration testing scripts provided [https://github.com/elastic/examples/tree/master/Alerting/Sample%20Watches](https://github.com/elastic/examples/tree/master/Alerting/Sample%20Watches).

Example in [YAML](https://github.com/elastic/examples/pull/239):

```auto
metadata:
  api_url: 'https://api.example.net'

transform:
  script:
    inline: |-
      ctx.actions = [
        'my_webhook1': [
          'webhook': [
            'method': 'POST',
            'url': ctx.metadata.api_url,
            'body': '{"something_for_first_doc":23}'
          ]
        ],
        'my_webhook2': [
          'webhook': [
            'method': 'POST',
            'url': ctx.metadata.api_url,
            'body': '{"something_for_second_doc":23}'
          ]
        ]
      ];
      return true;

```

In a practical use case, `ctx.actions` would be generated using a for-each loop.

Feedback? How can we proceed with this?

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [August 2, 2019, 2:15pm UTC](https://discuss.elastic.co/t/dynamically-generate-watch-actions-using-painless-to-support-multiple-actions/193527/2 "2019-08-02T14:15:32Z")

</div>

Hey,

have you seen [https://www.elastic.co/guide/en/elastic-stack-overview/7.3/action-foreach.html](https://www.elastic.co/guide/en/elastic-stack-overview/7.3/action-foreach.html) ?

--Alex

---

<div class="post-metadata">

### Author: ![ypid-geberit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ypid-geberit/32/35149_2.png) [@ypid-geberit](https://discuss.elastic.co/u/ypid-geberit)
#### Post date: [August 5, 2019, 9:12am UTC](https://discuss.elastic.co/t/dynamically-generate-watch-actions-using-painless-to-support-multiple-actions/193527/3 "2019-08-05T09:12:46Z")

</div>

Hi Alex

Thanks very much for the hint. I must have missed that. This is the proper solution I was looking for 😉

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 2, 2019, 9:12am UTC](https://discuss.elastic.co/t/dynamically-generate-watch-actions-using-painless-to-support-multiple-actions/193527/4 "2019-09-02T09:12:52Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
