Troubleshooting Python script in Zapier automation

I’m trying to run a Python script in Zapier but it’s not working. The error message is vague, just saying there was a problem creating a run python and that my code had an error. I’m not sure what’s wrong.

Here’s my code:

site_urls = {
    'Item1': 'http://example1.com',
    'Item2': 'http://example2.com'
}

item_names = {
    'Item1': 'First Item',
    'Item2': 'Second Item'
}

result_links = {}

for key, value in input_data.items():
    if value:
        input_data[key] = site_urls[key]
    else:
        del input_data[key]

for key in input_data:
    result_links[key] = f'<p><a href="{site_urls[key]}">{item_names[key]}</a></p>'

email_content = '<br>'.join(result_links.values())
return email_content

Can anyone spot what might be causing the error? I’m a bit rusty with Python so any help would be great. Thanks!

hey man, looks like ur missing the input_data dictionary. zapier needs that defined somewhere b4 the script runs. try adding smthin like:

input_data = {‘Item1’: True, ‘Item2’: False}

at the start. that might fix it. good luck!

I’ve encountered similar issues with Zapier Python scripts before. The problem likely stems from how Zapier handles input data. Instead of using a predefined input_data dictionary, try accessing Zapier’s input directly:

input_data = input

This allows your script to work with the data Zapier provides. Also, ensure all your dictionary keys match exactly between site_urls, item_names, and the incoming data. Zapier can be quite particular about these details.

Lastly, consider adding some error handling to help diagnose issues:

try:
# Your existing code here
except Exception as e:
return f’Error: {str(e)}’

This will give you more specific error messages to work with. Hope this helps!

I’ve run into this exact problem before, and it can be frustrating. The issue is likely related to how Zapier handles global variables. Try modifying your script to use Zapier’s input_data directly:

def process_data(input_data):
    site_urls = {
        'Item1': 'http://example1.com',
        'Item2': 'http://example2.com'
    }
    item_names = {
        'Item1': 'First Item',
        'Item2': 'Second Item'
    }
    result_links = {}
    
    for key, value in input_data.items():
        if value and key in site_urls:
            result_links[key] = f'<p><a href="{site_urls[key]}">{item_names[key]}</a></p>'
    
    return '<br>'.join(result_links.values())

output = process_data(input_data)

This approach wraps your logic in a function and uses Zapier’s input_data directly. It also adds some checks to prevent KeyErrors. Give it a shot and let us know if it resolves the issue.