Autofill CVR when a company is created

I’ve been trying to write a code to autofill the CVR number of a company when it is created. I have a code which I think should work but it isn’t at the moment. Can anyone help me on this? Below is the code I have so far:

import requests

from bs4 import BeautifulSoup

YOUR_HUBSPOT_ACCESS_TOKEN = “For my eyes only”

# Function to get the CVR of the company

def get_company_cvr(domain​:disappointed_face:

url = 'https://' + domain

try:

    response = requests.get(url,verify=False)

except requests.exceptions.SSLError as e:

    print(f"SSL error: {e}")

    return 'CVR not found'

soup = BeautifulSoup(response.text, 'html.parser')

cvr\_tag = soup.find('span', class\_='cvr-number')

if cvr\_tag:

    return cvr\_tag.text

else:

    return 'CVR not found'

# Function to get the recently created companies

def get_recent_companies():

headers = {

    'Authorization': 'Bearer YOUR\_HUBSPOT\_ACCESS\_TOKEN'

}

url = "https://api.hubapi.com/companies/v2/companies/recent/created"

response = requests.get(url, headers=headers)

return response.json()

# Function to update the CVR of a company

def update_company_cvr(company_id, cvr​:disappointed_face:

url = f"https://api.hubapi.com/companies/v2/companies/{company\_id}?access\_token={YOUR\_HUBSPOT\_ACCESS\_TOKEN}"

data = {

    "properties": \[

        {

            "name": "cvr",

            "value": cvr

        }

    ]

}

headers = {'content-type': 'application/json'}

requests.patch(url, json=data, headers=headers)

# Main function

def main():

# Get the recent companies

recent\_companies = get\_recent\_companies()

# Update the CVR of each recently created company

for company in recent\_companies\['results']:

    domain = company\["properties"]\["domain"]\["value"]

    cvr = get\_company\_cvr(domain)

    update\_company\_cvr(company\["companyId"], cvr)

Hey, @GKristjansson :waving_hand: Thanks for the questions. Out of curiosity, do the functions each work individually? Narrowing down exactly where it’s breaking down can help the community understand how and where they can assist.

Thank you! — Jaycee

Hi Jaycee

Yes the functions all work except for when I try to update the CVR number. I search a website for the CVR number (works) and it seems to work when I patch it. But when I check the profile of the company in HubSpot the CVR field is still empty.

Any idea what this might be @Jaycee_Lewis