Data Table splitting and random choice

Hi all!
Using a data table to store multiple phone numbers, separated by comma's, each row may have between 1 and 50 numbers in it.

I need help writing a response that will return 1 of those phone numbers at random, but I'm struggling to write a response that will:

  1. Turn the returned object(the column Local_Touch_Numbers) into an array. I assume a split function based on the comma
  2. Find a random number in that array and output only that in the data action

Example JSON output
{
"Local_Touch_Numbers": "7191112222, 7192223333, 7193334444, 7194445555, 7195556666",
"LastUpdate": "updated 4/29",
"key": "719"
}

current table pull

/api/v2/flows/datatables/${input.DataTableID}/rows/${input.key}?showbrief=false

Desired output from the data action would be ONLY one, but a random one of those numbers in Local_Touch_Numbers above.

Thank you!!

Hey W_Tracy,

Give this a try:

{
  "translationMap": {"LTN": "Local_Touch_Numbers", "Key": "key"},
  "translationMapDefaults": {},
  "successTemplate": "#set( $LTN-array = $LTN.replace(\"$esc.quote\", \"\").split(\", \")) #set( $random-entry = $math.random(0, $LTN-array.size()))  {\"result\" : \"$LTN-array.get($random-entry)\"}"
}

The replace gets rid of the quotes on the ends of the Local_Touch_Numbers string. The split then chops it up into an array. The math.random will return a number >= 0 and < the size of the array. Finally, the result will get the random index from the array of numbers and wrap it in quotes to make it a string.

--Jason

2 Likes

Gentleman and a Scholar, thank you!!

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