Painless scipt

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:

    "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!

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.