{
  "name": "Maps-style lead list into Sheets",
  "nodes": [
    {
      "parameters": {
        "content": "## Maps-style lead list into Sheets\n\nPortfolio demo only. This workflow is an original example for demonstration. It is not client work and it does not represent a deployment for any customer.\n\nRun **Run demo** to execute the offline rules engine. The live branch is used only when the incoming item sets `demo` to false and the placeholder credentials have been replaced.",
        "height": 220,
        "width": 520,
        "color": 4
      },
      "id": "01f9b41a-fff2-42ed-9843-024d2bd8b512",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -40
      ]
    },
    {
      "parameters": {},
      "id": "1ca92cc2-3683-415e-afe5-2fda654313c9",
      "name": "Run demo",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        40,
        280
      ],
      "notes": "CLI and the n8n editor start here. An empty manual run loads the bundled sample.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "maps-style-lead-list",
        "authentication": "none",
        "responseMode": "lastNode",
        "responseData": "firstEntryJson",
        "options": {}
      },
      "id": "194662f2-c8d6-47c8-a414-54e3e4044c02",
      "name": "Inbound webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        40,
        460
      ],
      "notes": "POST JSON to this path. Include demo:false only after credentials are connected.",
      "notesInFlow": true,
      "webhookId": "33393b19-9f6d-44f0-bb9f-eb19b955e388"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const sample = {\n  \"demo\": true,\n  \"url\": \"https://example.com/place/lumen\",\n  \"html\": \"<title>Lumen Freight</title><p>+1 555 555 0190</p>\",\n  \"company\": \"Lumen Freight\",\n  \"phone\": \"+15555550190\"\n};\nconst incoming = $input.first()?.json ?? {};\nconst provided = Object.keys(incoming).filter((key) => incoming[key] !== undefined && incoming[key] !== null && incoming[key] !== '');\nconst base = provided.length ? { ...sample, ...incoming } : { ...sample };\nconst flag = incoming.demo;\nbase.demo = !(flag === false || flag === 'false' || flag === 0 || flag === '0');\nreturn [{ json: base }];\n"
      },
      "id": "c17e41f4-2ed4-40c7-874e-234caa58f484",
      "name": "Normalize input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        340,
        360
      ],
      "notes": "Merges the request over the bundled sample. demo stays true unless the caller explicitly turns it off.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nconst produced = (() => {\n\nconst html = String(item.html || '');\nconst wanted = ['company', 'phone', 'title'];\nconst record = { url: item.url || '', fetchedAt: item.fetchedAt || new Date().toISOString() };\nfor (const field of wanted) {\n  if (item[field]) record[field] = item[field];\n}\nif (!record.email) {\n  const found = html.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}/i);\n  if (found) record.email = found[0].toLowerCase();\n}\nif (!record.phone) {\n  const found = html.match(/\\+?[0-9][0-9()\\-\\s]{8,}/);\n  if (found) record.phone = found[0].trim();\n}\nif (!record.title) {\n  const found = html.match(/<title>([^<]+)<\\/title>/i);\n  if (found) record.title = found[1].trim();\n}\nconst missing = wanted.filter((field) => !record[field]);\nconst route = missing.length === 0 ? 'complete' : missing.length < wanted.length ? 'partial' : 'empty';\nreturn {\n  summary: (record.title || record.url || 'Page') + ' extracted with route ' + route + '.',\n  route,\n  record: { ...record, missing: missing.join(',') },\n  slackText: route.toUpperCase() + ' scrape ' + (record.url || ''),\n  actions: [{ app: 'Google Sheets', operation: 'append', payload: record }],\n};\n\n})();\nif (!produced || typeof produced.summary !== 'string' || !produced.summary.trim()) {\n  throw new Error('Apply rules must return a summary string');\n}\nreturn [{\n  json: {\n    ...item,\n    ...produced,\n    demo: item.demo !== false,\n  },\n}];\n"
      },
      "id": "1ca461b7-c66d-4d47-a87b-8ea912b83b80",
      "name": "Apply rules",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        620,
        360
      ],
      "notes": "Deterministic stand-in for the model or enrichment API. Replace or precede this with a live model node when you turn demo mode off.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "cad4a2dd-8cca-469a-88eb-2a859945c8b4",
              "leftValue": "={{ $json.demo }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "3f01f83c-3292-4b92-ae81-3ca178882504",
      "name": "Demo mode?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        900,
        360
      ],
      "notes": "True: package the offline result. False: run the credentialed apps.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nreturn [{\n  json: {\n    demo: true,\n    status: 'demo_ready',\n    workflow: 'maps-style-lead-list',\n    summary: item.summary,\n    route: item.route ?? null,\n    record: item.record ?? {},\n    actions: item.actions ?? [],\n    slackText: item.slackText ?? item.summary,\n    notes: 'Demo mode did not call external apps. Send {\"demo\": false} after placeholder credentials are replaced.',\n  },\n}];\n"
      },
      "id": "abb292b6-ddeb-4719-9e74-db82af213104",
      "name": "Package demo result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1180,
        180
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "sheet",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_SPREADSHEET_ID"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Local leads"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "b3037f82-9930-42d1-9c38-79968b20d67c",
      "name": "Append Local leads",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1180,
        760
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "PLACEHOLDER_GOOGLESHEETSOAUTH2API",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "contact",
        "operation": "upsert",
        "email": "={{ $json.record.email || $json.email }}",
        "additionalFields": {
          "firstName": "={{ $json.record.firstName || $json.firstName }}",
          "lastName": "={{ $json.record.lastName || $json.lastName }}",
          "companyName": "={{ $json.record.company || $json.company }}"
        }
      },
      "id": "83186e1b-02a3-4374-a1b3-a5f1cabdd50a",
      "name": "Upsert HubSpot contact",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2.2,
      "position": [
        1460,
        760
      ],
      "credentials": {
        "hubspotOAuth2Api": {
          "id": "PLACEHOLDER_HUBSPOTOAUTH2API",
          "name": "HubSpot account"
        }
      }
    },
    {
      "parameters": {
        "content": "## Live path\n\nReplace every `PLACEHOLDER_*` credential, then POST the sample with `\"demo\": false`.\n\nThe nodes on this branch use the `record` and `actions` fields produced by Apply rules.",
        "height": 180,
        "width": 420,
        "color": 6
      },
      "id": "39c0248f-a48a-4c75-9ab7-1955f6358025",
      "name": "Live path",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1120,
        520
      ]
    }
  ],
  "connections": {
    "Run demo": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Inbound webhook": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize input": {
      "main": [
        [
          {
            "node": "Apply rules",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apply rules": {
      "main": [
        [
          {
            "node": "Demo mode?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Demo mode?": {
      "main": [
        [
          {
            "node": "Package demo result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Append Local leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append Local leads": {
      "main": [
        [
          {
            "node": "Upsert HubSpot contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "A local listing that already includes a phone and a site is normalized and can be pushed to HubSpot when the email or phone is present."
  },
  "active": false
}
