# Looping through items in a collection of records - Workflows

**URL:** <https://community.dynatrace.com/t/looping-through-items-in-a-collection-of-records-workflows/46370>\
**Category:** Automations\
**Tags:** integrations, workflows, automations\
**Created:** [June 28, 2024, 12:14pm UTC](https://community.dynatrace.com/t/looping-through-items-in-a-collection-of-records-workflows/46370 "2024-06-28T12:14:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![badgerfifteen](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@badgerfifteen](https://community.dynatrace.com/u/badgerfifteen)\
**Post date:** [June 28, 2024, 12:14pm UTC](https://community.dynatrace.com/t/looping-through-items-in-a-collection-of-records-workflows/46370/1 "2024-06-28T12:14:00Z")

</div>

What I need is to loop through all of the records from a DQL output and have it formatted in a human-readable format.

Here is my DQL query

```markup
timeseries percent = avg(dt.host.disk.used.percent), by: { dt.entity.host, dt.entity.disk }, filter: { in(dt.entity.host, classicEntitySelector("type(host),tag(\"Business Unit:FINANCE\")")) }
| fields util = abs(arrayAvg(percent)), entityName(dt.entity.host)
| fieldsRename dt.entity.host.name, Host

```

…which if I use get\_disk\_stats.records, equivalent to {{ result(“get\_disk\_stats”)[“records”] }}, I get the following output:

```markup
[
  {
    "Host": "WIN11023.corporate.org",
    "util": 73.14564947430772
  },
  {
    "Host": "WIN19876.corporate.org",
    "util": 49.68959838117046
  },
  {
    "Host": "WIN11234.corporate.org",
    "util": 37.80735823445836
  },
  {
    "Host": "WIN15678.corporate.org",
    "util": 69.33072939353247
  },
  {
    "Host": "WIN19987.corporate.org",
    "util": 37.659657944489574
  },
  {
    "Host": "WIN12045.corporate.org",
    "util": 42.12913605995685
  },
  {
    "Host": "WIN13456.corporate.org",
    "util": 0.5479891637632675
  }
]

```

How do I code the for loop within the input itself or in the ‘Options’ menu?

---

<div class="post-metadata">

**Author:** ![ChristopherHejl](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/christopherhejl/32/20735_2.png) [@ChristopherHejl](https://community.dynatrace.com/u/ChristopherHejl)\
**Post date:** [June 28, 2024, 12:39pm UTC](https://community.dynatrace.com/t/looping-through-items-in-a-collection-of-records-workflows/46370/2 "2024-06-28T12:39:12Z")

</div>

You can use a for loop in any input (with the exception of javascript task, there you would natively loop) in order to generate the message. Here would be an example with the data you provided:

```python
Here is the email.

You can use for expressions as well as if/else or any combination.

Host utilization summary:
{% for record in result("get_disk_stats")["records"] %}
	- {{ record["Host"] }}: {{ record["util"] }}
{% endfor %}

You can also use other expressions at any place.
Eg scanned bytes: {{ result("get_disk_stats")["metadata"]["grail"]["scannedBytes"] }}

```

 ![ChristopherHejl_1-1719578336007.png](https://global.discourse-cdn.com/dynatrace/original/3X/e/8/e807d3387a2d935fd8dbbc2e3d914a45c5e32e42.png)

---

<div class="post-metadata">

**Author:** ![badgerfifteen](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@badgerfifteen](https://community.dynatrace.com/u/badgerfifteen)\
**Post date:** [June 28, 2024, 1:38pm UTC](https://community.dynatrace.com/t/looping-through-items-in-a-collection-of-records-workflows/46370/3 "2024-06-28T13:38:52Z")

</div>

absolute genius, thank you so much! Have a good weekend! 🙂
