Sincronizarea datelor între sisteme face ca toate echipele să lucreze pe aceleași înregistrări. Ghidul construiește workflow-uri de sincronizare pe n8n 2.x: sync declanșat de webhook, cu detectarea conflictelor, sync incremental cu checkpoint-uri și o verificare programată a consistenței între sisteme.
Petru Constantin este fondatorul DeviDevs și a proiectat platforma multi-agent pe care rulează firma, cu guardrails impuse în cod. Exemplele de cod sunt scrise pe documentația n8n 2.47 (canalul stable).
Sincronizare bidirecțională CRM
Sincronizează datele între CRM și alte sisteme:
// Workflow:
// Webhook "CRM Webhook" (POST, Authentication: Header auth)
// -> Data Table "Get": sync_state row where entity_id = {{ $json.body.entity_id }}
// (node setting Always Output Data on, so a new entity still continues)
// -> Code "Resolve Conflict" (this node)
// -> If proceed
// true: Postgres "Insert or Update" into the entity table,
// then Data Table "Upsert" of the new sync state
// false: Slack "Send" to a data-quality channel with the conflict
// Webhooks arrive often, and the n8n docs warn that workflow static data
// "may behave unreliably under high-frequency workflow executions", so the
// sync state lives in a data table. When you write back to the CRM, skip events
// caused by your own sync user, or every write triggers another webhook.
// Code node "Resolve Conflict", mode: Run Once for Each Item
// $json: the stored state for this entity (empty when there is none)
const event = $('CRM Webhook').item.json.body;
const state = $json;
const actionMap = {
'contact.created': 'create',
'contact.updated': 'update',
'contact.deleted': 'delete',
'deal.created': 'create',
'deal.updated': 'update',
'deal.stage_changed': 'update',
'company.created': 'create',
'company.updated': 'update',
};
const action = actionMap[event.event_type] ?? 'unknown';
const source = event.source ?? 'crm';
const eventTime = new Date(event.timestamp ?? Date.now()).getTime();
const lastTime = state.last_modified ? new Date(state.last_modified).getTime() : null;
let conflict = null;
if (lastTime !== null && Math.abs(eventTime - lastTime) < 5 * 60 * 1000 && state.last_source !== source) {
conflict = 'concurrent_modification';
}
if (event.data?.version != null && state.version != null && event.data.version < state.version) {
conflict = 'version_mismatch';
}
// Strategy: stale versions are blocked; for contacts the CRM wins; otherwise last write wins.
let proceed = action !== 'unknown';
if (conflict === 'version_mismatch') {
proceed = false;
} else if (conflict) {
const priority = { crm: 1, erp: 2, database: 3 };
proceed = event.entity_type === 'contact'
? (priority[source] ?? 99) <= (priority[state.last_source] ?? 99)
: eventTime >= lastTime;
}
return {
json: {
entity_id: event.entity_id,
entity_type: event.entity_type,
source,
action,
data: event.data,
conflict,
proceed,
new_version: (state.version ?? 0) + 1,
last_modified: new Date(eventTime).toISOString(),
},
};Sincronizare incrementală a datelor
Gestionează seturi mari de date cu sincronizare incrementală:
// Workflow:
// Schedule Trigger (every 15 minutes)
// -> Postgres "Get Checkpoint", Execute Query:
// SELECT last_sync_time FROM sync_checkpoints
// WHERE sync_name = 'crm_to_warehouse' ORDER BY last_sync_time DESC LIMIT 1
// (node setting Always Output Data on, so the first run without a checkpoint still continues)
// -> HTTP Request "Fetch Changes" (credential for the CRM API)
// Send Query Parameters: modified_after = {{ $json.last_sync_time ?? '1970-01-01T00:00:00Z' }},
// limit = 100
// Options > Pagination: Update a Parameter in Each Request,
// Type Query, Name cursor, Value {{ $response.body.next_cursor }}
// Set the pagination stop condition on has_more = false (or an empty next_cursor);
// without it the last page is requested again with an empty cursor.
// -> Code "Transform and Validate" (this node)
// -> If _valid
// true: Postgres "Insert or Update" into contacts_staging, matching on id
// false: Postgres "Insert" into sync_errors
// -> Postgres "Save Checkpoint", Execute Query with Query Parameters:
// INSERT INTO sync_checkpoints (sync_name, last_sync_time, records_synced)
// VALUES ('crm_to_warehouse', $1, $2)
// Store the largest updated_at you received, not the current time,
// so records changed during the run are picked up next time.
// Never build INSERT statements by pasting values into SQL: use the
// Insert or Update operation or $1, $2 Query Parameters.
// Code node "Transform and Validate", mode: Run Once for All Items
// Input: one item per page from the paginated HTTP Request, each with a data array.
const isValidEmail = email => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const normalizePhone = phone => (phone ? String(phone).replace(/[^\d+]/g, '') : null);
const records = $input.all().flatMap(item => item.json.data ?? []);
const syncedAt = new Date().toISOString();
return records.map(record => {
const email = record.email ? String(record.email).toLowerCase().trim() : null;
const errors = [];
if (!record.id) errors.push('Missing ID');
if (!email || !isValidEmail(email)) errors.push('Invalid email');
return {
json: {
id: record.id,
external_id: record.external_id,
email,
first_name: record.first_name?.trim() ?? null,
last_name: record.last_name?.trim() ?? null,
full_name: `${record.first_name ?? ''} ${record.last_name ?? ''}`.trim(),
company: record.company_name ?? null,
phone: normalizePhone(record.phone),
created_at: record.created_at,
updated_at: record.updated_at,
source_system: 'crm',
sync_timestamp: syncedAt,
_valid: errors.length === 0,
_errors: errors,
},
};
});Consistența datelor în mai multe sisteme
Menține consistența datelor în mai multe sisteme simultan:
// Workflow:
// Schedule Trigger (every 6 hours)
// -> HTTP Request nodes that read the same sample of IDs from the CRM, the ERP and the warehouse
// -> Merge / Code step so each item looks like { id, crm, erp, warehouse }
// -> Code "Compare Systems" (this node)
// -> If inconsistent_records > 0: Slack "Send" to a data-quality channel,
// and a Data Table "Insert" per record into a repair queue that a person reviews
// Three REST APIs give you no shared transaction to roll back: write to the source
// of truth first, fan out from there, and let this check report what drifted.
// Code node "Compare Systems", mode: Run Once for All Items
const criticalFields = ['email', 'phone', 'company'];
const systems = ['crm', 'erp', 'warehouse']; // in priority order: CRM is the source of truth
const rows = $input.all().map(item => item.json);
const report = rows.map(row => {
const issues = [];
for (const field of criticalFields) {
const values = systems
.filter(sys => row[sys] && !row[sys].error)
.map(sys => ({ system: sys, value: row[sys][field] ?? null }));
const distinct = new Set(values.map(v => JSON.stringify(v.value)));
if (distinct.size > 1) {
issues.push({ field, values, recommended: values[0] });
}
}
return { id: row.id, issues };
}).filter(r => r.issues.length > 0);
return [{
json: {
checked_at: new Date().toISOString(),
samples_checked: rows.length,
inconsistent_records: report.length,
consistency_pct: rows.length
? Number(((rows.length - report.length) / rows.length * 100).toFixed(2))
: null,
report,
},
}];Concluzie
Sincronizarea datelor cere tratarea explicită a conflictelor, a actualizărilor incrementale și a diferențelor dintre sisteme. În n8n 2.x asta înseamnă un data table sau o bază de date pentru starea de sync, în locul workflow static data pe webhook-uri aglomerate, paginarea încorporată a nodului HTTP Request cu un checkpoint pentru seturi mari de date, Query Parameters în Postgres sau operația Insert or Update în locul SQL-ului construit din string-uri și o verificare programată a consistenței, ale cărei rezultate le vede un om înainte să se repare ceva.
Din producție
În JARVIS, sistemul multi-agent pe care rulează DeviDevs, un script deduplică înregistrările în ambele sensuri între lista de cold outreach DeviDevs și o foaie Google în care scriu și alți oameni. Pentru că fiecare scriere înlocuiește tot fișierul, scriptul citește modifiedTime al foii înainte și după, refuză scrierea dacă fișierul s-a schimbat între timp și iese cu un cod dedicat. Descoperirea rulează pe un snapshot local, deci nu depinde de OAuth sau de rețea. Într-un workflow n8n de sincronizare, asta e concurență optimistă (verifici un câmp de versiune înainte să suprascrii): o înregistrare schimbată între timp oprește scrierea.
Surse oficiale
Pe același subiect: Workflow-uri n8n cu AI: pattern-uri de integrare, n8n transformare date: workflow-uri ETL, Automatizare email cu n8n: ghid complet de implementare a workflow-urilor. Mai multe articole în categoria n8n Automation.