# Need help building a loop in RapidPro (iterate over a list and stop at the end)

**URL:** <https://forum.communityhealthtoolkit.org/t/need-help-building-a-loop-in-rapidpro-iterate-over-a-list-and-stop-at-the-end/5116>\
**Category:** Technical Support\
**Tags:** configuration\
**Created:** [July 1, 2025, 9:28pm UTC](https://forum.communityhealthtoolkit.org/t/need-help-building-a-loop-in-rapidpro-iterate-over-a-list-and-stop-at-the-end/5116 "2025-07-01T21:28:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justinho](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/justinho/32/973_2.png) [@Justinho](https://forum.communityhealthtoolkit.org/u/Justinho)\
**Post date:** [July 1, 2025, 9:28pm UTC](https://forum.communityhealthtoolkit.org/t/need-help-building-a-loop-in-rapidpro-iterate-over-a-list-and-stop-at-the-end/5116/1 "2025-07-01T21:28:36Z")

</div>

Hi everyone,

I’m working on a RapidPro flow (integrated with CHT) where I retrieve a list of children from a household using a webhook. My goal is to loop through this list (`webhook.rows`) and, for each child, send specific data (like `child_name`, `child_id`, etc.) via another webhook.

Here’s what I’ve already implemented:

- I initialize an index at `0` (`results.index`).
- I access child data using `webhook.rows[results.index]`.
- After processing one child, I increment the index with `@results.index + 1` and loop back to continue.
- I use `@((webhook.rows)[results.index]).value` to extract values from each row.

My problem is: I’m struggling to detect the end of the list properly. I can’t find a reliable way to stop the loop once all rows have been processed.

So, how can I write a condition in a `split by expression` that checks if `results.index` is beyond the size of `webhook.rows`? Is there a way to determine the list length or to safely detect that the current index is out of bounds?

Thanks in advance for any help, or if you have a working loop example to share!

---

<div class="post-metadata">

**Author:** ![binod](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/binod/32/60_2.png) [@binod](https://forum.communityhealthtoolkit.org/u/binod)\
**Post date:** [July 2, 2025, 3:25am UTC](https://forum.communityhealthtoolkit.org/t/need-help-building-a-loop-in-rapidpro-iterate-over-a-list-and-stop-at-the-end/5116/2 "2025-07-02T03:25:07Z")

</div>

Hi @Justinho,

In the `split by expression` component , you can have the expression `@results.index` check against `@(count(webhook.rows))`.

 ![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/1/1adae72df5ae0159c75a006d217d910d0f41cc2c.png)

Here is the full flow I tried with this approach:

 ![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/5/57be70c052c6ed365567963831e6a8c424bb078e.png)

This was the result of the simulator:

 ![image](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/d/d394d5de8c459e6a7ed38555155dab7039a2b439.png)

_Note:_ For testing this, I used [webhook.site](https://webhook.site/) to create a temporary webhook with this content:

```auto
{
"rows": ["apple", "ball", "cat", "doll"]
}

```

---

<div class="post-metadata">

**Author:** ![Justinho](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/justinho/32/973_2.png) [@Justinho](https://forum.communityhealthtoolkit.org/u/Justinho)\
**Post date:** [July 2, 2025, 2:53pm UTC](https://forum.communityhealthtoolkit.org/t/need-help-building-a-loop-in-rapidpro-iterate-over-a-list-and-stop-at-the-end/5116/3 "2025-07-02T14:53:50Z")

</div>

> [@binod](#):
>
> @(count(webhook.rows))

Thank you Binod for this solution, which has helped me solve this problem in my flows.

---

<div class="post-metadata">

**Author:** ![jhonnmick](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/jhonnmick/32/4040_2.png) [@jhonnmick](https://forum.communityhealthtoolkit.org/u/jhonnmick)\
**Post date:** [July 2, 2025, 5:28pm UTC](https://forum.communityhealthtoolkit.org/t/need-help-building-a-loop-in-rapidpro-iterate-over-a-list-and-stop-at-the-end/5116/4 "2025-07-02T17:28:01Z")

</div>

To loop through webhook.rows in RapidPro and stop safely at the end[,](https://calculadoradedias.com/habiles/) use @results.index \< count(webhook.rows) as your loop condition in the Split by Expression node—this ensures the loop continues only while the index is within bounds, preventing out-of-range errors and allowing you to process each item reliably.
