# Null reference check in watcher rule using painless script

**URL:** <https://discuss.elastic.co/t/null-reference-check-in-watcher-rule-using-painless-script/205360>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [October 26, 2019, 3:41pm UTC](https://discuss.elastic.co/t/null-reference-check-in-watcher-rule-using-painless-script/205360 "2019-10-26T15:41:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manikandanid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manikandanid/32/91013_2.png) [@manikandanid](https://discuss.elastic.co/u/manikandanid)\
**Post date:** [October 26, 2019, 3:41pm UTC](https://discuss.elastic.co/t/null-reference-check-in-watcher-rule-using-painless-script/205360/1 "2019-10-26T15:41:17Z")

</div>

In my watcher rule, i have implemented a looping for the items from the result as  
"body": "{"Type":"MemoryUsage","MidThreshold":90,"MaxThreshold":98,"ServerDetails":[{{[#ctx](javascript:void(0);).payload.aggregations.allservers.buckets}}{"ServerName":"{{key}}","Datatype":"{{latest.hits.hits.0.\_source.DataType}}","NodeCategory":"{{latest.hits.hits.0.\_source.node\_category}}","NodeType":"{{latest.hits.hits.0.\_source.node\_type}}","NodeLocation":"{{latest.hits.hits.0.\_source.node\_location}}","NodeIsActive":"{{latest.hits.hits.0.\_source.node\_isactive}}","NodePageRange":"{{latest.hits.hits.0.\_source.node\_pagerange}}","Value":{{latest.hits.hits.0.\_source.PercentUsedMemory}},"OtherInfo":"{{latest.hits.hits.0.\_source.Drive}}"},{{/ctx.payload.aggregations.allservers.buckets}}]}",

Few of my documents will not have the property {{latest.hits.hits.0.\_source.node\_isactive}} and for those documents currently my json property contains empty value (NodeIsActive :""). What i need is a conditional based property addition to json. Only if the document contains the property latest.hits.hits.0.\_source.node\_isactive, i should assign it to JSON property else the property NodeIsActive should not be available in the json.

---

<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:** [October 31, 2019, 1:18pm UTC](https://discuss.elastic.co/t/null-reference-check-in-watcher-rule-using-painless-script/205360/2 "2019-10-31T13:18:11Z")

</div>

you would need to execute a `transform` as part of that action, that checks for the both different isActive fields and unifies them into one field, as mustache as a templating language does not allow if-else constructs...

---

<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:** [November 28, 2019, 1:18pm UTC](https://discuss.elastic.co/t/null-reference-check-in-watcher-rule-using-painless-script/205360/3 "2019-11-28T13:18:21Z")

</div>

Closed on November 28, 2019, 1:18pm UTC

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