{ "id": "fetchsandboxLeadIntakeDemo", "name": "Rehearse a HubSpot lead and Slack alert", "active": false, "nodes": [ { "id": "manual", "name": "When I click Execute Workflow", "type": "n8n-nodes-base.manualTrigger", "typeVersion": 1, "position": [ 0, 0 ], "parameters": {} }, { "id": "start", "name": "Start FetchSandbox rehearsal", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 220, 0 ], "parameters": { "method": "POST", "url": "https://fetchsandbox.com/api/demo/n8n/lead-intake/start", "sendBody": true, "specifyBody": "json", "jsonBody": "{\"template\":\"n8n-lead-intake-demo-v1\"}", "options": { "timeout": 12000, "redirect": { "redirect": { "followRedirects": false } } } } }, { "id": "apply", "name": "Run the lead workflow", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 460, 0 ], "parameters": { "jsCode": "function advanceLeadIntake(input) {\n if (input.input_denied || input.result?.denied) return { done: true, status: 'forbidden' };\n if (input.input_rejected) return { done: true, status: 'rejected' };\n\n const context = JSON.parse(JSON.stringify(input.context ?? input));\n const result = input.result;\n if (result?.saved && result.state) context.state = result.state;\n const { lead_id: leadId, state, stage } = context;\n const stop = (status, reason) => ({ done: true, lead_id: leadId, status, reason });\n const request = (kind, fields, next) => ({\n context: { ...context, stage: next },\n request: { kind, ...fields },\n });\n const save = next => request('save', { state }, next);\n const provider = (provider, path, body, next) => request('provider', {\n provider, method: 'POST', path, body,\n }, next);\n const dealName = 'Lead intake ' + leadId;\n\n if (!stage) return request('claim', {}, 'claimed');\n\n // Writes may follow only a successful persisted checkpoint, including after a resume.\n if (stage === 'claimed') {\n if (!result?.claimed) return stop(result?.state?.status ?? 'unknown', 'lead_already_claimed');\n context.state = result.state;\n return request('save', { state: context.state }, 'decide');\n }\n if (['decide', 'send_deal', 'send_alert', 'finished', 'held'].includes(stage) && !result?.saved) {\n return stop('unknown', 'checkpoint_not_saved');\n }\n if (stage === 'decide') {\n const deal = state.operations.deal;\n if (deal.status === 'confirmed') {\n if (state.notification?.status === 'confirmed') return stop('confirmed', 'already_confirmed');\n if (['in_flight', 'unknown'].includes(state.notification?.status)) {\n state.status = 'unknown';\n return save('held');\n }\n state.notification = { status: 'in_flight' };\n return save('send_alert');\n }\n if (deal.status === 'pending') {\n deal.status = 'in_flight';\n return save('send_deal');\n }\n return provider('hubspot', '/crm/v3/objects/deals/search', {\n filterGroups: [{ filters: [{ propertyName: 'dealname', operator: 'EQ', value: dealName }] }],\n properties: ['dealname'],\n }, 'deal_read');\n }\n if (stage === 'send_deal') {\n return provider('hubspot', '/crm/v3/objects/deals', {\n properties: { dealname: dealName, pipeline: 'default', dealstage: 'appointmentscheduled' },\n }, 'deal_written');\n }\n if (stage === 'deal_written' && result?.status >= 200 && result.status < 300 && result.body?.id) {\n state.operations.deal = { status: 'confirmed', id: result.body.id };\n return save('decide');\n }\n if (stage === 'deal_written') {\n state.operations.deal.status = 'unknown';\n return save('decide');\n }\n if (stage === 'deal_read') {\n const found = result?.status === 200 ? result.body?.results : null;\n if (found?.length === 1 && found[0].id && found[0].properties?.dealname === dealName) {\n state.operations.deal = { status: 'confirmed', id: found[0].id };\n return save('decide');\n }\n state.status = 'unknown';\n return save('held');\n }\n if (stage === 'send_alert') {\n return provider('slack', '/chat.postMessage', {\n channel: 'C_PILOT', text: 'New test lead ' + leadId + ' (CRM ' + state.operations.deal.id + ')',\n }, 'alert_sent');\n }\n if (stage === 'alert_sent') {\n const accepted = result?.status === 200 && result.body?.ok === true && result.body?.ts;\n state.notification = accepted ? { status: 'confirmed', ts: result.body.ts } : { status: 'unknown' };\n state.status = accepted ? 'confirmed' : 'unknown';\n return save(accepted ? 'finished' : 'held');\n }\n if (stage === 'finished') return stop('confirmed', 'deal_and_alert_saved');\n if (stage === 'held') return stop('unknown', 'effect_unconfirmed');\n throw new Error('Unsupported lead intake step');\n}\nconst response = $json;\nif (response.status === 'recorded' || response.status === 'incomplete') {\n return [{json: {...response, finished: true}}];\n}\nif ($runIndex >= 75) throw new Error('The rehearsal did not finish within the allowed steps.');\nif (response.status === 'action') {\n return [{json: {finished: false, turn: response.turn, output: advanceLeadIntake(response.input)}}];\n}\nif ($runIndex > 0 && response.status !== 'waiting') throw new Error('Unexpected demo response.');\nreturn [{json: {finished: false}}];\n" } }, { "id": "done", "name": "Receipt recorded or run stopped", "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [ 700, 0 ], "parameters": { "conditions": { "options": { "caseSensitive": true, "typeValidation": "strict", "version": 2 }, "conditions": [ { "id": "finished", "leftValue": "={{ $json.finished }}", "rightValue": true, "operator": { "type": "boolean", "operation": "true", "singleValue": true } } ], "combinator": "and" }, "options": {} } }, { "id": "step", "name": "Continue FetchSandbox rehearsal", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 700, 210 ], "parameters": { "method": "POST", "url": "={{ 'https://fetchsandbox.com/api/demo/n8n/lead-intake/step/' + $('Start FetchSandbox rehearsal').first().json.run }}", "sendBody": true, "specifyBody": "json", "jsonBody": "={{ JSON.stringify($json.turn ? {turn: $json.turn, output: $json.output} : {}) }}", "options": { "timeout": 12000, "redirect": { "redirect": { "followRedirects": false } } }, "sendHeaders": true, "headerParameters": { "parameters": [ { "name": "Authorization", "value": "={{ 'Bearer ' + $('Start FetchSandbox rehearsal').first().json.capability }}" } ] } } }, { "id": "summary", "name": "Open the FetchSandbox receipt", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 960, 0 ], "parameters": { "jsCode": "const {receipt_url, verdict, checks, status} = $json; return [{json: {receipt_url, verdict, checks, status}}];" } } ], "connections": { "When I click Execute Workflow": { "main": [ [ { "node": "Start FetchSandbox rehearsal", "type": "main", "index": 0 } ] ] }, "Start FetchSandbox rehearsal": { "main": [ [ { "node": "Run the lead workflow", "type": "main", "index": 0 } ] ] }, "Run the lead workflow": { "main": [ [ { "node": "Receipt recorded or run stopped", "type": "main", "index": 0 } ] ] }, "Receipt recorded or run stopped": { "main": [ [ { "node": "Open the FetchSandbox receipt", "type": "main", "index": 0 } ], [ { "node": "Continue FetchSandbox rehearsal", "type": "main", "index": 0 } ] ] }, "Continue FetchSandbox rehearsal": { "main": [ [ { "node": "Run the lead workflow", "type": "main", "index": 0 } ] ] } }, "settings": { "executionOrder": "v1", "saveDataSuccessExecution": "none", "saveDataErrorExecution": "none", "saveManualExecutions": false, "executionTimeout": 145 } }