{
  "name": "Sales Template 6 - Error handler",
  "nodes": [
    {
      "id": "e265dc63-6416-5b52-92ea-eab3da8c66f0",
      "name": "Guide",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -420,
        -360
      ],
      "parameters": {
        "content": "## Sales Template 6 - Error handler\nError workflow of Sales Templates 1 to 5 (Workflow settings › Error workflow).\n\nOn any failure: one row in the `Incidents` tab and one e-mail to the owner with the workflow, the node, the stage and a link to the execution. No lead data is copied into the alert: open the execution to see it and replay it after the fix. Close an incident by filling `resolved_at` and `resolved_by`.",
        "height": 340,
        "width": 460,
        "color": 3
      }
    },
    {
      "id": "3b4e3975-a341-59ed-91fc-83c7b6d06beb",
      "name": "On Workflow Error",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        180,
        0
      ],
      "parameters": {}
    },
    {
      "id": "5c3ed9c1-673f-545e-abdd-2ccba888c769",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        400,
        0
      ],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "19f54c7a-1a1a-574d-b23b-8472015b124d",
              "name": "cfg.sheetUrl",
              "value": "PASTE_YOUR_GOOGLE_SHEET_URL",
              "type": "string"
            },
            {
              "id": "dddbe914-bff7-5df1-9148-a6df35c3b4c1",
              "name": "cfg.incidentTab",
              "value": "Incidents",
              "type": "string"
            },
            {
              "id": "0149e425-260d-5b44-adb7-c79d36c10cb5",
              "name": "cfg.ownerEmail",
              "value": "owner@yourcompany.com",
              "type": "string"
            },
            {
              "id": "0ea2953d-edd6-5f39-ab78-8150fe99e3db",
              "name": "cfg.fromEmail",
              "value": "sales@yourcompany.com",
              "type": "string"
            },
            {
              "id": "8f33317c-172f-5daf-b244-17e5c76bad37",
              "name": "cfg.fromName",
              "value": "Sales Pipeline",
              "type": "string"
            }
          ]
        },
        "options": {},
        "includeOtherFields": true
      }
    },
    {
      "id": "dc1b1c28-5aa6-5d79-a7e1-f4dc43eb2699",
      "name": "Build Incident",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        620,
        0
      ],
      "parameters": {
        "jsCode": "// One incident per failed execution, without personal data (e-mail addresses and phone numbers are masked).\nconst e = $('On Workflow Error').first().json;\nconst clean = s => String(s || '').replace(/[^\\s@]+@[^\\s@]+\\.[^\\s@]+/g, '[email]').replace(/\\+?\\d[\\d\\s().-]{7,}\\d/g, '[number]').slice(0, 500);\nconst node = e.execution?.lastNodeExecuted || e.trigger?.error?.node?.name || 'unknown';\nconst stage = /Save Sending|Send E-mail|After Send|Save Sent/.test(node) ? 'sending (check send_status = sending: … on the lead)'\n  : /Receive Lead|Validate and Normalise|Read Leads|Check Duplicate|Save New Lead/.test(node) ? 'intake (lead may not be saved; the form got an error)'\n  : /Website|Qualification|Qualify|Validate AI|Save Qualification|Alert Salesperson/.test(node) ? 'qualification (lead saved, no AI result yet)'\n  : /Reply|Update Lead/.test(node) ? 'reply handling (check the mailbox)'\n  : 'other';\nreturn [{ json: {\n  incident_id: 'INC-' + (e.execution?.id || Date.now()),\n  occurred_at: new Date().toISOString(),\n  workflow: e.workflow?.name || 'unknown',\n  execution_id: String(e.execution?.id || ''),\n  execution_url: e.execution?.url || '',\n  node, stage,\n  message: clean(e.execution?.error?.message || e.trigger?.error?.message),\n  resolved_at: '', resolved_by: '',\n} }];\n"
      }
    },
    {
      "id": "794c2ce2-a7e2-5f1c-ae67-97884c754f75",
      "name": "Log Incident",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        840,
        0
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "={{ $('Settings').first().json.cfg.sheetUrl }}"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Settings').first().json.cfg.incidentTab }}"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "cellFormat": "RAW",
          "handlingExtraData": "ignoreIt"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "1e61e954-178b-5266-bd6b-e92667baaeaf",
      "name": "Alert Owner",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1060,
        0
      ],
      "parameters": {
        "fromEmail": "={{ $('Settings').first().json.cfg.fromName }} <{{ $('Settings').first().json.cfg.fromEmail }}>",
        "toEmail": "={{ $('Settings').first().json.cfg.ownerEmail }}",
        "subject": "=Sales workflow error · {{ $('Build Incident').first().json.node }}",
        "emailFormat": "text",
        "text": "=Incident {{ $('Build Incident').first().json.incident_id }}\n\nWorkflow: {{ $('Build Incident').first().json.workflow }}\nNode: {{ $('Build Incident').first().json.node }}\nStage: {{ $('Build Incident').first().json.stage }}\nMessage: {{ $('Build Incident').first().json.message }}\n\nOpen the execution: {{ $('Build Incident').first().json.execution_url }}\n\nAfter the fix, replay the execution from n8n and fill resolved_at and resolved_by in the Incidents tab.",
        "options": {
          "appendAttribution": false,
          "replyTo": "={{ $('Settings').first().json.cfg.fromEmail }}"
        }
      }
    }
  ],
  "connections": {
    "On Workflow Error": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Build Incident",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Incident": {
      "main": [
        [
          {
            "node": "Log Incident",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Incident": {
      "main": [
        [
          {
            "node": "Alert Owner",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true
  }
}
