Looping Result Data from CHT in Rapid Pro

Hey there folks,

I’m using Rapid Pro and I am facing a blocker on how to loop over results to perform an action for each of the results.

The options I see in Rapid pro are mainly if..else

In my situation, when a supervisor submits a form, I want to send a message to all CHWs under the Supervisor’s area and notify them about the upcoming event.

I’m using Rapid Pro to get the phone numbers of the CHWs. I’ve been able to do that and the result is like

{"total_rows":11,"offset":9,"rows":[ 
{"id":"6c55fa2c-fbf6-4e1f-b2ea-cf9ae52046cb","key":"fd39b216-a15a-4b06-a631-43fe504d6c91","value":"0781819189"}, 
{"id":"cf3d0f4f-ed57-43ff-bdb9-d88522f35f20","key":"fd39b216-a15a-4b06-a631-43fe504d6c91","value":"0718345346"} 
]}

The challenge I now face is how to send a message to each number in the result? My plan is to send a report back to CHT via the API for each of the number / contact.

But I haven’t found any resource that describes looping the results in Rapid Pro.

Might anyone in the community have come across a similar use case that could unblock me.

This is how I solved my issue to loop through the results.

  1. Call webhook get back an array

[
  {"id":"6c55fa2c-fbf6-4e1f-b2ea-cf9ae52046cb","value":"0781819189"},
  {"id":"cf3d0f4f-ed57-43ff-bdb9-d88522f35f20","value":"0718345346"}}
]
  1. Initialize a counter called index with 0 by adding a Save Flow Result action
  2. Check whether there is a number in current index (which is 0) with a Split by Expression
    @(webhook.rows[results.index])
  3. If there is a number → do something (Send message, send report to CHT etc).
  4. Update the index using another Save Flow Result action
    @(results.index + 1)
  5. Loop back to the Split by Expression step of the flow and check @(webhook.rows[results.index]) this time it will check index 1 and get the second number
  6. If there is a number → do something (Send message, send report to CHT etc).

Continue with the loop until there are no numbers left.

4 Likes