MOFS
MOFSβ€’4h ago

convert user capture

Hi everyone, I have a simple for some but not for me πŸ™‚ issue. When the user answers yes or no to a capture, I need to convert it to true or false when I send my api call to create the record. I would like to do this so it's reusable for several captures in the flow. I tried functions but did not work as expected. Any other suggestions? Thanks in advance.
2 Replies
Tico | Voiceflow Assistant
You’ve got two easy, reusable ways to flip a β€œyes / no” capture into a true / false value before you hit your API step. ──────────────────────── 1) Inline conversion right inside the API body
──────────────────────── The JSON Body editor lets you use little JS expressions inside {{= … }}.
Example (assume the capture is stored in {yes_no}):
{
"agreed": {{= yes_no.toLowerCase() === 'yes' }}
}
{
"agreed": {{= yes_no.toLowerCase() === 'yes' }}
}
When the request is sent: β€’ if the user said β€œyes” β†’ "agreed": true
β€’ anything else (no, nope, etc.) β†’ "agreed": false Nothing else to maintain, and you can reuse that one-liner anywhere you reference the variable. ──────────────────────── 2) A reusable Function block (if you prefer a dedicated step)
──────────────────────── Create a Function step with one INPUT variable (e.g. answer) and one OUTPUT variable (e.g. bool_answer).
Paste this code:
export default async function main(args) {
const a = (args.inputVars.answer || '').trim().toLowerCase();
return {
outputVars: {
bool_answer: a === 'yes' || a === 'y' || a === 'true' || a === '1'
}
};
}
export default async function main(args) {
const a = (args.inputVars.answer || '').trim().toLowerCase();
return {
outputVars: {
bool_answer: a === 'yes' || a === 'y' || a === 'true' || a === '1'
}
};
}
β€’ Drop this Function anywhere you need the conversion.
β€’ Pass the capture into answer, the step spits out bool_answer (true/false) ready for your API body. Either route keeps your flow clean and 100 % reusable. Pick the one that fits your style!
MOFS
MOFSOPβ€’4h ago
this is great. Do you know if calling this function multiple times (5x +) will increase the number of credits consumed? The credits VF use it's crazy. a conversation in my flow ranges from 40 to 60. I would need hundreds of thousands to roll it out to production. I appreciate the help.

Did you find this page helpful?