Select random bundle from JSON

I have a question that I feel is probably really simple to do, but I can’t figure it out.

My process is: I have a Hubspot form submission for a “Call me back” form, that enters a phone number into a Scenario using a Webhook. I then make an API call to Aircall to get the availability of agents. This returns a JSON with agent IDs and a value for availability: available, unavailable or offline.

Now I want to randomly pick one of the available agents, so that I can put their ID into another API call to Aircall to start an outbound call.

I’m at the point where I have:

  • An Iterator to turn the JSON into separate agent bundles
  • A filter that runs right after the iterator to only pass agents that have availability = Available.

…but now I need to randomize the selection of the agent somehow. And I’m lost :frowning:

I’ve tried putting them in a data store first, along with an incremented [i] for a key, then using a Set Variable to generate a random number between 1 and [i] after the last iteration, to decide which record to retrieve from the database. But I reached a dead end there because I couldn’t find a way to stop the iteration without also losing access to the [i]. That’s probably way too convoluted…

This is what I had:

Any ideas? I feel like this should be relatively easy but I can’t figure it out.

The best way to select a random bundle is to

  1. Convert bundles into an array using the array aggregator
  2. Right in the next module, use the array iterator with the map turned on, to iterate the just aggregated array, except use the shuffle() option to shuffle the array contents randomly.
  3. The iterator will now proceed to generate bundles from your array but in random order. You can then setup a filter to filter for example by bundle position number to select the first x bundles, or just select the first one.

Here the array aggregator is connected to a Search module that just retrieved a bundle of bundles from an app and the array aggregator converts all those bundles into an array with each bundle as a collection in the array.

The iterator then just rearranges the result in random order using shuffle, and regenerates the bundles for subsequent processing.