W_Tracy | 2023-06-05 15:11:52 UTC | #1
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:
- Turn the returned object(the column LocalTouchNumbers) into an array. I assume a split function based on the comma
- Find a random number in that array and output only that in the data action
Example JSON output { "LocalTouchNumbers": "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 LocalTouchNumbers above.
Thank you!!
Jason_Mathison | 2023-06-05 20:09:38 UTC | #2
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 LocalTouchNumbers 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
W_Tracy | 2023-06-06 18:03:24 UTC | #3
Gentleman and a Scholar, thank you!!
system | 2023-07-07 18:03:44 UTC | #4
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 20259