Zapier Code - Python KeyError when accessing input_data Key with no Value
I am using a Zapier code step to format multiple pieces of data for later use in my multi-step Zap. The curious thing about this problem is that my Code step passes the test. My suspicion is that it fails when one of the pieces of data that I am passing into the input_data dictionary has no value from the previous step. This is often the case with optional form fields which may or may not contain value.
I am getting a KeyError in my Task History logs, and subsequently Zapier keeps turning my Zap off.
Did I make a mistake? Or is this a bug in the way Zapier Code handles input_data keys that have no value?
Here is my code:
gender = input_data['gender']
renewalDate = input_data['renewalDate']
dateOfBirth = input_data['dateOfBirth']
createdOn = input_data['createdOn']
registrationDate = input_data['registrationDate']
fullName = input_data['fullName']
nameArray = fullName.split(" ")
firstName = nameArray[0]
lastName = nameArray[-1]
def format_date(d):
if len(d) > 10:
formatted = d[:10]
return formatted
else:
return None
if gender == '1':
gender = 'Not set'
elif gender == '2':
gender = 'Male'
elif gender == '3':
gender = 'Female'
elif gender == '4':
gender = 'Other'
else:
gender = 'Rather not say'
renewalDate = format_date(renewalDate)
dateOfBirth = format_date(dateOfBirth)
createdOn = format_date(dateOfBirth)
registrationDate = format_date(registrationDate)
output = [{
'gender': gender,
'renewalDate': renewalDate,
'dateOfBirth': dateOfBirth,
'createdOn': createdOn,
'registrationDate': registrationDate,
'firstName': firstName,
'lastName': lastName
}]
Here is Zapier reporting the error in task history
zapier
add a comment |
I am using a Zapier code step to format multiple pieces of data for later use in my multi-step Zap. The curious thing about this problem is that my Code step passes the test. My suspicion is that it fails when one of the pieces of data that I am passing into the input_data dictionary has no value from the previous step. This is often the case with optional form fields which may or may not contain value.
I am getting a KeyError in my Task History logs, and subsequently Zapier keeps turning my Zap off.
Did I make a mistake? Or is this a bug in the way Zapier Code handles input_data keys that have no value?
Here is my code:
gender = input_data['gender']
renewalDate = input_data['renewalDate']
dateOfBirth = input_data['dateOfBirth']
createdOn = input_data['createdOn']
registrationDate = input_data['registrationDate']
fullName = input_data['fullName']
nameArray = fullName.split(" ")
firstName = nameArray[0]
lastName = nameArray[-1]
def format_date(d):
if len(d) > 10:
formatted = d[:10]
return formatted
else:
return None
if gender == '1':
gender = 'Not set'
elif gender == '2':
gender = 'Male'
elif gender == '3':
gender = 'Female'
elif gender == '4':
gender = 'Other'
else:
gender = 'Rather not say'
renewalDate = format_date(renewalDate)
dateOfBirth = format_date(dateOfBirth)
createdOn = format_date(dateOfBirth)
registrationDate = format_date(registrationDate)
output = [{
'gender': gender,
'renewalDate': renewalDate,
'dateOfBirth': dateOfBirth,
'createdOn': createdOn,
'registrationDate': registrationDate,
'firstName': firstName,
'lastName': lastName
}]
Here is Zapier reporting the error in task history
zapier
add a comment |
I am using a Zapier code step to format multiple pieces of data for later use in my multi-step Zap. The curious thing about this problem is that my Code step passes the test. My suspicion is that it fails when one of the pieces of data that I am passing into the input_data dictionary has no value from the previous step. This is often the case with optional form fields which may or may not contain value.
I am getting a KeyError in my Task History logs, and subsequently Zapier keeps turning my Zap off.
Did I make a mistake? Or is this a bug in the way Zapier Code handles input_data keys that have no value?
Here is my code:
gender = input_data['gender']
renewalDate = input_data['renewalDate']
dateOfBirth = input_data['dateOfBirth']
createdOn = input_data['createdOn']
registrationDate = input_data['registrationDate']
fullName = input_data['fullName']
nameArray = fullName.split(" ")
firstName = nameArray[0]
lastName = nameArray[-1]
def format_date(d):
if len(d) > 10:
formatted = d[:10]
return formatted
else:
return None
if gender == '1':
gender = 'Not set'
elif gender == '2':
gender = 'Male'
elif gender == '3':
gender = 'Female'
elif gender == '4':
gender = 'Other'
else:
gender = 'Rather not say'
renewalDate = format_date(renewalDate)
dateOfBirth = format_date(dateOfBirth)
createdOn = format_date(dateOfBirth)
registrationDate = format_date(registrationDate)
output = [{
'gender': gender,
'renewalDate': renewalDate,
'dateOfBirth': dateOfBirth,
'createdOn': createdOn,
'registrationDate': registrationDate,
'firstName': firstName,
'lastName': lastName
}]
Here is Zapier reporting the error in task history
zapier
I am using a Zapier code step to format multiple pieces of data for later use in my multi-step Zap. The curious thing about this problem is that my Code step passes the test. My suspicion is that it fails when one of the pieces of data that I am passing into the input_data dictionary has no value from the previous step. This is often the case with optional form fields which may or may not contain value.
I am getting a KeyError in my Task History logs, and subsequently Zapier keeps turning my Zap off.
Did I make a mistake? Or is this a bug in the way Zapier Code handles input_data keys that have no value?
Here is my code:
gender = input_data['gender']
renewalDate = input_data['renewalDate']
dateOfBirth = input_data['dateOfBirth']
createdOn = input_data['createdOn']
registrationDate = input_data['registrationDate']
fullName = input_data['fullName']
nameArray = fullName.split(" ")
firstName = nameArray[0]
lastName = nameArray[-1]
def format_date(d):
if len(d) > 10:
formatted = d[:10]
return formatted
else:
return None
if gender == '1':
gender = 'Not set'
elif gender == '2':
gender = 'Male'
elif gender == '3':
gender = 'Female'
elif gender == '4':
gender = 'Other'
else:
gender = 'Rather not say'
renewalDate = format_date(renewalDate)
dateOfBirth = format_date(dateOfBirth)
createdOn = format_date(dateOfBirth)
registrationDate = format_date(registrationDate)
output = [{
'gender': gender,
'renewalDate': renewalDate,
'dateOfBirth': dateOfBirth,
'createdOn': createdOn,
'registrationDate': registrationDate,
'firstName': firstName,
'lastName': lastName
}]
Here is Zapier reporting the error in task history
zapier
zapier
asked Nov 20 at 22:21
Paul Shearer
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When using the Python code module in Zapier the key-value pairs you supply for the input_data variable are converted into a Python dictionary object. The error you are receiving is a result of attempting to retrieve a key from the input_data dictionary that is not there. Noted in the documentation:
d[key]:
Return the item of d with key key. Raises a KeyError if key is not in the map.
I would instead recommend retrieving values from the input_data dictionary using the d.get(key)
method.
get(key[, default]):
Return the value for key if key is in the
dictionary, else default. If default is not given, it defaults to
None, so that this method never raises a KeyError.
This is handy because rather than returning an error if it does not find a matching key it simply returns None
, or you can specify a default return value if the key is not found d.get(key, default)
. You can read more about it in the link provided above.
Hope this helps.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402469%2fzapier-code-python-keyerror-when-accessing-input-data-key-with-no-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When using the Python code module in Zapier the key-value pairs you supply for the input_data variable are converted into a Python dictionary object. The error you are receiving is a result of attempting to retrieve a key from the input_data dictionary that is not there. Noted in the documentation:
d[key]:
Return the item of d with key key. Raises a KeyError if key is not in the map.
I would instead recommend retrieving values from the input_data dictionary using the d.get(key)
method.
get(key[, default]):
Return the value for key if key is in the
dictionary, else default. If default is not given, it defaults to
None, so that this method never raises a KeyError.
This is handy because rather than returning an error if it does not find a matching key it simply returns None
, or you can specify a default return value if the key is not found d.get(key, default)
. You can read more about it in the link provided above.
Hope this helps.
add a comment |
When using the Python code module in Zapier the key-value pairs you supply for the input_data variable are converted into a Python dictionary object. The error you are receiving is a result of attempting to retrieve a key from the input_data dictionary that is not there. Noted in the documentation:
d[key]:
Return the item of d with key key. Raises a KeyError if key is not in the map.
I would instead recommend retrieving values from the input_data dictionary using the d.get(key)
method.
get(key[, default]):
Return the value for key if key is in the
dictionary, else default. If default is not given, it defaults to
None, so that this method never raises a KeyError.
This is handy because rather than returning an error if it does not find a matching key it simply returns None
, or you can specify a default return value if the key is not found d.get(key, default)
. You can read more about it in the link provided above.
Hope this helps.
add a comment |
When using the Python code module in Zapier the key-value pairs you supply for the input_data variable are converted into a Python dictionary object. The error you are receiving is a result of attempting to retrieve a key from the input_data dictionary that is not there. Noted in the documentation:
d[key]:
Return the item of d with key key. Raises a KeyError if key is not in the map.
I would instead recommend retrieving values from the input_data dictionary using the d.get(key)
method.
get(key[, default]):
Return the value for key if key is in the
dictionary, else default. If default is not given, it defaults to
None, so that this method never raises a KeyError.
This is handy because rather than returning an error if it does not find a matching key it simply returns None
, or you can specify a default return value if the key is not found d.get(key, default)
. You can read more about it in the link provided above.
Hope this helps.
When using the Python code module in Zapier the key-value pairs you supply for the input_data variable are converted into a Python dictionary object. The error you are receiving is a result of attempting to retrieve a key from the input_data dictionary that is not there. Noted in the documentation:
d[key]:
Return the item of d with key key. Raises a KeyError if key is not in the map.
I would instead recommend retrieving values from the input_data dictionary using the d.get(key)
method.
get(key[, default]):
Return the value for key if key is in the
dictionary, else default. If default is not given, it defaults to
None, so that this method never raises a KeyError.
This is handy because rather than returning an error if it does not find a matching key it simply returns None
, or you can specify a default return value if the key is not found d.get(key, default)
. You can read more about it in the link provided above.
Hope this helps.
answered Nov 20 at 22:51
Michael Case
2188
2188
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402469%2fzapier-code-python-keyerror-when-accessing-input-data-key-with-no-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown