I’m new to Zapier and struggling with JavaScript implementation. I have a workflow that receives person data from a chatbot and need to process it with custom JavaScript code.
My setup: I get two numbers from the chatbot trigger, then use Zapier’s JavaScript action to calculate health metrics. For testing, I’m using sample values like mass = 70 and height = 180.
I found some JavaScript code online that calculates health indicators, but I can’t figure out how to properly return the results back to Zapier. I need both the numerical result and a text message, but I don’t understand Zapier’s output format.
// Health calculator functions
function getHealthMessage(index) {
var messageList = [{value: 12, text: "severely underweight condition"},
{value: 16, text: "significantly below normal range"},
{value: 18.5, text: "slightly below optimal range"},
{value: 25, text: "within healthy range!"},
{value: 30, text: "above recommended range"},
{value: 40, text: "significantly overweight!"},
{value: 50, text: "extreme weight category!"}];
var j;
var message = messageList[0].text;
for (j = 0; j < messageList.length; j++) {
if (index < messageList[j].value) {
message = messageList[j].text;
break;
}
}
return message;
}
function calculateIndex(tall, mass) {
var result = 0;
if (tall > 0 && mass > 0) {
result = Math.round((mass / Math.pow(tall/100, 2)) * 10) / 10;
}
return result;
}
function processData() {
var tallValue = document.getElementById('tall');
var massValue = document.getElementById('mass');
var display = document.getElementById('display');
var outcome = document.getElementById('outcome');
var index = calculateIndex(tallValue.value, massValue.value);
if (index > 0) {
display.textContent = index.toPrecision(3);
outcome.textContent = getHealthMessage(index)
}
}
output = {healthData: processData}; // my attempt to return data
The code runs without errors but returns weird characters instead of the proper text message. How do I correctly format the return statement or output object in Zapier? I need to send clean results back to my chatbot.
Any help would be appreciated!