Jun 12, 2017

Using JavaScript in Zaps

Zapier has a utility called "Code by Zapier" that allows you to run anything from little snippets of code to full-blown Node.js apps inside your zaps. There are some limitations on how long your code can take to execute, so the power isn't limitless, but it turns out this tool can solve a lot of problems with only a little (or a lot) of code.
Many times the Zapier triggers do just what I want, but the actions don't. Now I can solve that by writing more sophisticated code on the back end and using the new Code by Zapier to call that code. Other times, I just need to add some simple if-then-else logic inside a Zap and again this new feature does just what I need. I'm planning a series of posts here to dig into various ways to use Javascript. This feature also supports Python, but I'll limit myself to examples that use Javascript.
A simple example is the ability to set your Slack Status via a Zap. Normally this is a two-step Zap using a Schedule by Zapier trigger to fire an action on a Slack Action that sets the Slack status and emoji. Each weekday morning this fires to set my Slack status and emoji to indicate I'm available. This works great without code, but the first time I was out of the office on PTO, it overrode my PTO status. This is where a bit of code and three extra Zap steps were helpful.
  1. The Slack Find User by Email action.
  2. A Code by Zapier action
  3. A Filter by Zapier filter.
The first step just needs to find yourself. Set the Code by Zapier step to use JavaScript then do these steps (shown in the image):
  1. In the Input Data section in the box on the left, type a name for the data, I used StatusText.
  2. Using the Insert a Field widget Select the newly added Find User by Email action. After clicking you'll see a long drop down and you can scroll down until you see User Profile Status Text.
  3. The selected field Select that to map the user status to the StatusText input field. The selected user's status will be stored in an object named inputData in a field named StatusText.
  4. Add an if statement and set up a json object to return that can be used in the next step.. The code looks like this.
    //If on PTO don't change the status
    if (inputData.StatusText.toUpperCase().indexOf("PTO") != -1){
    output = {continue:false};
    output = {continue:true};

Now in the Filter by Zapier step you'll click the Code by Zapier step. You'll notice in the image I named my code step "Set continue false if on PTO". Once you click on your code step, you should see an element named Continue. This name can also be anything you want, although Zapier does appear to capitalize it. If your code needs to return more information just create a valid JSON object and every valid field from the JSON object will show up in the list. Once you have the Continue field selected, set the conditional box to (Boolean) iS true.
That's it. If the JavaScript returns false, the filter will kick in and the original final step of setting your Slack status won't fire. If it returns true, the final step will be invoked.

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.