How to properly return values from JavaScript code in Zapier automation workflows?

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!

You’re mixing DOM manipulation with Zapier’s JavaScript environment. Zapier’s Code by Zapier runs server-side, so it can’t access document.getElementById() or any DOM elements.

Your calculation functions look good - just adapt them for Zapier’s input/output system. Don’t try grabbing values from DOM elements. Pull them straight from Zapier’s input data instead. Replace your processData() function with something that takes actual values from your trigger data.

For output, Zapier wants an array of objects. Structure your return like return [{bmi_value: calculatedIndex, health_message: messageText}]; instead of a single output object. This creates proper key-value pairs that other workflow steps can reference.

I got confused by this same thing when I started with Zapier. The shift from client-side to server-side JavaScript thinking is tricky. Just treat input data as variables instead of DOM elements and you’ll get clean results.

Your processData function isn’t returning anything useful. You’re trying to update DOM elements, but Zapier doesn’t have a DOM environment. Plus that output line assigns the function reference instead of running it. Quick fix: ditch all the document.getElementById stuff and return the calculated values directly. Try return [{index: calculateIndex(inputData.tall, inputData.mass), text: getHealthMessage(calculatedIndex)}]; instead.

Your processData function is the problem - it’s trying to grab DOM elements that don’t exist in Zapier. You can’t manipulate HTML elements there. Just pass the height and mass values directly from your trigger data.

Here’s how I do it:

// Get values from input data
const height = inputData.height || 180; // your trigger data
const mass = inputData.mass || 70;

// Calculate using your existing function
const bmiValue = calculateIndex(height, mass);
const healthMessage = getHealthMessage(bmiValue);

// Return properly formatted output
return [{bmi: bmiValue, message: healthMessage}];

Those weird characters? You’re probably returning the processData function itself instead of calling it and getting the actual results. Make sure you’re calling the function and returning real values, not function references. Also, Zapier needs an array of objects in the return, not just a single object.