Converting Country Names to ISO Codes in Zapier with JavaScript

I’m trying to use Zapier’s Email Parser to get country names. I need to change these names into two-letter ISO country codes before sending them to Salesforce as part of a lead. I think I can do this with Zapier’s Code function using JavaScript, but I’m stuck.

I found some JavaScript that might work, but I’m not great with coding. When I try to use it in Zapier with ‘name’ as the country input, I get an error saying ‘SyntaxError: Unexpected template string’.

Here’s a short version of the code I’m trying to use:

const countryList = {
  'US': { name: 'United States' },
  'CA': { name: 'Canada' },
  'UK': { name: 'United Kingdom' },
  // more countries...
};

function getCountryCode(countryName) {
  for (let code in countryList) {
    if (countryList[code].name === countryName) {
      return code;
    }
  }
  return 'Country not found';
}

Can someone help me figure out what’s wrong and how to make this work in Zapier? Thanks!

As someone who’s worked extensively with Zapier and country code conversions, I can share a few insights that might help you out, Sophia. First off, the ‘SyntaxError’ you’re encountering is likely due to how Zapier interprets JavaScript. To avoid this, stick to ES5 syntax instead of ES6.

Here’s a modified version of your code that should work in Zapier:

var countryList = {
  'US': { name: 'United States' },
  'CA': { name: 'Canada' },
  'UK': { name: 'United Kingdom' },
  // Add more countries here
};

function getCountryCode(countryName) {
  for (var code in countryList) {
    if (countryList[code].name === countryName) {
      return code;
    }
  }
  return 'Country not found';
}

var input = inputData.name;
var countryCode = getCountryCode(input);
output.set('countryCode', countryCode);

This approach has worked well for me in similar scenarios. Just make sure your ‘countryList’ is comprehensive enough for your needs. If you’re dealing with a lot of countries, consider using a more robust solution like a country code API or a pre-built npm package as suggested by others.

hey sophia, i think ur issue might be with how zapier handles template strings. try using regular quotes instead of backticks for the country names. also, make sure ur passing the country name correctly to the function. heres a quick example:

const countryCode = getCountryCode(‘United States’);
output.set(‘countryCode’, countryCode);

hope this helps! lmk if u need more help

I’ve encountered similar issues with Zapier’s Code function. The problem might be in how you’re passing the country name to the function. Try modifying your code to explicitly get the input from Zapier:

const input = inputData.name;
const countryCode = getCountryCode(input);
output.set(‘countryCode’, countryCode);

Also, ensure your country list is comprehensive. You might want to consider using a pre-built library like the ‘country-list’ npm package, which Zapier supports. This approach is more reliable and saves you from maintaining a long list of countries manually.

If you’re still having trouble, double-check that your input is exactly as expected. Sometimes, trailing spaces or unexpected characters can cause issues.