Failed to run your Python code ‘str’ object has no attribute ‘copy’ in Zapier

I faced with a problem in Zapier. I have an Error “Failed to run your Python code ‘str’ object has no attribute ‘copy’ in Zapier”, but in the VSCode everything works just perfect.

enter image description here

Here is my code:

import re
import requests
import json

# Email for search
email_to_search  = (input['Email'])

# AMOcrm
subdomain = 'DOMAIN'
user_login = 'LOGIN'
api_key = 'API'

# Parameters for authorization
params = {'USER_LOGIN': user_login, 'USER_HASH': api_key}

# URL for company search by email
company_search_url = f'https://{subdomain}.amocrm.ru/api/v4/companies?query={email_to_search}'

# Request execution
response = requests.get(company_search_url, params=params)

# Check response status and company search
if response.status_code == 200:
    company_data = response.json()
    companies = company_data.get('_embedded', {}).get('companies', [])
    if companies:
        company_id = companies[0]['id']

        # Check response status and company search
        company_info_url = f'https://{subdomain}.amocrm.ru/api/v4/companies/{company_id}?with=leads'

        # Request execution
        company_info_response = requests.get(company_info_url, params=params)

        # Checking the response status and displaying company information
        if company_info_response.status_code == 200:
            company_info_data = company_info_response.json()

            # Obtaining information on related leads
            related_leads = company_info_data.get('_embedded', {}).get('leads', [])
            if related_leads:
                # Getting the ID of the last lead
                last_lead_id = related_leads[-1]['id']
                
                # Formation and output of a link to the last lead
                last_lead_url = f"https://{subdomain}.amocrm.ru/leads/detail/{last_lead_id}"
                print(last_lead_url)
            else:
                last_lead_url = ("No related leads found")

        else:
            last_lead_url = (f"Error in fetching company info: {company_info_response.status_code}")

    else:
        last_lead_url = ("Company not found")
else:
    last_lead_url = (f"Error: {response.status_code}")

output = last_lead_url

Checking the Zap settings in Zapier: I looked at the Zap settings in Zapier. This included making sure the field names were correct and matched the data expected in the code.

Fixing bugs in the code: I scrutinized my code and corrected errors I found, such as an error in the field names that were specified in the code, and made sure that the code was correctly extracting and processing data.

Checking for other issues: Despite all efforts, the error “Failed to run the Python code ‘str’ object has no attribute ‘copy'” remained unresolved. I verified that the data was in the correct format and the settings in Zapier were correct, but the error continued to occur.

  • There’s no copy anywhere in the code you posted.

    – 

  • input['Email'] is wrong, it should be input('Email'). You obviously didn’t copy the code accurately.

    – 




  • 1

    Yeah, there’s no way this code “works just perfect” in vscode…

    – 




  • giving the whole error might be helpful to debug. Because you are not calling .copy() on any str here. So, this might be happening somewhere behind your code.

    – 

Leave a Comment