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
- 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! - Unnecessary
new String()calls: The results from.split()are already strings, so wrapping them innew String()is extra code that doesn’t help. - Variable scoping issues: Using
varcan lead to unexpected behavior when reusing variable names likeiandarrayNosacross 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
outputarray: 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/constinstead ofvar: These have stricter scoping rules, so you won’t run into issues with reused variable names.- Added
visitTypefield: 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 fornulland handles empty strings too.
How to Set Up Your Zap Now:
- Trigger: Cognito Form
- Code Step: Use the corrected code above
- Filter 1: Add a filter that only passes items where
visitTypeequalssales - Zoho1 Action: Create tasks using the sales-specific fields (
account,units, etc.) - Filter 2: Add a filter that only passes items where
visitTypeequalsnon-sales - 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
相关产品推荐
相关产品推荐

