Zapier Code - Python KeyError when accessing input_data Key with no Value











up vote
0
down vote

favorite












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










share|improve this question







New contributor




Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    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










    share|improve this question







    New contributor




    Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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










      share|improve this question







      New contributor




      Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question







      New contributor




      Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Paul Shearer

      1




      1




      New contributor




      Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Paul Shearer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          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.






          share|improve this answer





















            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',
            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
            });


            }
            });






            Paul Shearer is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            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








            up vote
            1
            down vote













            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.






            share|improve this answer

























              up vote
              1
              down vote













              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.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                Michael Case

                1638




                1638






















                    Paul Shearer is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    Paul Shearer is a new contributor. Be nice, and check out our Code of Conduct.













                    Paul Shearer is a new contributor. Be nice, and check out our Code of Conduct.












                    Paul Shearer is a new contributor. Be nice, and check out our Code of Conduct.















                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Sphinx de Gizeh

                    Dijon

                    Équipe cycliste