Code
js
// n8n Code node, mode "Run Once for All Items".
// Place it after a node that reads back the rows you just saved.
const ID_FIELD = 'id'; // field every real record must have
const MIN_PLANNED = 1; // fewer planned records than this is a failure
function realIds(rows, idField) {
return rows
.map((row) => (row ? row[idField] : undefined))
.filter((id) => id !== undefined && id !== null && id !== '')
.map(String);
}
function verifyRun(plannedRows, savedRows, idField = ID_FIELD, minPlanned = MIN_PLANNED) {
const planned = new Set(realIds(plannedRows, idField));
const savedCounts = new Map();
for (const id of realIds(savedRows, idField)) {
savedCounts.set(id, (savedCounts.get(id) || 0) + 1);
}
const missing = [...planned].filter((id) => !savedCounts.has(id));
const duplicated = [...savedCounts]
.filter(([, count]) => count > 1)
.map(([id]) => id);
const problems = [];
if (planned.size < minPlanned) {
problems.push(`${planned.size} real records planned, expected at least ${minPlanned}`);
}
if (missing.length > 0) {
problems.push(`${missing.length} missing: ${missing.slice(0, 20).join(', ')}`);
}
if (duplicated.length > 0) {
problems.push(`${duplicated.length} duplicated: ${duplicated.slice(0, 20).join(', ')}`);
}
if (problems.length > 0) {
throw new Error(`Run verification failed: ${problems.join('; ')}`);
}
return [{ json: { ok: true, planned: planned.size, missing: 0, duplicated: 0 } }];
}
// Inside the n8n Code node, uncomment the next lines as the last lines.
// 'Planned rows' is the name of the node that holds the rows you meant to write.
// return verifyRun(
// $('Planned rows').all().map((item) => item.json),
// $input.all().map((item) => item.json),
// );Why it happens
n8n marks a run as failed only when a node throws an error. A filter that drops every item ends the branch quietly, so nothing is saved and nothing fails. "Always Output Data" makes it worse, because it sends one empty item and the item count says 1. A total can also look right while some records are missing and others were saved twice.
Steps
- Keep the rows you plan to write in one node, for example one named
Planned rows. - After the write node, add a node that reads the same rows back from the database or sheet.
- Add a Code node after the read-back node and set the mode to "Run Once for All Items".
- Paste the code and uncomment the last lines. Change
ID_FIELDand the node name to match your workflow. - The function keeps only rows with a real ID, so empty items are not counted.
- It lists missing IDs and duplicated IDs separately, then throws if either list is not empty.
- The thrown error turns the execution red, so your Error Trigger workflow can send the alert.
Pitfalls
- The last lines are commented out because a top-level
returnonly works inside n8n. The check does nothing until you uncomment them. - Read back only the rows from this run, for example by batch ID or date. Reading a whole table is slow and reports old duplicates.
- IDs are compared as text, so
7and"7"match. Trim or normalise IDs first if your systems format them differently. - This check cannot see a run that never started. A stopped trigger or a killed n8n process needs an outside deadline check.
- An HTTP 200 response can still carry a failure in its body. Check for an expected ID before you treat the row as planned.
Seen on: n8n Community ·
Related service: n8n, Make and Zapier workflow automation