# Ctx.payload object data disappearing after inclusion of script\_field in watcher api

**URL:** <https://discuss.elastic.co/t/ctx-payload-object-data-disappearing-after-inclusion-of-script-field-in-watcher-api/254185>\
**Category:** Elasticsearch\
**Created:** [November 3, 2020, 7:53pm UTC](https://discuss.elastic.co/t/ctx-payload-object-data-disappearing-after-inclusion-of-script-field-in-watcher-api/254185 "2020-11-03T19:53:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![esguy](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@esguy](https://discuss.elastic.co/u/esguy)\
**Post date:** [November 3, 2020, 7:53pm UTC](https://discuss.elastic.co/t/ctx-payload-object-data-disappearing-after-inclusion-of-script-field-in-watcher-api/254185/1 "2020-11-03T19:53:45Z")

</div>

Hello,

I'm having an issue where the data in my ctx.payload object is disappearing after I include "script\_field" in my watcher. The watcher executes perfectly fine with the desired output when the "script\_field" field is not included. Any thoughts on why this is happening?

Here is my code with the script\_field:

```
{
  "trigger": {
    "schedule": {
      "cron": "0 0/1 * * * ?"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "my_index-*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "query": {
            "bool": {
              "must": [
                {
                  "query_string": {
                    "query": "stack trace",
                    "default_operator": "AND"
                  }
                }
              ],
              "filter": {
                "range": {
                  "@timestamp": {
                    "from": "{{ctx.trigger.scheduled_time}}||-15m",
                    "to": "{{ctx.trigger.triggered_time}}"
                  }
                }
              }
            }
          },
          "script_fields": {
            "field_name": {
                "script": {
                    "lang": "painless",
                    "source": "doc['hostname.keyword'].value.substring(7,10)"
                }
            }
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gt": 0
      }
    }
  },
  "actions": {
    "email_admin": {
      "throttle_period_in_millis": 900000,
      "email": {
        "profile": "standard",
        "to": [
          "first last <first.last@domain.com>"
        ],
        "subject": "Stack Trace Error(s): {{#ctx.payload.hits.hits}} {{fields.field_name.0}} {{/ctx.payload.hits.hits}}",
        "body": {
          "html": """ <html>
          
          Hello,
          <br />
          <br />
          There are stack trace errors in the following hosts:
          <br />
          <br />
          {{#ctx.payload.hits.hits}} 
          <strong> Host: </strong> {{_source.hostname}} <br />
          <hr /> <br />
          {{/ctx.payload.hits.hits}}
          
          </html>
          """
        }
      }
    }
  }
}

```

The html just outputs "Host: " however when the script\_field is removed it outputs the hostname properly. Any help would be greatly appreciated. Thanks in advance.

---

<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:** [December 1, 2020, 7:54pm UTC](https://discuss.elastic.co/t/ctx-payload-object-data-disappearing-after-inclusion-of-script-field-in-watcher-api/254185/2 "2020-12-01T19:54:04Z")

</div>

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