Three tips for testing and debugging javascript in Zapier code actions

Zapier is bar none the handiest utility in existence for automating online workflows and connecting different online services. This was true even before the introduction of custom code actions, which allow you to drop in blocks of javascript and python code to manipulate data in your zaps, and it’s doubly true now.

Debugging and testing custom code in Zapier, however, takes a little getting used to, especially if you’re accustomed to more traditional programming tools. Here are three immediate things to keep in mind as you get started:

  1. Javascript code actions must return either an object or an array of objects. This is all well and good, but if you expect your code to return an array when testing your action within Zapier, you may find yourself confused when the test outputs only the first item of your array. You may then find yourself checking your code, over and over again, then your input, over and over again, then your favorite swear words, then your sanity. But the truth is your code is working fine, and Zapier is working fine: testing a code action will output only the first object in an array, no matter how many objects are actually in the array. This is only true of testing your zap; when the zap actually runs, the full array will be processed.
  2. The above behavior seems counterintuitive (I mean, why wouldn’t someone expect to see the full output of their array in a test?), until you consider this: any subsequent action after your code will process each item in the returned array individually. For instance, if your code returns an array with four items, and the following action is Zapier’s stock text formatter to transform all upper case letters to lower case letters, then the text formatter will loop through all four, acting on each in order. So, when the test in Zapier shows you only the first item in your multi-item array, it’s showing you the data that will be immediately passed to the next action, which is the most relevant thing when chaining actions together in your zap. It takes a minute to wrap your head around, but when you get it everything falls into place.
  3. Logging to the console is your friend. Drop in a console.log(your-array-variable-here) within your code action to confirm the full contents of your array during testing. The output of console.log will show in the test output under the logs key.
A single array item output in testing
Javascript code that returns an array of objects will display only the first item of the array in the Zapier test output. But the full array will be processed when the zap is run. In this example, the zap returned the first url in an array of urls, using this javascript code for pulling all URLs from any text.
Output from console.log in Zapier
Use console.log() in your Zapier javascript code to verify the full contents of an array when testing your code action.

How to use Zapier to extract all URLs from a text source

Here’s a nifty trick for pulling all of the URLs out of text with a Zapier javascript code action.

Using this, you can extract URLs from any Zapier text input. For instance, you could use the Zapier email trigger to pull all of the URLs from an email message, or you could use the push button trigger to pull the URLs from a webpage.

The code is based on Zapier’s example documentation for extracting email addresses from text, combined with this handy regex for extracting URLs.

Here’s the code, which you can paste into a Zapier javascript code action:

var urls = inputData.rawText.match(/\b((?:[a-z][\w\-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]|\((?:[^\s()<>]|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi) || [];
return {
  return {url: url};

The output will be an array of javascript objects, which you can then process with other Zapier actions. If you’re new to working with code actions in Zapier, check out these tips for testing Zapier code actions and working with arrays.