Data Quality
Python
Webhook Trigger

Auto-Create & Associate Company from Domain (Python)

Extract corporate domain from newly created contacts, look up or create matching Company, and link them using Python.

Why this automation is needed

Native HubSpot workflows often struggle with complex multi-object traversals, conditional lookback attribution windows, custom phone format standardizations, and custom scoring calculations.

With Workflood, this recipe runs in a secure gVisor container sandbox with pre-authenticated HubSpot SDK injection, avoiding HubSpot Data Hub execution limits, memory bottlenecks, and per-seat fees.

Action Source Code (Python)

sync-company-domain-enrichment-python.py
Python
def main(input_data, hs_client, http_client, secret, console):
    raw = input_data[0] if isinstance(input_data, list) and input_data else (input_data or {})
    contact_id = raw.get('contact_id') or raw.get('contactId') or raw.get('objectId') or raw.get('hs_object_id')
    email = raw.get('email')

    freemail = {
        'gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'icloud.com',
        'aol.com', 'protonmail.com', 'zoho.com', 'mail.com', 'gmx.com'
    }

    if contact_id and not email:
        c = hs_client.crm.contacts.basic_api.get_by_id(contact_id, properties=['email'])
        email = c.properties.get('email')

    # Trigger-agnostic fallback: Scan recent contacts
    if not email:
        recent = hs_client.crm.contacts.basic_api.get_page(limit=25, properties=['email'])
        enriched = 0
        for c in (recent.results or []):
            c_email = c.properties.get('email')
            if not c_email or '@' not in c_email:
                continue
            c_domain = c_email.split('@')[1].lower().strip()
            if c_domain in freemail:
                continue
            existing = hs_client.crm.companies.basic_api.get_page(
                limit=1,
                properties=['name', 'domain'],
                filters=[{"value": c_domain, "propertyName": "domain", "operator": "EQ"}]
            )
            if existing.results:
                comp_id = existing.results[0].id
            else:
                c_name = c_domain.split('.')[0].capitalize()
                new_comp = hs_client.crm.companies.basic_api.create(properties={"name": c_name, "domain": c_domain})
                comp_id = new_comp.id
            hs_client.crm.associations.v4.basic_api.create(
                from_object_type='contacts',
                object_id=c.id,
                to_object_type='companies',
                to_object_id=comp_id,
                types=[{"associationCategory": "HUBSPOT_DEFINED", "associationTypeId": 1}]
            )
            enriched += 1
        return {"associated": enriched > 0, "message": f"Trigger-agnostic scan: enriched {enriched} contact(s)"}

    domain = email.split('@')[1].lower().strip()
    if domain in freemail:
        return {"associated": False, "domain": domain, "message": f"Skipped freemail: {domain}"}

    existing = hs_client.crm.companies.basic_api.get_page(
        limit=1,
        properties=['name', 'domain'],
        filters=[{"value": domain, "propertyName": "domain", "operator": "EQ"}]
    )

    if existing.results:
        company_id = existing.results[0].id
    else:
        name = domain.split('.')[0].capitalize()
        company = hs_client.crm.companies.basic_api.create(properties={"name": name, "domain": domain})
        company_id = company.id

    if contact_id:
        hs_client.crm.associations.v4.basic_api.create(
            from_object_type='contacts',
            object_id=contact_id,
            to_object_type='companies',
            to_object_id=company_id,
            types=[{"associationCategory": "HUBSPOT_DEFINED", "associationTypeId": 1}]
        )

    return {"associated": True, "companyId": company_id, "domain": domain, "message": f"Linked to company {company_id}"}

How to deploy in Workflood

1

Connect your HubSpot Portal

Add your HubSpot Private App token once. Workflood securely encrypts it via libsodium XChaCha20 and injects pre-authenticated SDK clients.

2

Create Action from Template

Select this recipe in Workflood. Edit variables, customize logic in Monaco editor, or execute dry runs with sample JSON payloads.

3

Trigger via Webhook or Cron

Trigger from standard HubSpot workflows via Webhook action, or schedule recurring batch syncs with cron expressions and timezone handling.

Ready to run

Deploy in Workflood

Single Portal is $79/mo flat with unlimited team seats, 50,000 runs/mo, and no per-seat costs.

Node 20 & Python 3.11 runtimes
gVisor micro-container sandbox
60s execution timeout (vs HubSpot 20s)
Two-way GitHub repository sync
Instant rollback & version history