Revenue Ops
Python
Webhook Trigger

Tiered Commission & Quota Split Calculator (Python)

Calculate multi-tier commission rates (e.g. 8% base, 12% accelerator) when a deal is won 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)

calculate-rep-commission-tiers-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')

    base_rate = float(input_data.get('base_rate', 0.08))
    accelerator_rate = float(input_data.get('accelerator_rate', 0.12))
    tier_threshold = float(input_data.get('tier_threshold', 50000))

    deal_list = []
    if deal_id:
        deal = hs_client.crm.deals.basic_api.get_by_id(deal_id, properties=['dealname', 'amount', 'dealstage', 'closedate'])
        deal_list.append({"id": deal_id, "amount": float(deal.properties.get('amount') or 0)})
    else:
        # Trigger-agnostic fallback: Scan recent closed-won deals
        won = hs_client.crm.deals.basic_api.get_page(
            limit=25,
            properties=['dealname', 'amount', 'dealstage', 'closedate'],
            filters=[{"value": "closedwon", "propertyName": "dealstage", "operator": "EQ"}]
        )
        for d in (won.results or []):
            deal_list.append({"id": d.id, "amount": float(d.properties.get('amount') or 0)})

    total_sum = 0.0
    for item in deal_list:
        amt = item["amount"]
        if amt <= 0:
            continue
        if amt <= tier_threshold:
            b_comm = amt * base_rate
            a_comm = 0.0
        else:
            b_comm = tier_threshold * base_rate
            a_comm = (amt - tier_threshold) * accelerator_rate

        t_comm = round(b_comm + a_comm, 2)
        eff_rate = round((t_comm / amt) * 100, 2)
        total_sum += t_comm

        hs_client.crm.deals.basic_api.update(item["id"], properties={
            "commission_payout": str(t_comm),
            "effective_commission_rate": f"{eff_rate}%",
            "commission_calculated_at": datetime.datetime.utcnow().isoformat() + "Z"
        })

    return {
        "dealId": deal_id,
        "totalCommission": round(total_sum, 2),
        "processedDeals": len(deal_list)
    }

Configurable Parameters

ParameterTypeDefaultDescription
baseRatenumber8Base commission rate applied up to the tier threshold.
tierThresholdnumber50000Deal revenue threshold where accelerated commission rate starts.
acceleratorRatenumber12Elevated commission percentage for amount exceeding threshold.

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.