Revenue Ops
Python
Webhook Trigger

Multi-Currency Deal FX Exchange Rate Converter (Python)

Convert foreign currency deal values to base currency (USD) and populate amount_in_home_currency in 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)

multi-currency-exchange-converter-python.py
Python
import datetime

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 {})
    deal_id = raw.get('deal_id') or raw.get('dealId') or raw.get('objectId') or raw.get('hs_object_id')

    home_currency = (input_data.get('home_currency') or 'USD').upper()
    usd_rates = {
        'USD': 1.0, 'EUR': 1.09, 'GBP': 1.28, 'CAD': 0.73,
        'AUD': 0.66, 'JPY': 0.0068, 'CHF': 1.14, 'SGD': 0.76
    }
    target_rate = usd_rates.get(home_currency, 1.0)

    deal_list = []
    if deal_id:
        amount = float(input_data.get('amount') or 0)
        deal_currency = (input_data.get('deal_currency') or '').upper()
        if not amount or not deal_currency:
            deal = hs_client.crm.deals.basic_api.get_by_id(deal_id, properties=['amount', 'deal_currency_code', 'hs_deal_currency_code'])
            amount = amount or float(deal.properties.get('amount') or 0)
            deal_currency = deal_currency or (deal.properties.get('deal_currency_code') or deal.properties.get('hs_deal_currency_code') or 'USD').upper()
        deal_list.append({"id": deal_id, "amount": amount, "currency": deal_currency})
    else:
        # Trigger-agnostic fallback: Scan recent deals
        recent = hs_client.crm.deals.basic_api.get_page(
            limit=25,
            properties=['dealname', 'amount', 'deal_currency_code', 'hs_deal_currency_code']
        )
        for d in (recent.results or []):
            amt = float(d.properties.get('amount') or 0)
            curr = (d.properties.get('deal_currency_code') or d.properties.get('hs_deal_currency_code') or 'USD').upper()
            if amt > 0:
                deal_list.append({"id": d.id, "amount": amt, "currency": curr})

    for item in deal_list:
        source_rate = usd_rates.get(item["currency"], 1.0)
        rate_multiplier = source_rate / target_rate
        converted_amount = round(item["amount"] * rate_multiplier, 2)

        hs_client.crm.deals.basic_api.update(item["id"], properties={
            "amount_in_home_currency": str(converted_amount),
            "fx_rate_applied": f"{rate_multiplier:.4f}",
            "fx_calculated_at": datetime.datetime.utcnow().isoformat() + "Z"
        })

    first = deal_list[0] if deal_list else None
    first_rate = (usd_rates.get(first["currency"], 1.0) / target_rate) if first else 1.0

    return {
        "dealId": deal_id,
        "homeCurrency": home_currency,
        "convertedAmount": round(first["amount"] * first_rate, 2) if first and len(deal_list) == 1 else None,
        "processedDeals": len(deal_list)
    }

Configurable Parameters

ParameterTypeDefaultDescription
homeCurrencyselectUSDTarget currency for standardized company-wide pipeline reporting.

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.