# Got internal error in transform script

**URL:** <https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843>\
**Category:** Elasticsearch\
**Created:** [October 13, 2017, 7:25am UTC](https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843 "2017-10-13T07:25:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RomainXie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/romainxie/32/22427_2.png) [@RomainXie](https://discuss.elastic.co/u/RomainXie)\
**Post date:** [October 13, 2017, 7:25am UTC](https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843/1 "2017-10-13T07:25:20Z")

</div>

Hi, all

I am a newbie for painless.  
I wrote a watcher, snippet of the transform bellow:

```
"transform":{
    "script": "string hosts=''; for (item in ctx.payload.aggregations.whichHost.buckets.entrySet()) {hosts = hosts + ', ' + item.getKey();} return ['hosts' : ''];"
},

```

After run the simulate, I got a error:

> Watcher: An internal server error occurred

I want to foreach the buckets, and then concatenate the key of the item.

How? 😭

---

<div class="post-metadata">

**Author:** ![RomainXie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/romainxie/32/22427_2.png) [@RomainXie](https://discuss.elastic.co/u/RomainXie)\
**Post date:** [October 13, 2017, 7:46am UTC](https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843/2 "2017-10-13T07:46:20Z")

</div>

I resolved this problem by followed script:

> String host=''; for (int i = 0; i \< ctx.payload.aggregations.whichHost.buckets.length; i++) {host = host + ', ' + ctx.payload.aggregations.whichHost.buckets[i].key;} return ['error\_count' : ctx.payload.aggregations.whichHost.buckets.length, 'hosts' : host]

But is there another way more pretty?

---

<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:** [October 13, 2017, 7:56am UTC](https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843/3 "2017-10-13T07:56:56Z")

</div>

hey,

if this a transformation issue, or do you just want to display the hosts in your action. If so, you could just use the `{{#join}}` feature of mustache to join an array of data together.

See [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-template.html#\_concatenating\_array\_of\_values](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-template.html#_concatenating_array_of_values)

I have created an example if you dont want to use mustache, but stay with lambdas, check out execute watch

```auto
POST _xpack/watcher/watch/_execute
{
  "watch" : {
    "trigger" : {
      "schedule" : {
        "interval" : "10m"
      }
    },
    "input" : { "simple" : {
      "foo" : [
        { "field" : "value1" },
        { "field" : "value2" },
        { "field" : "value3" },
        { "field" : "value4" },
        { "field" : "value5" }
        ]
    }},
    "actions" : {
      "logging" : {
        "transform" : {
          "script":" return ['foo': ctx.payload.foo.stream().map(item -> item.field).collect(Collectors.toList()).join(' ,')]"
        },
        "logging" : {
          "text" : "{{ctx.payload}}"
        }
      }
    }
  }
}

```

--Alex

---

<div class="post-metadata">

**Author:** ![RomainXie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/romainxie/32/22427_2.png) [@RomainXie](https://discuss.elastic.co/u/RomainXie)\
**Post date:** [October 13, 2017, 8:39am UTC](https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843/4 "2017-10-13T08:39:42Z")

</div>

Yeah, thank you very much.  
The code is perfect 😁

---

<div class="post-metadata">

**Author:** ![RomainXie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/romainxie/32/22427_2.png) [@RomainXie](https://discuss.elastic.co/u/RomainXie)\
**Post date:** [October 13, 2017, 5:30pm UTC](https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843/5 "2017-10-13T17:30:58Z")

</div>

Hi,

I try to use the {{#join}} in a template in the action section, but I got a syntax error. The snippet of the code below:

```
"actions": {
  "notify_slack": {
      "throttle_period_in_millis": 30000,
      "template": {
        "id": "tpl_slack_notify",
        "params": {
          "hosts": "{{ctx.payload.aggregations.whichHost.bucket"
        }
      }
  }
}

```

And I saw the manual for action in watcher, there is no anyone section talk about which use the template in the action snippet.

Could I use the template in the action section? Is there a sample for it?

Thank you

---

<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:** [October 16, 2017, 9:14am UTC](https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843/6 "2017-10-16T09:14:20Z")

</div>

I do not see any usage of join here? Please also paste the full watch for easier understanding.

Thank you so much!

--Alex

---

<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:** [November 13, 2017, 9:14am UTC](https://discuss.elastic.co/t/got-internal-error-in-transform-script/103843/7 "2017-11-13T09:14:36Z")

</div>

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