# Painless scipt

**URL:** <https://discuss.elastic.co/t/painless-scipt/307524>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [June 17, 2022, 3:07pm UTC](https://discuss.elastic.co/t/painless-scipt/307524 "2022-06-17T15:07:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soren\_vdc](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Soren\_vdc](https://discuss.elastic.co/u/Soren_vdc)\
**Post date:** [June 17, 2022, 3:07pm UTC](https://discuss.elastic.co/t/painless-scipt/307524/1 "2022-06-17T15:07:48Z")

</div>

Hi,

I have query with aggregation and as result I receive the buckets with the "key" and "doc\_count".  
I want to send mail for each bucket that is find in the output.  
I have this:

```auto
    "transform": {
      "script": {
        "source": """
      HashMap result = new HashMap();
      ArrayList logging_action_tenant = new ArrayList();
      ArrayList logging_action_number = new ArrayList();
		  ArrayList logging_doc_found = ctx.payload.aggregations.0.buckets;
		  for (int i = 0; i < logging_doc_found.length; i++) {
		    if (logging_doc_found[i].doc_count > 0) {
		    result.put(logging_doc_found[i].key, logging_doc_found[i].doc_count );
		    }
		  }
		  return (result);

		        """,
        "lang": "painless"
      }
},		  
   "actions": {
      "POSTFIX-Send-mail" : {
        "foreach" : "ctx.payload._value",
        "max_iterations" : 20,
        "email": {
        "profile": "standard",
        "to": [
          "test"
        ],
        "subject": " {{ctx.payload}} ",
        "body": {
          "text": " {{ctx.payload._value}} "
        }
      }

```

But receive this error:  
"error" : {  
"root\_cause" : [  
{  
"type" : "exception",  
"reason" : "specified foreach object was null: [ctx.payload.\_val]"  
}  
],  
"type" : "exception",  
"reason" : "specified foreach object was null: [ctx.payload.\_val]"  
}  
}

Any idea of possible solution?  
thx!

---

<div class="post-metadata">

**Author:** ![datadoge](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@datadoge](https://discuss.elastic.co/u/datadoge)\
**Post date:** [June 21, 2022, 11:19pm UTC](https://discuss.elastic.co/t/painless-scipt/307524/2 "2022-06-21T23:19:10Z")

</div>

I'm sorry that 4 days have gone by and no one has even bothered to answer your question. Don't worry, this is standard practice.

If I had to guess, one of your records has a null value with the key ctx.payload.\_val. Somewhere along the query, it probably hit the null value and returned that.

That is my best guess, hopefully someone else can help further.

---

<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:** [July 19, 2022, 11:19pm UTC](https://discuss.elastic.co/t/painless-scipt/307524/3 "2022-07-19T23:19:28Z")

</div>

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