{
  "name": "Expansion Signal Router",
  "nodes": [
    {
      "id": "d1cfa52b-35d0-4ec2-a77f-3ff2af932d69",
      "name": "Daily Signal Check",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        100,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "20 8 * * 1-5"
            }
          ]
        }
      }
    },
    {
      "id": "6b41a564-9208-44cd-8cb4-624e9a9afeb8",
      "name": "Get Customers",
      "type": "n8n-nodes-base.salesforce",
      "typeVersion": 1.1,
      "position": [
        300,
        300
      ],
      "parameters": {
        "resource": "search",
        "operation": "query",
        "query": "SELECT Amount, Account.Name FROM Opportunity WHERE IsWon = true"
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "553Tae1kPe5Wpthq",
          "name": "Salesforce account"
        }
      },
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "id": "c02c7078-aa6b-4c01-820d-0ddbf8e50a67",
      "name": "Get Recent Opportunities",
      "type": "n8n-nodes-base.salesforce",
      "typeVersion": 1.1,
      "position": [
        500,
        300
      ],
      "parameters": {
        "resource": "search",
        "operation": "query",
        "query": "SELECT Id, Name, StageName, CreatedDate, Account.Name FROM Opportunity WHERE CreatedDate = LAST_N_DAYS:180"
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "553Tae1kPe5Wpthq",
          "name": "Salesforce account"
        }
      },
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "id": "323e76c6-9f75-413f-9cf9-904e5bd86eeb",
      "name": "Get Account Team",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        700,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "2kpKzQwXkV4jVof7",
          "cachedResultName": "Account Team"
        },
        "returnAll": true
      },
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "id": "c71fdcbb-01c2-4003-b347-bb13a9e8e54d",
      "name": "Get Capacity",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        900,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "60uwN3kUzurpgdL3",
          "cachedResultName": "Account Capacity"
        },
        "returnAll": true
      },
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "id": "3a43ae48-26c9-44e5-bb24-03d06a12cad2",
      "name": "Get Team Activity",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "WMZnzywKrcBe7BRO",
          "cachedResultName": "Team Activity"
        },
        "returnAll": true
      },
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "id": "fc699b4c-1440-4ad5-b2ce-0aa042260709",
      "name": "Get Touchpoints",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        1300,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "4VwFmA1iJIEW3N3N",
          "cachedResultName": "Customer Touchpoints"
        },
        "returnAll": true
      },
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "id": "ee28f983-0281-4703-8e07-253358ff27da",
      "name": "Get Signal Log",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        1500,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "C3sCIYc3A0X5bymr",
          "cachedResultName": "Expansion Signal Log"
        },
        "returnAll": true
      },
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "id": "fd0f98d6-3a06-4765-ad49-e9053baef89a",
      "name": "Get Goal Drift Log",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        1700,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "6KqiVeFiUb8jQuCn",
          "cachedResultName": "Goal Drift Log"
        },
        "returnAll": true
      },
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "id": "9ec96ca0-34c9-4a51-a78b-375d24875e14",
      "name": "Detect Signals",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1900,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Detect expansion signals that are pure data, in code: capacity at 90%+ of what is licensed, and a team that started\n// using a product in the last 45 days. Collect the last 60 days of notes for Claude to read for stated intent.\nconst runDate = $today.toFormat('yyyy-MM-dd');\nconst back = n => $today.minus({ days: n }).toFormat('yyyy-MM-dd');\nconst won = $('Get Customers').all().map(i => i.json).filter(o => o.Account && o.Account.Name);\nconst value = {}; for (const o of won) value[o.Account.Name] = (value[o.Account.Name] || 0) + (Number(o.Amount) || 0);\nconst isCustomer = a => value[a] > 0;\nconst codeSignals = [];\nfor (const c of $('Get Capacity').all().map(i => i.json).filter(r => r.accountName && isCustomer(r.accountName))) {\n  const pct = Number(c.used) / Number(c.licensed);\n  if (pct >= 0.9) codeSignals.push({ accountName: c.accountName, type: 'capacity_cap', key: c.product,\n    evidence: c.product + ' at ' + Math.round(pct * 100) + '% of licensed ' + c.unit + ' (' + c.used + ' of ' + c.licensed + ', ' + c.month + ')' });\n}\nfor (const t of $('Get Team Activity').all().map(i => i.json).filter(r => r.accountName && isCustomer(r.accountName))) {\n  if (t.firstActive >= back(45)) codeSignals.push({ accountName: t.accountName, type: 'new_team', key: t.team,\n    evidence: 'New team on ' + t.product + ': ' + t.team + ' (' + t.activeUsers + ' active users since ' + t.firstActive + ')' });\n}\nconst windowStart = back(60);\nconst notes = $('Get Touchpoints').all().map(i => i.json).filter(n => n.accountName && isCustomer(n.accountName) && n.date >= windowStart);\nconst byAccount = {};\nfor (const n of notes) (byAccount[n.accountName] = byAccount[n.accountName] || []).push({ date: n.date, type: n.type, attendees: n.attendees, notes: n.notes });\nreturn [{ json: { runDate, windowStart, value, codeSignals, notesByAccount: Object.entries(byAccount).map(([accountName, notes]) => ({ accountName, notes })) } }];\n"
      },
      "executeOnce": true
    },
    {
      "id": "f8ddfeb3-6904-4a6e-ac75-af6bad94b766",
      "name": "Expansion Reader",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        300,
        450
      ],
      "parameters": {
        "promptType": "define",
        "text": "={{ JSON.stringify($json.notesByAccount, null, 1) }}",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "You read recent customer notes for Fortavault, Inc. (a fictional data protection company) and find expansion signals: concrete signs the customer may buy more. Count a signal only for a concrete plan or ask: adding new sites, clinics, plants or business units; a new team that wants the product; a question about pricing or licensing for more; budget approved for more. Do not count vague interest ('may look at X next year'), cost cutting, renewals of what they already have, or problems with the current product.\n\nFor each signal return accountName (exactly as given), what (one short plain sentence), quote (the exact words from the note that show it, copied word for word) and date (that note's date). Return an empty list if there are none."
        }
      },
      "executeOnce": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "id": "0dc049ce-e5b3-4920-8aae-77e8857d6a3d",
      "name": "Claude Sonnet",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1.3,
      "position": [
        100,
        450
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "claude-sonnet-4-6",
          "cachedResultName": "Claude Sonnet 4.6"
        },
        "options": {
          "temperature": 0
        }
      },
      "credentials": {
        "anthropicApi": {
          "id": "3d3ZiTi7BwnkFwqU",
          "name": "Anthropic account"
        }
      }
    },
    {
      "id": "4788f16a-3853-43a8-b867-9b4d4728f5b9",
      "name": "Signal Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        100,
        600
      ],
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\n \"type\": \"object\",\n \"properties\": {\n  \"signals\": {\n   \"type\": \"array\",\n   \"items\": {\n    \"type\": \"object\",\n    \"properties\": {\n     \"accountName\": {\n      \"type\": \"string\"\n     },\n     \"what\": {\n      \"type\": \"string\"\n     },\n     \"quote\": {\n      \"type\": \"string\"\n     },\n     \"date\": {\n      \"type\": \"string\"\n     }\n    },\n    \"required\": [\n     \"accountName\",\n     \"what\",\n     \"quote\",\n     \"date\"\n    ]\n   }\n  }\n },\n \"required\": [\n  \"signals\"\n ]\n}"
      }
    },
    {
      "id": "4180138f-4b48-478d-a07f-6716e10cebfd",
      "name": "Route & Track",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        500,
        450
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Route and track in code. A signal only converts on an opportunity for the same product (named in the opportunity),\n// so an unrelated renewal or cross-sell on the account never closes it out. Every signal gets exactly one owner (capacity -> the CSM for a true-up, anything new -> the\n// account manager) and a one-week deadline. Each run, open signals are matched to Salesforce: a new opportunity on the\n// account created on or after the day the signal was raised converts it; past the deadline with no opportunity escalates.\nconst d = $('Detect Signals').first().json;\nconst out = $('Expansion Reader').first().json.output || {};\nconst norm = s => String(s || '').replace(/[‘’]/g, \"'\").replace(/[“”]/g, '\"').replace(/\\s+/g, ' ').trim().toLowerCase();\nconst safe = t => String(t || '').replace(/~/g, '≈');\nconst PRODUCTS = ['Backup Cloud', 'Shield', 'Insights', 'Access'];\nconst productOf = t => PRODUCTS.find(p => String(t || '').includes(p)) || '';\nconst addDays = (date, n) => new Date(Date.parse(date + 'T00:00:00Z') + n * 86400000).toISOString().slice(0, 10);\nconst days = (a, b) => Math.round((Date.parse(b + 'T00:00:00Z') - Date.parse(a + 'T00:00:00Z')) / 86400000);\nconst team = {}; for (const t of $('Get Account Team').all().map(i => i.json).filter(r => r.accountName)) team[t.accountName] = t;\nconst log = $('Get Signal Log').all().map(i => i.json).filter(r => r.signalId);\nconst known = new Set(log.map(r => r.signalId));\nconst opps = $('Get Recent Opportunities').all().map(i => i.json).filter(o => o.Id && o.Account);\nconst driftLog = $('Get Goal Drift Log').all().map(i => i.json).filter(r => r.accountName);\nconst lastDriftRun = driftLog.map(r => r.runDate).sort().pop();\nconst drifting = new Set(driftLog.filter(r => r.runDate === lastDriftRun && r.flagged).map(r => r.accountName));\n// Stated intent: Claude's call, kept only if the quote is word for word in that account's note of that date.\nlet unverified = 0;\nconst stated = (out.signals || []).filter(s => {\n  const acct = d.notesByAccount.find(a => a.accountName === s.accountName);\n  const note = acct && acct.notes.find(n => n.date === s.date);\n  const ok = note && s.quote && norm(s.quote).length >= 12 && norm(note.notes).includes(norm(s.quote));\n  if (!ok) unverified++;\n  return ok;\n}).map(s => ({ accountName: s.accountName, type: 'stated_expansion', key: s.date, evidence: s.what, quote: s.quote.trim(), product: productOf(s.quote + ' ' + s.what) }));\nconst LABEL = { capacity_cap: 'Near capacity', new_team: 'New team', stated_expansion: 'Asked for more' };\nconst fresh = [...d.codeSignals, ...stated].map(s => ({ ...s, signalId: s.accountName + '|' + s.type + '|' + s.key }))\n  .filter(s => !known.has(s.signalId) && team[s.accountName]).map(s => {\n    const csmOwns = s.type === 'capacity_cap';\n    return { signalId: s.signalId, accountName: s.accountName, type: s.type, product: s.product || productOf(s.evidence), evidence: s.evidence, quote: s.quote || '',\n      owner: csmOwns ? team[s.accountName].csm : team[s.accountName].accountManager, ownerRole: csmOwns ? 'CSM' : 'Account Manager',\n      raisedOn: d.runDate, slaDue: addDays(d.runDate, 7), status: 'open', opportunityId: '', opportunityName: '', updatedOn: d.runDate };\n  });\nconst converted = [], escalated = [];\nfor (const r of log.filter(r => r.status === 'open')) {\n  const product = r.product || productOf(r.evidence + ' ' + r.quote);\n  const opp = opps.filter(o => o.Account.Name === r.accountName && String(o.CreatedDate).slice(0, 10) >= r.raisedOn && (!product || String(o.Name).includes(product))).sort((a, b) => a.CreatedDate.localeCompare(b.CreatedDate))[0];\n  const base = { signalId: r.signalId, accountName: r.accountName, type: r.type, product, evidence: r.evidence, quote: r.quote, owner: r.owner, ownerRole: r.ownerRole, raisedOn: r.raisedOn, slaDue: r.slaDue };\n  if (opp) converted.push({ ...base, status: 'opportunity', opportunityId: opp.Id, opportunityName: opp.Name, updatedOn: d.runDate, daysToOpp: days(r.raisedOn, String(opp.CreatedDate).slice(0, 10)) });\n  else if (r.slaDue < d.runDate) escalated.push({ ...r, daysPast: days(r.slaDue, d.runDate), age: days(r.raisedOn, d.runDate) });\n}\nif (!fresh.length && !converted.length && !escalated.length) return [];\nconst money = n => '$' + Math.round((n || 0) / 1000) + 'K';\nconst tag = a => drifting.has(a) ? '\\n      :compass: _also flagged by the Goal Drift Detector; check the goal before pitching_' : '';\nconst newLine = s => '• *' + s.accountName + '* (' + money(d.value[s.accountName]) + ' Closed Won) · ' + LABEL[s.type] + ': ' + safe(s.evidence) +\n  (s.quote ? '\\n      _“' + safe(s.quote) + '”_' : '') + '\\n      → *' + s.owner + '* (' + s.ownerRole + '), opportunity or decision due ' + s.slaDue + tag(s.accountName);\nlet text = ':seedling: *Expansion Signal Router* (' + d.runDate + ')\\n' +\n  (fresh.length ? '\\n*New signals* (' + fresh.length + '), each with one owner:\\n' + fresh.sort((a, b) => (d.value[b.accountName] || 0) - (d.value[a.accountName] || 0)).map(newLine).join('\\n') + '\\n' : '') +\n  (escalated.length ? '\\n:rotating_light: *Past deadline, no opportunity* (' + escalated.length + ')\\n' + escalated.map(e => '• *' + e.accountName + '* · ' + LABEL[e.type] + ': ' + safe(e.evidence) + '\\n      raised ' + e.raisedOn + ' (' + e.age + ' days ago), owner ' + e.owner + ', ' + e.daysPast + ' days past due. Escalated to sales and CS leadership.').join('\\n') + '\\n' : '') +\n  (converted.length ? '\\n:white_check_mark: *Became pipeline* (' + converted.length + ')\\n' + converted.map(c => '• *' + c.accountName + '* · ' + LABEL[c.type] + ' → ' + c.opportunityName + ' (' + c.daysToOpp + ' days after the signal)').join('\\n') + '\\n' : '') +\n  (unverified ? '\\n_' + unverified + ' stated signal(s) dropped: quote not found in the notes._' : '');\nif (text.length > 2900) text = text.slice(0, 2850).slice(0, text.slice(0, 2850).lastIndexOf('\\n')) + '\\n_…cut to fit Slack; every signal is in the Expansion Signal Log._';\nconst rows = [...fresh, ...converted.map(({ daysToOpp, ...r }) => r)];\nreturn [{ json: { digestText: text, rows, counts: { fresh: fresh.length, converted: converted.length, escalated: escalated.length, unverified } } }];\n"
      },
      "executeOnce": true
    },
    {
      "id": "14041629-2489-4bc5-8082-228413c6e229",
      "name": "Post Signal Digest",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.7,
      "position": [
        700,
        450
      ],
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "id",
          "value": "C0BQ3G96HL6",
          "cachedResultName": "general"
        },
        "text": "={{ $json.digestText }}",
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "credentials": {
        "slackApi": {
          "id": "mv7TIi8kucFPqPg6",
          "name": "Slack account"
        }
      }
    },
    {
      "id": "8516612f-3d01-4b11-83c5-14d64cd448a1",
      "name": "Split Signal Rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        700,
        600
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return $('Route & Track').first().json.rows.map(json => ({ json }));"
      }
    },
    {
      "id": "4c96c937-024d-4f5b-b817-d1f6f8af690b",
      "name": "Update Signal Log",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        900,
        600
      ],
      "parameters": {
        "resource": "row",
        "operation": "upsert",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "C3sCIYc3A0X5bymr",
          "cachedResultName": "Expansion Signal Log"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "signalId",
              "condition": "eq",
              "keyValue": "={{ $json.signalId }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        }
      }
    }
  ],
  "connections": {
    "Daily Signal Check": {
      "main": [
        [
          {
            "node": "Get Customers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Customers": {
      "main": [
        [
          {
            "node": "Get Recent Opportunities",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Recent Opportunities": {
      "main": [
        [
          {
            "node": "Get Account Team",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Account Team": {
      "main": [
        [
          {
            "node": "Get Capacity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Capacity": {
      "main": [
        [
          {
            "node": "Get Team Activity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Team Activity": {
      "main": [
        [
          {
            "node": "Get Touchpoints",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Touchpoints": {
      "main": [
        [
          {
            "node": "Get Signal Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Signal Log": {
      "main": [
        [
          {
            "node": "Get Goal Drift Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Goal Drift Log": {
      "main": [
        [
          {
            "node": "Detect Signals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Detect Signals": {
      "main": [
        [
          {
            "node": "Expansion Reader",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Expansion Reader": {
      "main": [
        [
          {
            "node": "Route & Track",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude Sonnet": {
      "ai_languageModel": [
        [
          {
            "node": "Expansion Reader",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Signal Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Expansion Reader",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Route & Track": {
      "main": [
        [
          {
            "node": "Post Signal Digest",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split Signal Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Signal Rows": {
      "main": [
        [
          {
            "node": "Update Signal Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}