# Sending context variable values as a parameter to a stored script

**URL:** <https://discuss.elastic.co/t/sending-context-variable-values-as-a-parameter-to-a-stored-script/147977>\
**Category:** Elasticsearch\
**Created:** [September 10, 2018, 3:02pm UTC](https://discuss.elastic.co/t/sending-context-variable-values-as-a-parameter-to-a-stored-script/147977 "2018-09-10T15:02:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kungfu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kungfu/32/35380_2.png) [@kungfu](https://discuss.elastic.co/u/kungfu)\
**Post date:** [September 10, 2018, 3:02pm UTC](https://discuss.elastic.co/t/sending-context-variable-values-as-a-parameter-to-a-stored-script/147977/1 "2018-09-10T15:02:37Z")

</div>

I'm copying this message here since I sended it to ElasticSearch support on 9/4 but I have still no answers.

Thanks.

* * *

Hello.

I'm creating watchers on ES. Since they will post a standard structure to a webhook I've created a stored script that takes a few parameters and stores them JSON structured in a context var.

Sometimes I need to use a context value to form a parameter. Let's see a case.

This is my script:

POST \_scripts/jmdlcbtestscript001  
{  
"script": {  
"lang": "painless",  
"source": "ArrayList whbody = new ArrayList(); whbody.add(['alert\_name':params.alert\_name]); return ['cuerpo': whbody[0]]"  
}  
}

And this is the way I'm calling it:

"actions": {  
"my-logging-action": {  
"transform": {  
"script": {  
"lang": "painless",  
"source": "jmdlcbtestscript001",  
"params": {  
"alert\_name": "Alert Name",  
}  
}  
},  
"webhook" : {  
"method" : "POST",  
"url" : "[https://myurl/api/alarms/push/](https://myurl/api/alarms/push/)",  
"body" : "{{[#toJson](javascript:void(0);)}}ctx.payload.cuerpo{{/toJson}}"  
}  
}  
}

So, now I want to name my alert like this:

"alert\_name": "Limit reached - {{ctx.payload.hits.total}} hits.",

If I add this to params "{{ctx.payload.hits.total}}" is sent as string. I tried using keyword script in various ways without success.

How can I do what I need?

Thanks.  
Juan Matías

---

<div class="post-metadata">

**Author:** ![Grant\_Murphy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grant_murphy/32/35799_2.png) [@Grant\_Murphy](https://discuss.elastic.co/u/Grant_Murphy)\
**Post date:** [September 11, 2018, 11:46pm UTC](https://discuss.elastic.co/t/sending-context-variable-values-as-a-parameter-to-a-stored-script/147977/2 "2018-09-11T23:46:07Z")

</div>

Hi Juan,

See: [Watcher Hits in Script Params](https://discuss.elastic.co/t/watcher-hits-in-script-params/101533/3) for a discussion around this. Where the comment was that there is no need to pass parameters around, you have full access to the `ctx` field in a script transform anyway.

HTH!

---

<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:** [October 9, 2018, 11:46pm UTC](https://discuss.elastic.co/t/sending-context-variable-values-as-a-parameter-to-a-stored-script/147977/3 "2018-10-09T23:46:16Z")

</div>

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