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

I’m new to Zapier and struggling with JavaScript output formatting. I set up a workflow where Manychat sends weight and height data to Zapier, then I use the Run JavaScript action to calculate body mass index.

The JavaScript runs without errors, but I can’t figure out how to properly return the calculated values back to my workflow. When I try to pass the results to Manychat, I get weird characters instead of the expected text.

Here’s my current code:

// BMI calculation functions
function getHealthMessage(index) {
  var categories = [
    {limit: 12, text: "severely underweight condition"}, 
    {limit: 16, text: "significantly below normal weight"}, 
    {limit: 18.5, text: "slightly under ideal weight"}, 
    {limit: 25, text: "healthy weight range!"}, 
    {limit: 30, text: "above recommended weight"}, 
    {limit: 40, text: "in obesity range"}, 
    {limit: 50, text: "severe obesity condition"}
  ];
  
  for (var j = 0; j < categories.length; j++) {
    if (index < categories[j].limit) {
      return categories[j].text;
    }
  }
  return categories[0].text;
}

function computeBMI(h, w) {
  if (h > 0 && w > 0) {
    return Math.round((w / Math.pow(h/100, 2)) * 100) / 100;
  }
  return 0;
}

function processData() {
  var userHeight = inputData.height;
  var userWeight = inputData.weight;
  var bmiValue = computeBMI(userHeight, userWeight);
  var healthMsg = getHealthMessage(bmiValue);
  
  // Not sure how to format this properly
  output = {finalBMI: bmiValue, message: healthMsg};
}

processData();

What’s the correct way to structure the output so Zapier can pass these values to the next step? Any help would be appreciated!

Your JavaScript code isn’t returning the output object - that’s why Zapier can’t capture the data properly. You need to return the output from your processData function and add a return statement at the end of your script: javascript function processData() { var userHeight = inputData.height; var userWeight = inputData.weight; var bmiValue = computeBMI(userHeight, userWeight); var healthMsg = getHealthMessage(bmiValue); return {finalBMI: bmiValue, message: healthMsg}; } return processData(); Those weird characters happen when Zapier can’t parse the output format. When you explicitly return the object, Zapier creates individual output fields you can use in later steps as finalBMI and message. I’ve seen this fix the same formatting problems in other workflows.

Yeah, Zapier’s JavaScript action trips people up. You’re creating an output variable but not returning it - Zapier needs explicit returns to grab the data. Change that last line from processData(); to return processData(); and make sure processData actually returns the object instead of just assigning to output. Those weird characters you’re seeing? That’s Zapier choking on undefined values.

This is super common with Zapier’s JavaScript step - I hit the same wall when I started. You’re declaring output as a variable but never actually returning it. Zapier needs that explicit return statement to grab your data.

Here’s the fix:

function processData() {
  var userHeight = inputData.height;
  var userWeight = inputData.weight;
  var bmiValue = computeBMI(userHeight, userWeight);
  var healthMsg = getHealthMessage(bmiValue);
  
  return {finalBMI: bmiValue, message: healthMsg};
}

return processData();

Use return instead of just assigning to output. When you return an object, Zapier creates output fields you can use in the next steps. Those weird characters? That’s Zapier getting undefined or null values instead of your actual results.

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