# Unable Return Multiple Variables from a transform in Elasticsearch Watcher script

**URL:** https://discuss.elastic.co/t/unable-return-multiple-variables-from-a-transform-in-elasticsearch-watcher-script/319816
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting, painless
**Created:** [November 25, 2022, 12:11pm UTC](https://discuss.elastic.co/t/unable-return-multiple-variables-from-a-transform-in-elasticsearch-watcher-script/319816 "2022-11-25T12:11:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Souvik\_Das](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/souvik_das/32/107606_2.png) [@Souvik\_Das](https://discuss.elastic.co/u/Souvik_Das)
#### Post date: [November 25, 2022, 12:11pm UTC](https://discuss.elastic.co/t/unable-return-multiple-variables-from-a-transform-in-elasticsearch-watcher-script/319816/1 "2022-11-25T12:11:00Z")

</div>

Hi team,

I have written a watch script where I need to pass the total hits value from an es search query and also a variable that I have created using transform. I have referred the following link for this -- [Watcher: Multiple transforms](https://discuss.elastic.co/t/watcher-multiple-transforms/193476)

Below is the script --

```auto
{
  "trigger": {
    "schedule": {
      "interval": "2h"
    }
  },
  "input": {
    "search": {
      "request": {
        "body": {
          "size": 1000,
		  "query": {
			"bool": {
			  "must": [],
			  "filter": [
				{
				  "match_all": {}
				},
				{
				  "range": {
					"response_time": {
					  "gte": 70,
					  "lt": null
					}
				  }
				},
				{
				  "match_phrase": {
					"field": "abc"
				  }
				},
				{
					"range": {
					  "response_datetime": {
						"gte": "{{ctx.trigger.scheduled_time}}||-2h",
						"lte": "{{ctx.trigger.scheduled_time}}",
						"format": "strict_date_optional_time||epoch_millis"
					  }
					}
				}
			  ],
			  "should": [],
			  "must_not": []
			}
		  }
        },
        "indices": [
          "test"
        ]
      }
    }
  },
  "condition": {
    "script": {
      "source": "if (ctx.payload.hits.total >= params.threshold) { return true; } return false;",
      "params": {
        "threshold": 5
      }
    }
  },
    "transform": {
      "script": {
        "source": """
		def map = [:]
		map.beginTime = Instant.ofEpochMilli(ctx.trigger.scheduled_time.getMillis()).minusSeconds(3600);
		map.no_of_txns = ctx.payload.hits.total;
		return map
		"""
      }
    },
  "actions": {
    "email_admin": {
      "email": {
        "profile": "standard",
        "from": "from_email@xyz.com",
        "to": ["my_email@abcd.com"],
        "subject": "Test Alert : Incident happened between {{ctx.payload.beginTime}} and {{ctx.trigger.scheduled_time}}",
        "body": {
          "html": "total count {{ctx.payload.no_of_txns}}"
        }
      }
    }
  }
}

```

Getting the below error while I'm simulating the script.

![image](https://us1.discourse-cdn.com/elastic/original/3X/0/7/07f1b4c272eef627393aa409d6c7dfdb1f8f45de.png)

I'm new to painless. Can anyone please help me to identify what I'm doing wrong?

Regards,  
Souvik

---

<div class="post-metadata">

### Author: ![Souvik\_Das](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/souvik_das/32/107606_2.png) [@Souvik\_Das](https://discuss.elastic.co/u/Souvik_Das)
#### Post date: [November 28, 2022, 5:21am UTC](https://discuss.elastic.co/t/unable-return-multiple-variables-from-a-transform-in-elasticsearch-watcher-script/319816/2 "2022-11-28T05:21:26Z")

</div>

Hi @spinscale , Could you please help me to find out the issue?

---

<div class="post-metadata">

### Author: ![Souvik\_Das](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/souvik_das/32/107606_2.png) [@Souvik\_Das](https://discuss.elastic.co/u/Souvik_Das)
#### Post date: [November 28, 2022, 1:18pm UTC](https://discuss.elastic.co/t/unable-return-multiple-variables-from-a-transform-in-elasticsearch-watcher-script/319816/3 "2022-11-28T13:18:34Z")

</div>

Found the issue in my script. Made a silly mistake. Resolved!

Below is the updated scripts --

```auto
    "transform": {
      "script": {
        "source": """
		def map = [:];
		map.beginTime = Instant.ofEpochMilli(ctx.trigger.scheduled_time.getMillis()).minusSeconds(3600);
		map.no_of_txns = ctx.payload.hits.total;
		return map
		"""
      }
    }

```

---

<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 26, 2022, 1:19pm UTC](https://discuss.elastic.co/t/unable-return-multiple-variables-from-a-transform-in-elasticsearch-watcher-script/319816/4 "2022-12-26T13:19:00Z")

</div>

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