How can I prevent double iterations in my Zapier JavaScript code?

I’m having trouble with my Zapier automation. I’ve got two zaps that work fine separately but when I try to combine them, I’m getting unexpected results.

Here’s what I’m trying to do:

  1. Trigger: Cognito Form (with repeating sections)
  2. JavaScript: Create arrays from form fields
  3. Action: Create ZOHO CRM Tasks

When I combine the zaps, the tasks are being duplicated. I’ve tried rearranging the steps and even merging the JavaScript code, but I’m still not getting it right.

Here’s a simplified version of my code:

function processData(prefix, inputData) {
  const fields = ['Account', 'Notes', 'VisitCall'];
  const arrays = {};
  
  fields.forEach(field => {
    const key = `string${prefix}${field}`;
    arrays[field] = inputData[key] ? inputData[key].split(',') : [];
  });
  
  return arrays.Account.map((_, i) => ({
    [`item${prefix}Account`]: arrays.Account[i],
    [`item${prefix}Notes`]: arrays.Notes[i],
    [`item${prefix}VisitCall`]: arrays.VisitCall[i]
  }));
}

const vsOutput = processData('VS', inputData);
const ovOutput = processData('OV', inputData);

output = [...vsOutput, ...ovOutput];

I’m new to JavaScript and Zapier. Any ideas on how to fix this looping issue?

I’ve encountered similar issues when combining Zaps, especially with repeating sections. The problem likely stems from how Zapier handles the data flow between steps. To prevent double iterations, you might want to consider using a single JavaScript step to process all your data at once, rather than separate steps for each prefix.

Try modifying your code to handle both prefixes in one go:

function processAllData(inputData) {
  const prefixes = ['VS', 'OV'];
  const fields = ['Account', 'Notes', 'VisitCall'];
  let output = [];

  prefixes.forEach(prefix => {
    const arrays = {};
    fields.forEach(field => {
      const key = `string${prefix}${field}`;
      arrays[field] = inputData[key] ? inputData[key].split(',') : [];
    });

    output = output.concat(arrays.Account.map((_, i) => ({
      [`item${prefix}Account`]: arrays.Account[i],
      [`item${prefix}Notes`]: arrays.Notes[i],
      [`item${prefix}VisitCall`]: arrays.VisitCall[i]
    })));
  });

  return output;
}

output = processAllData(inputData);

This approach should prevent duplication and simplify your Zap structure. Remember to test thoroughly after implementing changes.

I’ve been in your shoes, struggling with Zapier automations and unexpected duplications. Here’s what worked for me:

Instead of combining separate zaps, try creating a single zap with multiple actions. This approach helped me avoid double triggers and redundant processing.

For your JavaScript code, consider using a more robust method to handle the data. I found that using Object.entries() and reduce() gave me better control:

const processAllData = (inputData) => {
  const prefixes = ['VS', 'OV'];
  const fields = ['Account', 'Notes', 'VisitCall'];

  return prefixes.reduce((acc, prefix) => {
    const prefixData = fields.reduce((fieldAcc, field) => {
      const key = `string${prefix}${field}`;
      fieldAcc[field] = inputData[key] ? inputData[key].split(',') : [];
      return fieldAcc;
    }, {});

    const items = prefixData.Account.map((_, i) => 
      fields.reduce((itemAcc, field) => {
        itemAcc[`item${prefix}${field}`] = prefixData[field][i];
        return itemAcc;
      }, {})
    );

    return [...acc, ...items];
  }, []);
};

output = processAllData(inputData);

This approach consolidates everything into a single function, reducing the chances of unintended iterations. Hope this helps!

hey there, i had a similar issue with my zaps. have you tried using a single javascript step instead of combining multiple ones? that might help avoid the duplication problem. also, make sure you’re not accidentally triggering the zap twice somehow. double-check your trigger settings and see if theres any overlap there. good luck!