{"record":{"id":"b99315ec9eff4043","repo":"Zie619/n8n-workflows","slug":"insufficient-stock-for-retrieve-issue-request","errorCode":null,"errorMessage":"Insufficient stock for ${$('Retrieve Issue Request Details').first().json['Product ID']}","messagePattern":"Insufficient stock for (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"workflows/Code/0926_Code_Webhook_Create_Webhook.json","lineNumber":258,"sourceCode":"              \"value\": \"={{ $json.body['Measurement Unit'] }}\"\n            }\n          ]\n        },\n        \"includeOtherFields\": \"=\"\n      },\n      \"typeVersion\": 3.4,\n      \"notes\": \"This set node performs automated tasks as part of the workflow.\"\n    },\n    {\n      \"id\": \"47d2bb01-99e6-4ab1-b19d-bc9912243150\",\n      \"name\": \"Update Stock\",\n      \"type\": \"n8n-nodes-base.code\",\n      \"position\": [\n        7440,\n        3860\n      ],\n      \"parameters\": {\n        \"jsCode\": \"const currentStock = parseFloat($input.first().json['Current Stock']\\n );\\nconst approvedQuantity = parseFloat(\\n $('Verify Approval Data').first().json['Approved Quantity']);\\nconst newStock = currentStock - approvedQuantity;\\n\\nif (newStock < 0) throw new Error(`Insufficient stock for ${\\n  $('Retrieve Issue Request Details').first().json['Product ID']}`);\\n\\nreturn {\\n  json: {\\n    ...$json,\\n    \\\"Updated Current Stock\\\": newStock,\\n\\\"Material Name\\\":$input.first().json['Material Name'],\\\"Measurement Unit\\\":$input.first().json['Measurement Unit'],\\n\\\"Minimum Stock Level\\\": \\n  $input.first().json['Minimum Stock Level'],\\n  \\\"Issue Date\\\":\\n    $('Retrieve Issue Request Details').first().json['Issue Date'],\\n\\\"Product ID\\\": \\n  $('Retrieve Issue Request Details').first().json['Product ID']\\n \\n  }\\n};\"\n      },\n      \"typeVersion\": 2,\n      \"notes\": \"This code node performs automated tasks as part of the workflow.\"\n    },\n    {\n      \"id\": \"dcbb196f-1ecf-4137-af29-e511c4b7b9d9\",\n      \"name\": \"Receive Issue Request\",\n      \"type\": \"n8n-nodes-base.webhook\",\n      \"position\": [\n        5900,\n        3400\n      ],\n      \"webhookId\": \"73d4bdfc-2d8b-42f4-85d5-43ecae0953c1\",\n      \"parameters\": {\n        \"path\": \"raw-materials-issue\",\n        \"options\": {},\n        \"httpMethod\": \"POST\"\n      },","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/workflows/Code/0926_Code_Webhook_Create_Webhook.json#L240-L276","documentation":"Thrown by the 'Update Stock' Code node during issue-request approval: currentStock minus approvedQuantity would go below zero. This is a deliberate business-rule guard preventing negative inventory in the Google Sheets stock ledger.","triggerScenarios":"`Current Stock` from the stock lookup row is less than `Approved Quantity` from the approval webhook; `Current Stock` parsed as NaN (parseFloat of empty/absent sheet cell gives NaN, and NaN < 0 is false, but a 0 stock with positive approval triggers it); the sheet row for the Product ID holds stale stock after concurrent approvals.","commonSituations":"Approver approves more than available stock; two approvals race and the second sees outdated stock; sheet cell formatted as text or with currency symbols so parseFloat yields NaN or wrong values; sheet row lookup returned the wrong row.","solutions":["Verify the stock lookup row matched the correct Product ID and the Current Stock cell is a plain number.","Validate Approved Quantity against available stock BEFORE sending the approval request (the workflow has a 'Verify Requested Quantity' node - enforce it in the approval UI too).","Make the update atomic: re-read stock immediately before writing and use sheet locking or single-writer pattern to avoid race conditions.","Decide the business path for insufficient stock: reject with a message to the approver instead of an unhandled throw."],"exampleFix":"// before\nconst newStock = currentStock - approvedQuantity;\nif (newStock < 0) throw new Error(`Insufficient stock for ${$('Retrieve Issue Request Details').first().json['Product ID']}`);\n\n// after\nconst currentStock = parseFloat($input.first().json['Current Stock']);\nconst approvedQuantity = parseFloat($('Verify Approval Data').first().json['Approved Quantity']);\nif (!Number.isFinite(currentStock)) throw new Error(`Current Stock is not a number: ${$input.first().json['Current Stock']}`);\nif (!Number.isFinite(approvedQuantity)) throw new Error(`Approved Quantity is not a number`);\nconst newStock = currentStock - approvedQuantity;\nif (newStock < 0) throw new Error(`Insufficient stock for ${$('Retrieve Issue Request Details').first().json['Product ID']}: have ${currentStock}, approved ${approvedQuantity}`);","handlingStrategy":"validation","validationCode":"const currentStock = parseFloat($input.first().json['Current Stock']);\nconst approvedQuantity = parseFloat($('Verify Approval Data').first().json['Approved Quantity']);\nif (!Number.isFinite(currentStock)) throw new Error(`Current Stock not numeric: ${$input.first().json['Current Stock']}`);\nif (!Number.isFinite(approvedQuantity)) throw new Error('Approved Quantity not numeric');\nif (currentStock - approvedQuantity < 0) throw new Error(`Insufficient stock: have ${currentStock}, approved ${approvedQuantity}`);","typeGuard":"const isPositiveNumber = (v) => Number.isFinite(v) && v > 0;","tryCatchPattern":null,"preventionTips":["Validate Approved Quantity against available stock in the approval step before submission","Keep sheet numeric cells as plain numbers (no currency/text formats)","Re-read stock immediately before writing to narrow race windows","Handle the insufficient-stock case with an approver-facing rejection message instead of an unhandled error"],"tags":["n8n","inventory","business-rule","google-sheets","validation"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}