{
  "name": "Ticket Triage — Zendesk Trigger → Tag & Assign Group",
  "nodes": [
    {
      "parameters": {
        "event": "ticketCreated"
      },
      "id": "zendesk-trigger",
      "name": "Zendesk Trigger",
      "type": "n8n-nodes-base.zendeskTrigger",
      "typeVersion": 1,
      "position": [0, 0],
      "credentials": {
        "zendeskApi": {
          "id": "credential-id",
          "name": "Zendesk API"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const ticket = $input.first().json;\nconst subject = (ticket.subject || '').toLowerCase();\nconst description = (ticket.description || '').toLowerCase();\nconst text = `${subject} ${description}`;\n\nconst topicMap = {\n  'topic-billing': ['invoice', 'charge', 'billing', 'payment', 'receipt', 'subscription', 'refund'],\n  'topic-shipping': ['tracking', 'delivery', 'shipping', 'package', 'lost', 'delayed', 'transit'],\n  'topic-product': ['broken', 'defective', 'quality', 'size', 'color', 'damaged', 'wrong item'],\n  'topic-account': ['password', 'login', 'account', 'access', 'locked', 'sign in', 'reset'],\n  'topic-technical': ['error', 'bug', 'crash', 'not loading', 'api', 'integration', 'timeout'],\n};\n\nconst groupMap = {\n  'topic-billing': 'Billing Support',\n  'topic-shipping': 'Shipping Support',\n  'topic-product': 'Product Support',\n  'topic-account': 'Account Support',\n  'topic-technical': 'Technical Support',\n};\n\nconst matchedTopics = [];\nfor (const [tag, keywords] of Object.entries(topicMap)) {\n  if (keywords.some(kw => text.includes(kw))) {\n    matchedTopics.push(tag);\n  }\n}\n\nconst primaryTopic = matchedTopics[0] || 'topic-uncategorized';\n\nreturn [{\n  json: {\n    ticketId: ticket.id,\n    tags: matchedTopics.length > 0 ? matchedTopics : ['topic-uncategorized'],\n    group: groupMap[primaryTopic] || 'General Support',\n    matchedTopics,\n  }\n}];"
      },
      "id": "classify-ticket",
      "name": "Classify Ticket",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://YOUR_SUBDOMAIN.zendesk.com/api/v2/groups.json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "zendeskApi",
        "options": {}
      },
      "id": "fetch-groups",
      "name": "Fetch Groups",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 0],
      "credentials": {
        "zendeskApi": {
          "id": "credential-id",
          "name": "Zendesk API"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const groups = $('Fetch Groups').first().json.groups;\nconst targetGroup = $('Classify Ticket').first().json.group;\nconst classification = $('Classify Ticket').first().json;\n\nconst groupId = groups.find(g => g.name === targetGroup)?.id;\n\nreturn [{\n  json: {\n    ...classification,\n    groupId: groupId || null,\n  }\n}];"
      },
      "id": "map-group-id",
      "name": "Map Group ID",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 0]
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "=https://YOUR_SUBDOMAIN.zendesk.com/api/v2/tickets/{{ $json.ticketId }}.json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "zendeskApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"ticket\": {\n    \"tags\": {{ JSON.stringify($json.tags) }},\n    \"group_id\": {{ $json.groupId }}\n  }\n}",
        "options": {}
      },
      "id": "update-ticket",
      "name": "Update Ticket",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, 0],
      "credentials": {
        "zendeskApi": {
          "id": "credential-id",
          "name": "Zendesk API"
        }
      }
    }
  ],
  "connections": {
    "Zendesk Trigger": {
      "main": [
        [
          {
            "node": "Classify Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify Ticket": {
      "main": [
        [
          {
            "node": "Fetch Groups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Groups": {
      "main": [
        [
          {
            "node": "Map Group ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map Group ID": {
      "main": [
        [
          {
            "node": "Update Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "active": false,
  "meta": {
    "instanceId": "",
    "templateId": "revi-zendesk-ticket-triage"
  }
}
