You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Zapier合并JavaScript代码后出现重复执行及数据映射异常求助

Fixing Your Combined Zap Code for Cognito Forms to Zoho CRM

Hey there! Great job diving into JavaScript to combine your Zaps—let’s break down the key issues in your code and get it working smoothly.

First, Let’s Identify the Core Problems

  1. You’re overwriting critical variables: When you declare var output = []; the second time, it wipes out all the data you added from the "Visits with Sales" section. That’s why you couldn’t map to those fields in Zoho1—they were gone!
  2. Unnecessary new String() calls: The results from .split() are already strings, so wrapping them in new String() is extra code that doesn’t help.
  3. Variable scoping issues: Using var can lead to unexpected behavior when reusing variable names like i and arrayNos across sections.

Corrected Code

Here’s a revised version that preserves both sets of data and fixes these issues:

// Initialize a single output array to hold both types of visits
let output = [];

// Handle "Visits with Sales" section
if (inputData.stringVSAccount) {
  const vsAccounts = inputData.stringVSAccount.split(",");
  const vsUnits = inputData.stringVSUnits.split(",");
  const vsPrices = inputData.stringVSPrice.split(",");
  const vsNotes = inputData.stringVSNotes.split(",");
  const vsVisitCalls = inputData.stringVSVisitCall.split(",");

  // Loop through each sales visit item and add to output
  for (let i = 0; i < vsAccounts.length; i++) {
    output.push({
      visitType: "sales", // Add a marker to distinguish visit types
      account: vsAccounts[i],
      units: vsUnits[i],
      price: vsPrices[i],
      notes: vsNotes[i],
      visitCall: vsVisitCalls[i]
    });
  }
}

// Handle "Visits without Sales" section
if (inputData.stringOVAccount) {
  const ovAccounts = inputData.stringOVAccount.split(",");
  const ovNotes = inputData.stringOVNotes.split(",");
  const ovVisitCalls = inputData.stringOVVisitCall.split(",");

  // Loop through each non-sales visit item and add to output
  for (let i = 0; i < ovAccounts.length; i++) {
    output.push({
      visitType: "non-sales", // Marker for non-sales visits
      account: ovAccounts[i],
      notes: ovNotes[i],
      visitCall: ovVisitCalls[i]
    });
  }
}

// Return the combined list of visit items
return output;

Key Improvements:

  • Single output array: We initialize it once at the top, so both sales and non-sales visits are added to the same list instead of overwriting each other.
  • let/const instead of var: These have stricter scoping rules, so you won’t run into issues with reused variable names.
  • Added visitType field: This lets you filter which tasks go to each Zoho CRM action (more on that below).
  • Simplified null checks: if (inputData.stringVSAccount) works just as well as checking for null and handles empty strings too.

How to Set Up Your Zap Now:

  1. Trigger: Cognito Form
  2. Code Step: Use the corrected code above
  3. Filter 1: Add a filter that only passes items where visitType equals sales
  4. Zoho1 Action: Create tasks using the sales-specific fields (account, units, etc.)
  5. Filter 2: Add a filter that only passes items where visitType equals non-sales
  6. Zoho2 Action: Create tasks using the non-sales fields (account, notes, etc.)

This setup will ensure each task is created exactly once, no duplicates, and both sections are handled in a single Zap.

内容的提问来源于stack exchange,提问作者DMartin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:45:44