Hi everyone. I am having some trouble fleshing out the search node in my workflow. I have made a VAPI assistant and connected it to the workflow. I created a tool in VAPI called Check_inventory and specified that the assistant should extract structured data (name, contact number and item_name).
In theory, I want to be able to call this assistant and then ask about an item in inventory, it should then search the closest matching item and return back the price and quantity in the google sheet.
The problem I am having is that when I try test it, it always returns the first item in the list. I am having trouble setting up the search node or search tool properly.
{ “nodes”: [ { “parameters”: { “httpMethod”: “POST”, “path”: “vapi-stock-check”, “responseMode”: “responseNode”, “options”: {} }, “name”: “Webhook”, “type”: “n8n-nodes-base.webhook”, “typeVersion”: 2, “position”: [ -224, -16 ], “id”: “7e574c87-f883-4405-ab0e-e2335520a6b1”, “webhookId”: “07476c3d-9ddb-4536-a5e5-da111b1736dc” }, { “parameters”: { “documentId”: { “__rl”: true, “value”: “11UTq2ZYF4EdeLNO5vQtzPVqgneMdwvfcEVwOFp4fl2c”, “mode”: “list”, “cachedResultName”: “VAPI Stock Checker”, “cachedResultUrl”: “https://docs.google.com/spreadsheets/d/11UTq2ZYF4EdeLNO5vQtzPVqgneMdwvfcEVwOFp4fl2c/edit?usp=drivesdk” }, “sheetName”: { “__rl”: true, “value”: “gid=0”, “mode”: “list”, “cachedResultName”: “Stock”, “cachedResultUrl”: “https://docs.google.com/spreadsheets/d/11UTq2ZYF4EdeLNO5vQtzPVqgneMdwvfcEVwOFp4fl2c/edit#gid=0” }, “options”: {} }, “name”: “Get Inventory”, “type”: “n8n-nodes-base.googleSheets”, “typeVersion”: 4, “position”: [ -32, -16 ], “id”: “8c82f3d1-bce7-49f6-86e7-b6e5e3ad2db1”, “credentials”: { “googleSheetsOAuth2Api”: { “id”: “v1qt4VcDr7IHFLdv”, “name”: “Google Sheets account” } } }, { “parameters”: { “jsCode”: “// Get the webhook input data\nconst webhookData = $input.first().json;\n\n// Extract item query from various possible Vapi payload structures\nlet itemQuery = ‘’;\n\n// Try different possible paths where Vapi might send the data\nif (webhookData.message && webhookData.message.content) {\n itemQuery = webhookData.message.content;\n} else if (webhookData.content) {\n itemQuery = webhookData.content;\n} else if (webhookData.item_name) {\n itemQuery = webhookData.item_name;\n} else if (webhookData.query) {\n itemQuery = webhookData.query;\n} else if (webhookData.transcription) {\n itemQuery = webhookData.transcription;\n} else {\n // If none of the above, try to get the entire payload as string\n itemQuery = JSON.stringify(webhookData);\n}\n\n// Convert to lowercase for comparison\nitemQuery = itemQuery.toLowerCase();\n\n// Get inventory data from the previous node\nconst inventory = $(‘Get Inventory’).all();\n\n// Search for matching items\nconst matches = inventory.filter(item => \n item.json.item_name && item.json.item_name.toLowerCase().includes(itemQuery)\n);\n\nif (matches.length === 0) {\n return [{\n json: {\n found: false,\n message: `Sorry, I couldn’t find any items matching \”${itemQuery}\" in our inventory.`\n }\n }];\n}\n\n// Return the first match\nconst item = matches[0].json;\nreturn [{\n json: {\n found: true,\n item_name: item.item_name,\n quantity: item.quantity,\n price: item.price,\n message: `We have ${item.quantity} units of ${item.item_name} in stock, priced at $${item.price} each.`\n }\n}];" }, “name”: “Search Item”, “type”: “n8n-nodes-base.code”, “typeVersion”: 2, “position”: [ 176, -16 ], “id”: “f8717594-f759-492b-8cad-1f08b12b51b6” }, { “parameters”: { “respondWith”: “json”, “responseBody”: “={{ $json }}”, “options”: {} }, “name”: “Respond to Webhook”, “type”: “n8n-nodes-base.respondToWebhook”, “typeVersion”: 1, “position”: [ 384, -16 ], “id”: “7033012c-c019-41cd-837a-0e2b5c862621” } ], “connections”: { “Webhook”: { “main”: [ [ { “node”: “Get Inventory”, “type”: “main”, “index”: 0 } ] ] }, “Get Inventory”: { “main”: [ [ { “node”: “Search Item”, “type”: “main”, “index”: 0 } ] ] }, “Search Item”: { “main”: [ [ { “node”: “Respond to Webhook”, “type”: “main”, “index”: 0 } ] ] } }, “pinData”: {}, “meta”: { “templateCredsSetupCompleted”: true, “instanceId”: “1b8d2ca2943718e4fc337aa91564b217827cd67f2fa5b7578bc7ab1db4595cb9” } }

