# Iterat list on watcher elasticsearch

**URL:** <https://discuss.elastic.co/t/iterat-list-on-watcher-elasticsearch/298100>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [February 24, 2022, 12:23am UTC](https://discuss.elastic.co/t/iterat-list-on-watcher-elasticsearch/298100 "2022-02-24T00:23:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cheroufa](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Cheroufa](https://discuss.elastic.co/u/Cheroufa)\
**Post date:** [February 24, 2022, 12:23am UTC](https://discuss.elastic.co/t/iterat-list-on-watcher-elasticsearch/298100/1 "2022-02-24T00:23:16Z")

</div>

hi guys

i want to iterat list of indices to add them to e-mail alert and to send for every index one -e-mail alert.

action :

"send\_email": {  
"foreach" : "ctx.payload.list",  
"email": {  
"profile": "standard",  
"priority": "high",  
"to": [  
"xxxxxxxxxxxxxx@xxxxxxxxxxx"  
],  
"subject": "[Watcher Notification]",  
"body": {  
"text": "No data sent to index : {{ctx.payload.list}} last 24h"  
}  
}  
}  
},

the watcher send email for every index but in the body the variable : {{ctx.payload.list}} is empty  
my question is how to retrieve every element separatly from the list.

thanks for help

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 24, 2022, 8:53am UTC](https://discuss.elastic.co/t/iterat-list-on-watcher-elasticsearch/298100/2 "2022-02-24T08:53:02Z")

</div>

when you are using `foreach`, then the payload will change to contain the element of the current list iteration. See the example here using `._id` [Running an action for each element in an array | Elasticsearch Guide [8.0] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.0/action-foreach.html)

---

<div class="post-metadata">

**Author:** ![Cheroufa](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Cheroufa](https://discuss.elastic.co/u/Cheroufa)\
**Post date:** [March 1, 2022, 2:07pm UTC](https://discuss.elastic.co/t/iterat-list-on-watcher-elasticsearch/298100/3 "2022-03-01T14:07:43Z")

</div>

Thanks for the response,

i found a soultion : i delete the foreach and i iterate the list in the email body like that :

{{#ctx.payload.list}} {{key}} {{/ctx.payload.list}}

i hope that my response help someone.

---

<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:** [March 29, 2022, 2:08pm UTC](https://discuss.elastic.co/t/iterat-list-on-watcher-elasticsearch/298100/4 "2022-03-29T14:08:12Z")

</div>

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