{
  "name": "Round-Robin Lead Routing — HubSpot → Slack",
  "nodes": [
    {
      "parameters": {
        "eventsUi": { "eventValues": [{ "name": "contact.creation" }] }
      },
      "id": "hubspot-trigger",
      "name": "HubSpot Trigger",
      "type": "n8n-nodes-base.hubspotTrigger",
      "typeVersion": 1,
      "position": [0, 0],
      "webhookId": "hubspot-trigger-id",
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.hubapi.com/crm/v3/objects/contacts/{{ $json.objectId }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotApi",
        "sendQuery": true,
        "queryParameters": { "parameters": [{ "name": "properties", "value": "firstname,lastname,email,company,jobtitle,hubspot_owner_id" }] },
        "options": {}
      },
      "id": "fetch-contact",
      "name": "Fetch Contact",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const reps = [\n  { name: \"Alice\", hubspotOwnerId: \"12345678\", slackUserId: \"U01AAAA\" },\n  { name: \"Bob\",   hubspotOwnerId: \"23456789\", slackUserId: \"U02BBBB\" },\n  { name: \"Carol\", hubspotOwnerId: \"34567890\", slackUserId: \"U03CCCC\" },\n  { name: \"Dave\",  hubspotOwnerId: \"45678901\", slackUserId: \"U04DDDD\" },\n];\n\nconst staticData = $getWorkflowStaticData('global');\nconst currentIndex = staticData.roundRobinIndex || 0;\nconst assignedRep = reps[currentIndex % reps.length];\nstaticData.roundRobinIndex = (currentIndex + 1) % reps.length;\n\nconst contact = $('Fetch Contact').first().json;\n\nreturn [{\n  json: {\n    contactId: contact.id,\n    contactName: `${contact.properties.firstname || ''} ${contact.properties.lastname || ''}`.trim(),\n    contactEmail: contact.properties.email,\n    company: contact.properties.company,\n    title: contact.properties.jobtitle,\n    assignedRep: assignedRep,\n  }\n}];"
      },
      "id": "round-robin",
      "name": "Round Robin Assign",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "=https://api.hubapi.com/crm/v3/objects/contacts/{{ $json.contactId }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"properties\": { \"hubspot_owner_id\": \"{{ $json.assignedRep.hubspotOwnerId }}\" } }",
        "options": {}
      },
      "id": "update-owner",
      "name": "Update Owner",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [660, 0],
      "credentials": { "hubspotApi": { "id": "credential-id", "name": "HubSpot API" } }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": { "__rl": true, "value": "={{ $json.assignedRep.slackUserId }}", "mode": "id" },
        "messageType": "block",
        "blocksUi": "={\n  \"blocks\": [\n    {\n      \"type\": \"section\",\n      \"text\": {\n        \"type\": \"mrkdwn\",\n        \"text\": \"🆕 *New Lead Assigned to You*\\n*\" + $json.contactName + \"* — \" + ($json.title || 'No title') + \" at \" + ($json.company || 'Unknown company') + \"\\nEmail: \" + $json.contactEmail\n      }\n    },\n    {\n      \"type\": \"actions\",\n      \"elements\": [\n        {\n          \"type\": \"button\",\n          \"text\": { \"type\": \"plain_text\", \"text\": \"View in HubSpot\" },\n          \"url\": \"https://app.hubspot.com/contacts/YOUR_PORTAL_ID/contact/\" + $json.contactId\n        }\n      ]\n    }\n  ]\n}",
        "otherOptions": { "unfurl_links": false }
      },
      "id": "slack-dm",
      "name": "Slack DM Rep",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [880, 0],
      "credentials": { "slackApi": { "id": "credential-id", "name": "Slack API" } }
    }
  ],
  "connections": {
    "HubSpot Trigger": { "main": [[{ "node": "Fetch Contact", "type": "main", "index": 0 }]] },
    "Fetch Contact": { "main": [[{ "node": "Round Robin Assign", "type": "main", "index": 0 }]] },
    "Round Robin Assign": { "main": [[{ "node": "Update Owner", "type": "main", "index": 0 }]] },
    "Update Owner": { "main": [[{ "node": "Slack DM Rep", "type": "main", "index": 0 }]] }
  },
  "pinData": {},
  "settings": { "executionOrder": "v1" },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "active": false,
  "meta": { "instanceId": "", "templateId": "revi-round-robin" }
}
