{
  "id": "4B9LXs1yhViHbQOV",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI-Based Procurement Helpdesk Ticket Routing Workflow",
  "tags": [],
  "nodes": [
    {
      "id": "1c4e4523-59c4-4cbd-a291-d7790b68f448",
      "name": "New Procurement Email Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -2576,
        544
      ],
      "parameters": {
        "filters": {
          "labelIds": [
            "INBOX"
          ]
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ef0288e9-ddb3-468a-9ede-31bec953cdbf",
      "name": "Extract Email & Ticket Details",
      "type": "n8n-nodes-base.code",
      "position": [
        -2352,
        544
      ],
      "parameters": {
        "jsCode": "// \u2500\u2500 Enterprise Email Extractor for n8n Gmail Trigger \u2500\u2500\n\nconst item = $input.first().json;\n\n// Generate Ticket ID\nconst ticketId = `PRC-${new Date().getFullYear()}-${Math.floor(100000 + Math.random() * 900000)}`;\n\n// Extract fields directly from Gmail Trigger\nconst rawFrom =\n  item.From ||\n  item.from ||\n  'user@example.com';\n\nconst emailSubject =\n  item.Subject ||\n  item.subject ||\n  '(No Subject)';\n\nconst emailBody =\n  item.snippet ||\n  item.textPlain ||\n  item.textHtml ||\n  '';\n\n// Default values\nlet emailSender = rawFrom;\nlet senderName = 'Unknown';\n\n// Match: Name <email>\nconst match = rawFrom.match(/(.*)<(.*)>/);\n\nif (match) {\n\n  senderName = match[1]\n    .trim()\n    .replace(/\"/g, '');\n\n  emailSender = match[2]\n    .trim()\n    .replace('>', '');\n\n} else {\n\n  emailSender = rawFrom.trim();\n\n  // Generate readable sender name\n  senderName = emailSender\n    .split('@')[0]\n    .replace(/[._-]/g, ' ')\n    .replace(/\\b\\w/g, c => c.toUpperCase());\n}\n\n// Dates\nconst receivedAt = item.internalDate\n  ? new Date(Number(item.internalDate)).toISOString()\n  : new Date().toISOString();\n\n// IDs\nconst threadId =\n  item.threadId ||\n  `thread-${Date.now()}`;\n\nconst messageId =\n  item.id ||\n  `msg-${Date.now()}`;\n\nreturn [{\n  json: {\n\n    ticket_id: ticketId,\n\n    email_subject: emailSubject,\n\n    email_body: emailBody,\n\n    email_sender: emailSender,\n\n    sender_name: senderName,\n\n    received_at: receivedAt,\n\n    thread_id: threadId,\n\n    message_id: messageId\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "c2893f3e-fa63-441e-bf85-5b102aa2ea44",
      "name": "Check Existing Ticket Thread",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -2128,
        544
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "=={{ $json.thread_id }}",
              "lookupColumn": "=thread_id"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1994809169,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1s_ltcgxrd2B5sde2PdvryS5lIj-FGEsuPBTzPhOVGbs/edit#gid=1994809169",
          "cachedResultName": "Sheet2"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1s_ltcgxrd2B5sde2PdvryS5lIj-FGEsuPBTzPhOVGbs",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1s_ltcgxrd2B5sde2PdvryS5lIj-FGEsuPBTzPhOVGbs/edit?usp=drivesdk",
          "cachedResultName": "Helpdesk"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7,
      "alwaysOutputData": true
    },
    {
      "id": "8cc60992-1e8e-4cd0-a368-7d1d5875a6b0",
      "name": "Is This a New Ticket?",
      "type": "n8n-nodes-base.if",
      "position": [
        -1904,
        544
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "condition_dup",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $('Extract Email & Ticket Details').item.json.thread_id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "afd68921-1893-46c4-9728-e950b56298cc",
      "name": "AI Procurement Ticket Classifier",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -1680,
        544
      ],
      "parameters": {
        "text": "=You are an enterprise procurement helpdesk AI classifier.\nAnalyze the incoming procurement support request and classify it accurately.\n\nPossible categories:\n- Supplier Onboarding\n- Invoice Dispute\n- PO Issue\n- Contract Approval\n- Vendor Compliance\n- Payment Delay\n- Procurement Policy\n- Catalog Issue\n- Urgent Production Purchase\n- General Inquiry\n\nUrgency Levels: Low | Medium | High | Critical\nRisk Levels: Low | Medium | High\nSentiment: Positive | Neutral | Negative | Frustrated | Urgent\n\nTeams:\n- Procurement\n- AccountsPayable\n- SupplierManagement\n- Legal\n- Finance\n- Compliance\n- HelpDesk\n\nReturn ONLY valid JSON. No markdown. No explanation. confidence must be between 0 and 1. requires_human should be true if unclear or high-risk.\n\nJSON Format:\n{\n  \"category\": \"\",\n  \"urgency\": \"\",\n  \"sentiment\": \"\",\n  \"risk_level\": \"\",\n  \"team\": \"\",\n  \"summary\": \"\",\n  \"keywords\": [],\n  \"requires_human\": false,\n  \"confidence\": 0.0,\n  \"suggested_customer_reply\": \"\",\n  \"internal_agent_note\": \"\",\n  \"recommended_next_action\": \"\"\n}\n\nEmail Subject:\n{{ $json.email_subject }}\n\nEmail Body:\n{{ $json.email_body }}",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 3.1
    },
    {
      "id": "3e3b188a-94e2-4243-bd82-bff7ad5923ff",
      "name": "Groq Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        -1616,
        768
      ],
      "parameters": {
        "model": "llama-3.1-8b-instant",
        "options": {}
      },
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "00967a74-68f8-49ff-97f5-c86fc31e7ad6",
      "name": "Smart Routing & SLA Engine",
      "type": "n8n-nodes-base.code",
      "position": [
        -1328,
        544
      ],
      "parameters": {
        "jsCode": "// \u2500\u2500 Parse AI classification response \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nconst inputData = $input.first().json;\n\nlet aiResponseText = '';\nif (inputData.text)                                     aiResponseText = inputData.text;\nelse if (inputData.output)                              aiResponseText = inputData.output;\nelse if (inputData.content && Array.isArray(inputData.content))\n  aiResponseText = inputData.content.map(c => c.text || '').join('');\nelse                                                    aiResponseText = JSON.stringify(inputData);\n\naiResponseText = aiResponseText.replace(/```json\\n?/gi, '').replace(/```\\n?/gi, '').trim();\n\nlet classification = {};\ntry {\n  classification = JSON.parse(aiResponseText);\n} catch (e) {\n  classification = {\n    category: 'General Inquiry', urgency: 'Medium', sentiment: 'Neutral',\n    risk_level: 'Low', team: 'HelpDesk',\n    summary: 'Unable to classify - requires manual review',\n    keywords: [], requires_human: true, confidence: 0.0,\n    suggested_customer_reply: 'Thank you for reaching out. A member of our team will review your request shortly.',\n    internal_agent_note: 'Auto-classification failed. Requires manual review.',\n    recommended_next_action: 'Manually review and assign ticket.'\n  };\n}\n\nlet emailFields = {};\ntry { emailFields = $('Extract Email & Ticket Details').first().json; } catch(e) { emailFields = {}; }\n\n// \u2500\u2500 Routing Engine \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nconst category   = classification.category   || 'General Inquiry';\nconst urgency    = classification.urgency    || 'Low';\nconst risk       = classification.risk_level || 'Low';\nconst team       = classification.team       || 'HelpDesk';\nconst sentiment  = classification.sentiment  || 'Neutral';\nconst confidence = classification.confidence || 0.5;\n\nconst SLA_MAP = { Critical: 1, High: 4, Medium: 24, Low: 72 };\n\nconst QUEUE_MAP = {\n  SupplierManagement: 'queue-supplier-mgmt',\n  AccountsPayable   : 'queue-accounts-payable',\n  Procurement       : 'queue-procurement',\n  Legal             : 'queue-legal',\n  Finance           : 'queue-finance',\n  Compliance        : 'queue-compliance',\n  HelpDesk          : 'queue-general'\n};\n\nconst PRIORITY_SCORE = { Critical: 4, High: 3, Medium: 2, Low: 1 };\n\n// Risk escalation override\nlet effectiveUrgency = urgency;\nif (risk === 'High' && urgency !== 'Critical') effectiveUrgency = 'High';\n\n// Sentiment-based escalation: Negative + High \u2192 escalate\nif (sentiment === 'Negative' && effectiveUrgency === 'High') effectiveUrgency = 'Critical';\nif (sentiment === 'Frustrated' && effectiveUrgency === 'High') effectiveUrgency = 'Critical';\n\n// Low-confidence \u2192 manual review queue\nlet assignedTeam   = team;\nlet ticketStatus   = 'open';\nif (confidence < 0.65) {\n  classification.requires_human = true;\n  assignedTeam  = 'HelpDesk';\n  ticketStatus  = 'pending-human-review';\n}\n\nconst needsEscalation = (effectiveUrgency === 'Critical') || (risk === 'High');\n\nconst slaDeadline = new Date(Date.now() + (SLA_MAP[effectiveUrgency] || 24) * 3600 * 1000).toISOString();\n\nconst output = {\n  // Email fields\n  ticket_id    : emailFields.ticket_id    || `PRC-${new Date().getFullYear()}-${Math.floor(100000+Math.random()*900000)}`,\n  email_subject: emailFields.email_subject || '',\n  email_body   : emailFields.email_body   || '',\n  email_sender : emailFields.email_sender  || '',\n  sender_name  : emailFields.sender_name   || '',\n  received_at  : emailFields.received_at   || new Date().toISOString(),\n  thread_id    : emailFields.thread_id     || '',\n  message_id   : emailFields.message_id    || '',\n\n  // Classification\n  category    : category,\n  urgency     : urgency,\n  sentiment   : sentiment,\n  risk_level  : risk,\n  summary     : classification.summary  || '',\n  keywords    : (classification.keywords || []).join(', '),\n  requires_human: classification.requires_human || false,\n  confidence  : confidence,\n\n  // AI Copilot fields\n  suggested_customer_reply  : classification.suggested_customer_reply   || '',\n  internal_agent_note       : classification.internal_agent_note        || '',\n  recommended_next_action   : classification.recommended_next_action    || '',\n\n  // Routing decisions\n  assigned_queue    : QUEUE_MAP[assignedTeam] || 'queue-general',\n  assigned_team     : assignedTeam,\n  effective_urgency : effectiveUrgency,\n  sla_hours         : SLA_MAP[effectiveUrgency] || 24,\n  sla_deadline      : slaDeadline,\n  priority_score    : PRIORITY_SCORE[effectiveUrgency] || 2,\n  needs_escalation  : needsEscalation,\n  escalation_reason : needsEscalation ? `Urgency=${effectiveUrgency}, Risk=${risk}, Sentiment=${sentiment}` : null,\n\n  // Ticket metadata\n  ticket_status   : ticketStatus,\n  auto_assigned   : confidence >= 0.75,\n  routing_version : '2.0',\n  routed_at       : new Date().toISOString()\n};\n\nreturn [{ json: output }];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "fc2513ea-6310-4580-9d35-9be16ec4c804",
      "name": "Check Escalation Rules",
      "type": "n8n-nodes-base.if",
      "position": [
        -1104,
        544
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "condition_escalation",
              "operator": {
                "type": "boolean",
                "operation": "true"
              },
              "leftValue": "={{ $json.needs_escalation }}",
              "rightValue": true
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "ce8b8667-29e0-431f-9c1d-a239bb578217",
      "name": "Send Slack Escalation Alert",
      "type": "n8n-nodes-base.slack",
      "position": [
        -880,
        448
      ],
      "parameters": {
        "select": "channel",
        "blocksUi": "={{ JSON.stringify([\n  {\n    \"type\": \"header\",\n    \"text\": { \"type\": \"plain_text\", \"text\": \" PROCUREMENT ESCALATION ALERT\" }\n  },\n  {\n    \"type\": \"section\",\n    \"fields\": [\n      { \"type\": \"mrkdwn\", \"text\": \"*Ticket ID:*\\n\" + $json.ticket_id },\n      { \"type\": \"mrkdwn\", \"text\": \"*Category:*\\n\" + $json.category }\n    ]\n  },\n  {\n    \"type\": \"section\",\n    \"fields\": [\n      { \"type\": \"mrkdwn\", \"text\": \"*From:*\\n\" + $json.sender_name + \" (\" + $json.email_sender + \")\" },\n      { \"type\": \"mrkdwn\", \"text\": \"*Effective Urgency:*\\n\" + $json.effective_urgency }\n    ]\n  },\n  {\n    \"type\": \"section\",\n    \"fields\": [\n      { \"type\": \"mrkdwn\", \"text\": \"*Risk Level:*\\n\" + $json.risk_level },\n      { \"type\": \"mrkdwn\", \"text\": \"*Sentiment:*\\n\" + $json.sentiment }\n    ]\n  },\n  {\n    \"type\": \"section\",\n    \"fields\": [\n      { \"type\": \"mrkdwn\", \"text\": \"*Assigned Team:*\\n\" + $json.assigned_team },\n      { \"type\": \"mrkdwn\", \"text\": \"*SLA Deadline:*\\n\" + $json.sla_deadline }\n    ]\n  },\n  {\n    \"type\": \"section\",\n    \"text\": { \"type\": \"mrkdwn\", \"text\": \"*Subject:* \" + $json.email_subject }\n  },\n  {\n    \"type\": \"section\",\n    \"text\": { \"type\": \"mrkdwn\", \"text\": \"*AI Summary:* \" + $json.summary }\n  },\n  {\n    \"type\": \"section\",\n    \"text\": { \"type\": \"mrkdwn\", \"text\": \"*Recommended Next Action:* \" + $json.recommended_next_action }\n  },\n  {\n    \"type\": \"section\",\n    \"text\": { \"type\": \"mrkdwn\", \"text\": \"*SLA:* Respond within \" + $json.sla_hours + \" hour(s)\\n*Reason:* \" + $json.escalation_reason }\n  },\n  { \"type\": \"divider\" }\n]) }}",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "procurement-escalations"
        },
        "messageType": "block",
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "67bb4cf8-6577-4aff-af0d-1d2ef72d213e",
      "name": "Create Escalated Support Ticket",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -656,
        448
      ],
      "parameters": {
        "url": "https://jsonplaceholder.typicode.com/posts/1",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      },
      "typeVersion": 4
    },
    {
      "id": "c1816e7b-f872-4a69-bdf5-69a063ed4812",
      "name": "Create Standard Support Ticket",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -752,
        672
      ],
      "parameters": {
        "url": "https://jsonplaceholder.typicode.com/posts/1",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      },
      "typeVersion": 4
    },
    {
      "id": "3870300e-b8b7-49ec-b340-8f96727fb22b",
      "name": "Prepare Ticket Assignment Data",
      "type": "n8n-nodes-base.set",
      "position": [
        -400,
        544
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "03e7098a-aff0-4880-b7cf-16d31dec9ea8",
      "name": "AI Agent Dashboard Summary",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -208,
        544
      ],
      "parameters": {
        "text": "=Generate a short professional support ticket response summary for the agent dashboard.\n\nTicket ID: {{ $json.ticket_id }}\nCategory: {{ $json.category }}\nAssigned Team: {{ $json.assigned_team }}\nUrgency: {{ $json.effective_urgency }}\nRisk: {{ $json.risk_level }}\nSentiment: {{ $json.sentiment }}\nSLA Deadline: {{ $json.sla_deadline }}\nSummary: {{ $json.summary }}\nRecommended Next Action: {{ $json.recommended_next_action }}\n\nGenerate a concise professional status update under 80 words. Include: ticket status, assigned team, urgency, SLA deadline, and next action.",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 3.1
    },
    {
      "id": "04255a11-3a51-48a7-94ff-a68f56d4c48d",
      "name": "Groq Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        -144,
        768
      ],
      "parameters": {
        "model": "llama-3.1-8b-instant",
        "options": {}
      },
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0abdbb82-0275-4ae1-9ee0-6d295131ac33",
      "name": "Send Customer Auto Reply",
      "type": "n8n-nodes-base.gmail",
      "position": [
        144,
        544
      ],
      "parameters": {
        "sendTo": "={{ $('Smart Routing & SLA Engine').item.json.email_sender }}",
        "message": "=Dear {{ $('Smart Routing & SLA Engine').item.json.sender_name }},\n\nThank you for contacting the Procurement Helpdesk.\n\nYour request has been received and a ticket has been created for you.\n\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nTicket ID      : {{ $('Smart Routing & SLA Engine').item.json.ticket_id }}\nCategory       : {{ $('Smart Routing & SLA Engine').item.json.category }}\nAssigned Team  : {{ $('Smart Routing & SLA Engine').item.json.assigned_team }}\nPriority       : {{ $('Smart Routing & SLA Engine').item.json.effective_urgency }}\nSLA Response By: {{ $('Smart Routing & SLA Engine').item.json.sla_deadline }}\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\n{{ $('Smart Routing & SLA Engine').item.json.suggested_customer_reply }}\n\nIf you have any additional information to add, please reply to this email.\n\nBest regards,\nProcurement Helpdesk Team",
        "options": {},
        "subject": "=[Ticket #{{ $('Smart Routing & SLA Engine').item.json.ticket_id }}] We've received your procurement request",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "027ad8c3-76f8-4a05-a27d-05968fbdbfa4",
      "name": "Log Ticket into Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        368,
        544
      ],
      "parameters": {
        "columns": {
          "value": {
            "summary": "={{ $('Smart Routing & SLA Engine').item.json.summary }}",
            "urgency": "={{ $('Smart Routing & SLA Engine').item.json.urgency }}",
            "category": "={{ $('Smart Routing & SLA Engine').item.json.category }}",
            "keywords": "={{ $('Smart Routing & SLA Engine').item.json.keywords }}",
            "routed_at": "={{ $('Smart Routing & SLA Engine').item.json.routed_at }}",
            "sentiment": "={{ $('Smart Routing & SLA Engine').item.json.sentiment }}",
            "sla_hours": "={{ $('Smart Routing & SLA Engine').item.json.sla_hours }}",
            "thread_id": "={{ $('Smart Routing & SLA Engine').item.json.thread_id }}",
            "ticket_id": "={{ $('Smart Routing & SLA Engine').item.json.ticket_id }}",
            "confidence": "={{ $('Smart Routing & SLA Engine').item.json.confidence }}",
            "risk_level": "={{ $('Smart Routing & SLA Engine').item.json.risk_level }}",
            "sender_name": "={{ $('Smart Routing & SLA Engine').item.json.sender_name }}",
            "email_sender": "={{ $('Smart Routing & SLA Engine').item.json.email_sender }}",
            "sla_deadline": "={{ $('Smart Routing & SLA Engine').item.json.sla_deadline }}",
            "agent_summary": "={{ $json.output }}",
            "assigned_team": "={{ $('Smart Routing & SLA Engine').item.json.assigned_team }}",
            "email_subject": "={{ $('Smart Routing & SLA Engine').item.json.email_subject }}",
            "ticket_status": "={{ $('Smart Routing & SLA Engine').item.json.ticket_status }}",
            "assigned_queue": "={{ $('Smart Routing & SLA Engine').item.json.assigned_queue }}",
            "priority_score": "={{ $('Smart Routing & SLA Engine').item.json.priority_score }}",
            "requires_human": "={{ $('Smart Routing & SLA Engine').item.json.requires_human }}",
            "needs_escalation": "={{ $('Smart Routing & SLA Engine').item.json.needs_escalation }}",
            "effective_urgency": "={{ $('Smart Routing & SLA Engine').item.json.effective_urgency }}",
            "internal_agent_note": "={{ $('Smart Routing & SLA Engine').item.json.internal_agent_note }}",
            "recommended_next_action": "={{ $('Smart Routing & SLA Engine').item.json.recommended_next_action }}",
            "suggested_customer_reply": "={{ $('Smart Routing & SLA Engine').item.json.suggested_customer_reply }}"
          },
          "schema": [
            {
              "id": "ticket_id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ticket_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "email_subject",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "email_subject",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "email_sender",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "email_sender",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "sender_name",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "sender_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "thread_id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "thread_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "category",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "category",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "urgency",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "urgency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "effective_urgency",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "effective_urgency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "risk_level",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "risk_level",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "sentiment",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "sentiment",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "assigned_team",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "assigned_team",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "assigned_queue",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "assigned_queue",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "priority_score",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "priority_score",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "sla_hours",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "sla_hours",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "sla_deadline",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "sla_deadline",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "needs_escalation",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "needs_escalation",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "requires_human",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "requires_human",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "confidence",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "confidence",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "keywords",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "keywords",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "summary",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "summary",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "suggested_customer_reply",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "suggested_customer_reply",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "internal_agent_note",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "internal_agent_note",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "recommended_next_action",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "recommended_next_action",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ticket_status",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ticket_status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "routed_at",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "routed_at",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "agent_summary",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "agent_summary",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1s_ltcgxrd2B5sde2PdvryS5lIj-FGEsuPBTzPhOVGbs/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1s_ltcgxrd2B5sde2PdvryS5lIj-FGEsuPBTzPhOVGbs",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1s_ltcgxrd2B5sde2PdvryS5lIj-FGEsuPBTzPhOVGbs/edit?usp=drivesdk",
          "cachedResultName": "Helpdesk"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4
    },
    {
      "id": "90146595-1da2-4605-947b-074c16d1f78c",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2608,
        -272
      ],
      "parameters": {
        "width": 624,
        "height": 576,
        "content": "#### How it works\nThis workflow acts as an AI-powered Procurement Helpdesk system that automates the complete support process for incoming procurement emails. It continuously monitors the support mailbox, extracts important ticket details such as sender information, subject, email content, and thread ID, and checks for duplicate requests before creating a new ticket.\n\nThe email is then analyzed using AI to identify the request category, urgency, sentiment, risk level, and the correct internal team responsible for handling the issue. Based on the AI analysis, the workflow automatically assigns priority levels, calculates SLA deadlines, and routes tickets accordingly.\n\nFor critical or high-risk requests, instant escalation alerts are sent to Slack for faster response handling. Customers automatically receive acknowledgement emails, while all ticket details, AI decisions, and support activity are logged into Google Sheets for tracking, reporting, and audit purposes.\n\n## Setup steps \n1. Connect Gmail OAuth\n2. Connect Groq AI API\n3. Connect Slack OAuth\n4. Connect Google Sheets OAuth\n5. Replace demo HTTP APIs with Freshdesk/Jira/ServiceNow APIs\n6. Create Slack channel: procurement-escalations\n7. Add required Google Sheet columns before activation"
      },
      "typeVersion": 1
    },
    {
      "id": "a366bcd9-cf82-45e6-9109-585c1c1e1351",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2624,
        416
      ],
      "parameters": {
        "color": 7,
        "width": 880,
        "height": 368,
        "content": "## Email Intake & Validation\nReads incoming Gmail messages, extracts sender/ticket details, checks for duplicate thread IDs, and allows only new tickets to continue into AI processing."
      },
      "typeVersion": 1
    },
    {
      "id": "0eb44083-f6c8-4afd-864a-ef38a601f704",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1728,
        304
      ],
      "parameters": {
        "color": 7,
        "width": 768,
        "height": 592,
        "content": "## AI Ticket Analysis & Smart Assignment\nAI classifies the request type, identifies urgency and risk, calculates SLA deadlines, and routes the ticket to the correct support team automatically."
      },
      "typeVersion": 1
    },
    {
      "id": "2d034b20-e874-48a5-bc86-cc78da470338",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -928,
        304
      ],
      "parameters": {
        "color": 7,
        "width": 448,
        "height": 592,
        "content": "## Priority Escalation Management\nCritical or high-risk procurement requests trigger instant Slack escalation alerts and are forwarded to the ticketing system with higher priority handling."
      },
      "typeVersion": 1
    },
    {
      "id": "8484cc9e-5144-4a29-975f-8249e98188ad",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -448,
        336
      ],
      "parameters": {
        "color": 7,
        "width": 960,
        "height": 560,
        "content": "## Customer Notification & Ticket Records\nGenerates internal support summaries, sends professional acknowledgement emails to customers, and stores ticket details for reporting and operational tracking."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "5306e7a6-6488-437a-a48f-3024e8cf2e18",
  "connections": {
    "Groq Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Procurement Ticket Classifier",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Groq Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent Dashboard Summary",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Is This a New Ticket?": {
      "main": [
        [
          {
            "node": "AI Procurement Ticket Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Escalation Rules": {
      "main": [
        [
          {
            "node": "Send Slack Escalation Alert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Standard Support Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Customer Auto Reply": {
      "main": [
        [
          {
            "node": "Log Ticket into Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent Dashboard Summary": {
      "main": [
        [
          {
            "node": "Send Customer Auto Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Smart Routing & SLA Engine": {
      "main": [
        [
          {
            "node": "Check Escalation Rules",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Slack Escalation Alert": {
      "main": [
        [
          {
            "node": "Create Escalated Support Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Existing Ticket Thread": {
      "main": [
        [
          {
            "node": "Is This a New Ticket?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New Procurement Email Trigger": {
      "main": [
        [
          {
            "node": "Extract Email & Ticket Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Standard Support Ticket": {
      "main": [
        [
          {
            "node": "Prepare Ticket Assignment Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Email & Ticket Details": {
      "main": [
        [
          {
            "node": "Check Existing Ticket Thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Ticket Assignment Data": {
      "main": [
        [
          {
            "node": "AI Agent Dashboard Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Escalated Support Ticket": {
      "main": [
        [
          {
            "node": "Prepare Ticket Assignment Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Procurement Ticket Classifier": {
      "main": [
        [
          {
            "node": "Smart Routing & SLA Engine",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}