JavaScript code executing duplicate iterations in Zapier automation

I’m having trouble with my Zapier workflow where JavaScript seems to run extra times. I have two separate automations that work fine individually, but when I combine them, things get duplicated.

Each automation processes form data from different sections and creates CRM records. The process goes like this:

  • Form trigger with repeating fields
  • JavaScript that splits comma-separated values into arrays
  • Creates individual CRM tasks from each array item

When I try to merge both scripts, I get duplicate entries. Here’s my combined JavaScript:

if (inputData.salesAccountData == null) {
  var accountList = [];
  var quantityList = [];
  var costList = [];
  var commentsList = [];
  var meetingTypeList = [];
} else {
  var accountList = inputData.salesAccountData.split(",");
  var quantityList = inputData.salesQuantityData.split(",");
  var costList = inputData.salesCostData.split(",");
  var commentsList = inputData.salesCommentsData.split(",");
  var meetingTypeList = inputData.salesMeetingData.split(",");
}
var results = [];
var totalItems = accountList.length;
var counter = 0;
do {
  var currentAccount = new String(accountList[counter]);
  var currentQuantity = new String(quantityList[counter]);
  var currentCost = new String(costList[counter]);
  var currentComments = new String(commentsList[counter]);
  var currentMeeting = new String(meetingTypeList[counter]);
  var itemData = {};
  itemData.accountName = currentAccount;
  itemData.quantity = currentQuantity;
  itemData.cost = currentCost;
  itemData.comments = currentComments;
  itemData.meetingType = currentMeeting;
  results.push({ itemData });
  counter++;
} while (counter < totalItems);

if (inputData.generalAccountData == null) {
  var generalAccountList = [];
  var generalCommentsList = [];
  var generalMeetingList = [];
} else {
  var generalAccountList = inputData.generalAccountData.split(",");
  var generalCommentsList = inputData.generalCommentsData.split(",");
  var generalMeetingList = inputData.generalMeetingData.split(",");
}
var results = [];
var totalItems = generalAccountList.length;
var counter = 0;
do {
  var currentGeneralAccount = new String(generalAccountList[counter]);
  var currentGeneralComments = new String(generalCommentsList[counter]);
  var currentGeneralMeeting = new String(generalMeetingList[counter]);
  var itemData = {};
  itemData.generalAccount = currentGeneralAccount;
  itemData.generalComments = currentGeneralComments;
  itemData.generalMeeting = currentGeneralMeeting;
  results.push({ itemData });
  counter++;
} while (counter < totalItems);

I’m new to JavaScript and think I might be missing something basic. Why does this create duplicates and how can I fix it?

Found your problem - you’re wiping out your results array halfway through the script. You build the first array with all the sales data, then you declare var results = []; again, which kills everything you just processed. That’s why you’re only getting the general account data at the end.

Just remove that second var results = []; line and keep appending to your existing array. Also, you’re reusing counter and totalItems variables, which gets messy. I’d rename them to something like generalCounter and generalTotalItems for the second section.

The duplication might be coming from your Zapier workflow too. Check if you’ve got multiple triggers firing or if the form’s submitting twice. Look at your Zapier logs to see if it’s running multiple times for one submission - happens a lot when people double-click submit buttons.

Your problem’s pretty simple. You’re reinitializing your results array with var results = []; before processing the general account data, which wipes out all the sales data from the first loop. You’re basically throwing away everything from the first section and only keeping the second results. Just remove that second var results = []; line so both sections add to the same array. Also, you can clean up your code by combining those null checks at the start. One more thing - make sure your form data arrays are the same length. If you’ve got three sales accounts but only two quantities, you’ll hit errors when trying to access undefined array elements. Add some quick length validation before processing to avoid headaches.

you’re declaring var results = []; twice, which resets the array. The first loop builds your sales data, then you wipe it clean before the second loop runs. that’s why you only see general account data. just remove that second declaration and you’re set.