I’m having trouble with a JavaScript code snippet in Zapier that should clean up customer names.
I’ve been transferring customer data from Shopify to another platform through Zapier for about a year now. Everything works fine most of the time, but I run into problems when customer names contain special characters like ampersands (&) or parentheses (). These characters break the data transfer process.
I’m trying to write a JavaScript function in Zapier’s Code Action to remove these problematic characters, but I keep getting errors. I’m pretty new to JavaScript so I might be missing something obvious.
// wrapped in async function
// await can be used anywhere in the function
cleanName = inputData.customerName.replace(/[^a-zA-Z0-9 ]/g, '-').replace(/-{2,}/g, '-');
return [cleanName];
The goal is to take names like “Mike & Sarah (wedding)” and convert them to something like “Mike - Sarah - wedding” so the integration doesn’t fail. What am I doing wrong with this code?
Your return value’s probably the issue. Zapier Code Actions need an object with named properties, not an array. Try this:
let cleanName = inputData.customerName.replace(/[^a-zA-Z0-9 ]/g, '-').replace(/-{2,}/g, '-');
return {cleanedName: cleanName};
I’ve used similar data cleaning for e-commerce integrations - the return format matters. Also add error handling for empty or undefined customerName fields since they’ll kill your whole zap. Just check if inputData.customerName exists before manipulating it and you’ll avoid headaches later.
Your variable declaration and return statement are the issue. Zapier needs specific formatting for Code Actions to work.
I hit the same problem cleaning product names with weird characters from international suppliers. Using const or let for variables and returning an object with named properties fixed it.
Try this:
const cleanName = inputData.customerName.replace(/[^a-zA-Z0-9 ]/g, '-').replace(/-{2,}/g, '-').replace(/^-+|-+$/g, '');
return {cleanedCustomerName: cleanName};
I added trim functionality at the end to remove leading or trailing dashes - your regex pattern can create those. Also, Zapier sometimes times out with complex string operations, so test with your actual data volume first. The error you’re seeing is probably from the array return format, not the string manipulation.
I’ve dealt with this data cleanup stuff for years. Your regex looks fine, but there’s a better way than patching JavaScript in Zapier.
I ditched Zapier code actions for Latenode last year. Much more reliable for string manipulation.
Latenode gives you proper JavaScript execution - no weird return format requirements. Write normal code that actually works. Build complex cleanup logic without hitting execution limits.
Create a flow: Shopify data → cleaning function → clean data to your destination. No more broken integrations from special characters.
I run similar e-commerce flows and the reliability difference is massive. Check it out: https://latenode.com
you’re missing the var declaration. try var cleanName = inputData.customerName.replace(/[^a-zA-Z0-9 ]/g, '-').replace(/-{2,}/g, '-'); instead. also make sure you return it as an object like return {cleanName: cleanName}; - zapier’s picky about that stuff.