Three tips for testing and debugging javascript in Zapier code actions

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.
Up Next:

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

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