# Declare a field variable for use in a for cycle

**URL:** <https://discuss.elastic.co/t/declare-a-field-variable-for-use-in-a-for-cycle/253452>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-monitoring, elastic-stack-alerting\
**Created:** [October 27, 2020, 3:10pm UTC](https://discuss.elastic.co/t/declare-a-field-variable-for-use-in-a-for-cycle/253452 "2020-10-27T15:10:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_David\_Jaramillo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/juan_david_jaramillo/32/76831_2.png) [@Juan\_David\_Jaramillo](https://discuss.elastic.co/u/Juan_David_Jaramillo)\
**Post date:** [October 27, 2020, 3:10pm UTC](https://discuss.elastic.co/t/declare-a-field-variable-for-use-in-a-for-cycle/253452/1 "2020-10-27T15:10:15Z")

</div>

hi, guys, i need your help with this code, it's well?

```auto
{
  "trigger": {
    "schedule": {
      "interval": "24h"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "agenteallot*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "{{ctx.trigger.scheduled_time}}||-5m",
                      "lte": "{{ctx.trigger.scheduled_time}}",
                      "format": "strict_date_optional_time||basic_time_no_millis"
                    }
                  }
                },
                {
                  "term": {
                    "monitor.name.keyword": "01-1-CAV30328203-DATOS-PPAL-2.7G-99.98"
                  }
                }
              ]
            }
          },
          "aggs": {
            "metricAgg": {
              "max": {
                "field": "summary.down"
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": "int a = ctx.payload.metricAgg.value; for (int i = a; i = 1; i==a) { if (ctx.payload.metricAgg.value = params.threshold) { return false; } } return true;",
      "lang": "painless",
      "params": {
        "threshold": 1
      }
    }
  },
  "actions": {
    "email_1": {
      "email": {
        "profile": "standard",
        "attach_data": {
          "format": "yaml"
        },
        "to": [
          "victor.vera@megadvantage.com",
          "juan.jaramillo@megadvantage.com",
          "juancho.jaramillo16@gmail.com"
        ],
        "subject": "{{ctx.metadata.name}} ",
        "body": {
          "text": """Buen dia, Adjunto registro del reporte.

 ⚠️ALERTA NODO!!! El siguiente enlace ha restablecido la conexión. 

 Estado: UP🟢 

 Nombre del Enlace: 01-1-CAV30328203-DATOS-PPAL-2.7G-99.98

 🕧Hora: {{ctx.payload.time_triggered}} {{ctx.result.execution_time}}

 Área: Networking 
 
🔔Mensaje Alerta: El enlace ha vuelto a responder. Responder correo a telecomunicaciones@udistrital.edu.co 

 Ingrese aquí para más información: https://72dfe17217744236af40cc31b704a664.us-central1.gcp.cloud.es.io:9243/s/distrital/app/uptime#"""
        }
      }
    }
  },
  "transform": {
    "script": {
      "source": "return ['time_triggered': Instant.ofEpochMilli(ctx.trigger.triggered_time.getMillis()).atZone(ZoneId.of('America/Bogota')).format(DateTimeFormatter.ofPattern('YYYY-MM-dd HH:mm:ss'))];",
      "lang": "painless"
    }
  }
}

```

**I need a to be equal to "ctx.payload.metricAgg.value" and put it in the for cycle, but it doesn't let me declare the variable as such**

 ![Screenshot 2020-10-27 100718](https://us1.discourse-cdn.com/elastic/original/3X/7/1/715759228725275f8f4d0768fee6d812d69fe51b.png)

---

<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 24, 2020, 3:10pm UTC](https://discuss.elastic.co/t/declare-a-field-variable-for-use-in-a-for-cycle/253452/2 "2020-11-24T15:10:19Z")

</div>

Closed on November 24, 2020, 3:10pm UTC

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