{
  "name": "Salesforce Lead Round-Robin — Salesforce → Slack",
  "nodes": [
    {
      "parameters": { "resource": "Lead", "event": "created", "pollTimes": { "item": [{ "mode": "everyMinute" }] } },
      "id": "sf-trigger", "name": "Salesforce Trigger",
      "type": "n8n-nodes-base.salesforceTrigger", "typeVersion": 1, "position": [0, 0],
      "credentials": { "salesforceOAuth2Api": { "id": "credential-id", "name": "Salesforce OAuth2" } }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://YOUR_INSTANCE.salesforce.com/services/data/v59.0/sobjects/Lead/{{ $json.Id }}",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "salesforceOAuth2Api",
        "options": {}
      },
      "id": "fetch-lead", "name": "Fetch Lead",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [220, 0],
      "credentials": { "salesforceOAuth2Api": { "id": "credential-id", "name": "Salesforce OAuth2" } }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const reps = [\n  { name: \"Alice\", sfUserId: \"005xx0000012345\", slackUserId: \"U01AAAA\" },\n  { name: \"Bob\",   sfUserId: \"005xx0000023456\", slackUserId: \"U02BBBB\" },\n  { name: \"Carol\", sfUserId: \"005xx0000034567\", slackUserId: \"U03CCCC\" },\n  { name: \"Dave\",  sfUserId: \"005xx0000045678\", 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 lead = $('Fetch Lead').first().json;\n\nreturn [{\n  json: {\n    leadId: lead.Id,\n    leadName: `${lead.FirstName || ''} ${lead.LastName || ''}`.trim(),\n    leadEmail: lead.Email,\n    company: lead.Company,\n    leadSource: lead.LeadSource,\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://YOUR_INSTANCE.salesforce.com/services/data/v59.0/sobjects/Lead/{{ $json.leadId }}",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "salesforceOAuth2Api",
        "sendBody": true, "specifyBody": "json",
        "jsonBody": "={ \"OwnerId\": \"{{ $json.assignedRep.sfUserId }}\" }",
        "options": {}
      },
      "id": "update-owner", "name": "Update Lead Owner",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [660, 0],
      "credentials": { "salesforceOAuth2Api": { "id": "credential-id", "name": "Salesforce OAuth2" } }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR_INSTANCE.salesforce.com/services/data/v59.0/sobjects/Task",
        "authentication": "predefinedCredentialType", "nodeCredentialType": "salesforceOAuth2Api",
        "sendBody": true, "specifyBody": "json",
        "jsonBody": "={\n  \"Subject\": \"New lead assigned — review within 5 minutes\",\n  \"WhoId\": \"{{ $json.leadId }}\",\n  \"OwnerId\": \"{{ $json.assignedRep.sfUserId }}\",\n  \"Status\": \"Not Started\",\n  \"Priority\": \"High\",\n  \"ActivityDate\": \"{{ $now.format('yyyy-MM-dd') }}\"\n}",
        "options": {}
      },
      "id": "create-task", "name": "Create Follow-Up Task",
      "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [880, 0],
      "credentials": { "salesforceOAuth2Api": { "id": "credential-id", "name": "Salesforce OAuth2" } }
    },
    {
      "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 Salesforce Lead Assigned to You*\\n*\" + $json.leadName + \"* — \" + ($json.company || 'Unknown company') + \"\\nEmail: \" + ($json.leadEmail || 'N/A') + \"\\nSource: \" + ($json.leadSource || 'N/A')\n      }\n    },\n    {\n      \"type\": \"actions\",\n      \"elements\": [{ \"type\": \"button\", \"text\": { \"type\": \"plain_text\", \"text\": \"View in Salesforce\" }, \"url\": \"https://YOUR_INSTANCE.lightning.force.com/lightning/r/Lead/\" + $json.leadId + \"/view\" }]\n    }\n  ]\n}",
        "otherOptions": { "unfurl_links": false }
      },
      "id": "slack-dm", "name": "Slack DM Rep",
      "type": "n8n-nodes-base.slack", "typeVersion": 2.3, "position": [1100, 0],
      "credentials": { "slackApi": { "id": "credential-id", "name": "Slack API" } }
    }
  ],
  "connections": {
    "Salesforce Trigger": { "main": [[{ "node": "Fetch Lead", "type": "main", "index": 0 }]] },
    "Fetch Lead": { "main": [[{ "node": "Round Robin Assign", "type": "main", "index": 0 }]] },
    "Round Robin Assign": { "main": [[{ "node": "Update Lead Owner", "type": "main", "index": 0 }]] },
    "Update Lead Owner": { "main": [[{ "node": "Create Follow-Up Task", "type": "main", "index": 0 }]] },
    "Create Follow-Up Task": { "main": [[{ "node": "Slack DM Rep", "type": "main", "index": 0 }]] }
  },
  "pinData": {}, "settings": { "executionOrder": "v1" },
  "staticData": null, "tags": [], "triggerCount": 0, "active": false,
  "meta": { "instanceId": "", "templateId": "revi-sf-lead-routing" }
}
