Skip to main content

Hi there!
Tim here from the Zapier Support Team with a way to improve your Code by Zapier Steps.

A common question that I see relating to Code Steps is how to prevent them from erroring when a value mapped into the Input Data fields sometimes comes through as empty.

Let’s consider this Python Code Step:

If we try to test it, we get KeyError: 'value3'

This is because when the contents of an Input Data field are empty, the key for that particular field isn’t created in the input_data object, so we’re trying to access something that isn’t there. The Input Data fields from the example above would create an object in the Code environment that looks like this: input_data = {"value1”: "hello", "value2": "world}

We can see that because value3 was empty, it wasn’t added to the input_data object.

Knowing this, we can catch the error and assign a default value instead. In Python, this could look like a try: except: Block. For example:

This gives us the following result: 


As you can see, value3 came through with a default value that we set instead of causing the Step to error.

With Javascript, we can use a similar solution where we can check if the value exists (evaluates to true in an If statement) and assign a default value if it doesn’t:
 

Here’s an alternate, more condensed way that you could write this in Javascript:


In this example, I created an array of the my Input Data value names so that I can use a loop to check if each one exists and set the same default value for each one that’s missing. This could be useful if you wanted the default for all missing values to be something like `0` or an empty String `""`. Both version of the Javascript give us a similar output to the Python example:

 

I hope you find this useful in getting your Zaps with Code to run more smoothly. Let me know what questions you have about this in the comments!

Please note

We aren't always able to help with Code questions via Zapier Support because not everyone on the Support Team is familiar with Javascript and Python, and supporting custom code is technically outside of our scope of support. A great place to ask if we can't support you directly is here in our community or on Stack Overflow by tagging your question "Zapier": https://stackoverflow.com/questions/tagged/zapier

Awesome, @TimS! I’ve seen this question come up several times in Support and also in the Community — great job with this :)


This is so sad,  a few years ago when we started with Zapier we were told to handle empty values by using a format step evaluating if the value is empty and setting it to default. The problem is we have 20, 30, and up to 40 steps zaps were half of them are these empty test format steps, and guess what… we pay for these steps every month, easily we have spent hundreds of dollars for no reason during multiple years, what a shame. We are using 20k tasks every month and probably we only need 10k or 12k, this is so wrong. Zapier should show us this way to handle empty values from the beginning.

Let's make something clear, this is not a Support issue, or a documentation issue, the fact that the code step fails to pass a input variable when it comes empty is evidence of how badly the tool was developed. This issue is on the Zapier itself developers, they should design and creat the “code” step to pass the input as empty ”” or null as any other systems does but they decided to "not create the input”, this is the real issue and it's on them.

 

Change this behavior should be the right solution as an improvement in the Zapier's core itself, allowing code steps to create empty variables when the input is empty, at least you should put a link on every Keyerror for empty values or missing fields to this article for everybody to check it out. Nobody should use format steps in these situations and you guys in Zapier must do everything to make sure it goes this way. Zapier is the most known and expensive automation solution and this is a very dirty way of making “extra” money from us. You must fix it right away and stop this madness, right?


Hi @zap learner ,

Sorry to hear that you weren’t aware of this. The reason that I set out to write this post was to try and help users in exactly your position with the hopes that the same folks who are likely to try and use an advanced part of Zapier like Code Steps would also be searching for ways to improve their Zaps. 

We don’t want you paying any more than you need to run your Zaps and we really like helping optimize things where we find an opportunity to do that. 

Customer Champions aren’t expected to know Python/Javascript, and supporting custom code is outside of the scope of our support, so my expectation is that when asked a question like this, support would give you an answer involving publicly available, non-code related apps like Formatter to lend a hand.

This is the answer I used to give and the solution I used in my own Zaps back when I was starting out using Zapier and hadn’t done much coding yet. It was only later after learning and practising code that I realized how the issue could be worked around. In fact since I wrote this post, I’ve found an even more straightforward way to set defaults in Python. It’s in the help documentation, and I didn’t even realize it until now because I didn’t understand why you would want to access values in this way.

In Python, there is a get() method you can use on any dictionary to retrieve a value. If the key doesn’t exist, it returns null instead. You can also specify a default to return instead of the null. If I had a ‘choice’ input_data value that I wanted to get with a default of ‘Default’ if it wasn’t supplied, I would write: choice = input_data.get('choice', 'Default')

Even without specifying a default value, get() doesn’t throw a KeyError. You can see that being used in the first screenshot on this help page: https://zapier.com/help/create/code-webhooks/use-python-code-in-zaps

Zapier in general works with empty fields the same way in the Code app that it does for other apps. When we run an update Step for example, we only want to update the fields that have data mapped and populated into them. If a field comes through as blank, we wouldn’t want to update that as it could delete existing data in the record being updated.

I was also frustrated by not getting empty strings for keys that didn’t have values in the past. With more experience coding, I think that not creating those empty keys can foster better, more efficient code. For example, if empty strings were added, and the you used the get method as I described above, you’d get the empty string instead of the default you wanted, so you would need to write even more code to set the actual defaults.

I am glad that we were able to reach you with this info now and if this helps you use an advanced feature to save money and/or do more with Zapier, that’s just what we want.


Using classes and ES6 for #javascript:

:Use Case: URL Constructor for Airtable

// ☸
class DefaultKeys {constructor(keys={}){Object.assign(this,{
base : 'missingBase',
table : 'missingTable',
view : '',
fields : 'missingFields',
filterByFormula : '',
maxRecords : 100,
returnOnlyRecordIDs : false
}, keys);}}

let values = new DefaultKeys(inputData)
// ☸

where, base, table, view, fields, filterByFormula, maxRecords, & returnOnlyRecordIDs are possible keys in inputData.


Any thoughts on how to avoid the Deprecation Warning when running this JavaScript code based on your example?

ERROR (node:8) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead. (Triggered by calling processImmediate on process.) (Use `node --trace-deprecation ...` to show where the warning was created) START RequestId: 3676031b-69ee-4200-91f4-1b03338ded82 Version: $LATEST

 

 

 


Any thoughts on how to avoid the Deprecation Warning when running this JavaScript code based on your example?

ERROR (node:8) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead. (Triggered by calling processImmediate on process.) (Use `node --trace-deprecation ...` to show where the warning was created) START RequestId: 3676031b-69ee-4200-91f4-1b03338ded82 Version: $LATEST

 

 

 

okay odd .. I re-ran the test and there was no error.


Reply