Qeasy Cloud
Get Started

Purchase Inbound Sync: A Single-Strategy Playbook from Marketing Cloud to SQL Server

· 何金辉· Integration Solutions· 8 views· 4 min read
SQL Server汤臣倍健营销云采购入库轻易云Incremental SyncField Mapping幂等设计

What This Strategy Solves

In a retail/distribution business, purchase-inbound orders are created and approved in the marketing cloud, and the downstream ERP (built on SQL Server) needs these documents for accounting, inventory, and supplier reconciliation. On the Qeasy data-integration platform we configure a single purchase-inbound sync strategy: the marketing cloud is the source, SQL Server is the target, isolated per distributor, pulled by time window, and written into a header table plus a line-item table.

Data Flow and Field Mapping

The end-to-end chain is marketing cloud (source) → Qeasy middleware → SQL Server (target). The source uses POST /erp/api/order/query/purInWarehsOrder to fetch documents; the target writes data through custom SQL into two tables: header gxkphz and line gxkpmx.

Key field mapping (condensed):

Business meaningSource (marketing cloud)Target (header / line)
Document numbernumberdjbh (shared)
Unique keyidid (used for idempotency)
Doc typedjlxdjbh–related djlx
Business daterqrq
Inbound timeontimeontime
Supplier / partner IDwldwidwldwid
Receiver / phone / addrshouhr, lxdh, shhdzshouhr, lxdh, shhdz
Tax-included amounthsjehsje
Remarkbeizhubeizhu
Original marketing-cloud doc numberwebdjbhwebdjbh (header)
Product IDspidspid (line)
Warehouse ID / nameckid, cknameckid, ckname (line)
Batch / expiry / shelf lifepihao, sxrq, baozhiqipihao, sxrq, baozhiqi (line)
Quantity / tax-included priceshl, hshjshl, hshj (line)
Related doc numberxgdjbhxgdjbh (line)

The header and line tables are joined on djbh; line rows are identified by dj_sn. id is also the idempotency key — as long as the source id is stable, repeated pulls will not produce duplicate rows.

How to Configure on Qeasy

On the Qeasy platform this strategy is split into two actions: the source action queries approved inbound orders by distributor and time window; the target action writes the result into SQL Server.

Source configuration highlights:

  • API: /erp/api/order/query/purInWarehsOrder, POST.
  • tenantId hard-coded to a single distributor (a multi-distributor scenario must be split into separate strategies).
  • status=1 (approved / outbound) — never let unapproved drafts flow into the accounting DB.
  • beginTime uses the {{LAST_SYNC_TIME|datetime}} variable; endTime is computed at runtime.
  • Both number and id have idCheck enabled, with id as the primary idempotency key and number as a business reference.

Target configuration highlights:

  • Target DB: MSSQL; action type: EXECUTE.
  • extend_params_1 points to itemList so the line-item array is bound to the line table.
  • The header SQL INSERT INTO gxkphz ... and the line SQL INSERT INTO gxkpmx ... use named parameters.
  • The djbh on both tables comes from the same source document number, guaranteeing a clean 1-to-N join.

A common Qeasy customer pattern is to centralize all code translations in the platform's mapping table — IDs like wldwid, ckid, spid that come in as strings on the source side and must become internal numeric keys on the target side. Keeping the mapping in one place lets a single edit apply to every strategy that needs it.

Implementation Steps

We split the rollout into three schedule phases so that nothing runs at high frequency before it is proven.

Phase 1 — establish the incremental baseline. Run a one-off "first full reload": do not pass beginTime, filter by status, and write all approved historical inbound orders into SQL Server. When the reload completes, Qeasy records LAST_SYNC_TIME as the starting point for subsequent incrementals.

Phase 2 — bring up incremental scheduling. Source cron is */3 7-23 * * * (every 3 minutes during business hours); target cron is */2 7-23 * * * (every 2 minutes), slightly faster than the source so the downstream never lags. Stay at low volume for 1–2 days.

Phase 3 — steady-state operation. Verify that LAST_SYNC_TIME advances as expected, that there is no "source has it, target missing it" gap, and that the idCheck hit rate looks healthy. If anything is missing, trigger a manual backfill — preferably by passing an explicit yxyNumber whitelist rather than widening the time window.

Lessons Learned

  1. Not pinning the status filter. We once saw unapproved drafts flowing into the ERP because status was left open; the books were later reversed and stock was wiped twice. Pin status=1 on the source side and let Qeasy filter there — do not rely on downstream SQL.

  2. Inserting line items in a per-row loop. An earlier design wrote one INSERT per line, and a moderately large order dragged the run into minutes. The target extend_sql_1 supports array-based batch binding; always batch, never loop. This is a classic "correct but slow" pitfall.

  3. idCheck on number only. number is editable on the marketing cloud; only id is the true primary key. If only number is checked, a source-side renumber will be treated as a new document and inserted again. Keep id as the idempotency key, number as a business field.

  4. Scattered code mapping. We once had a wldwid mapping hard-coded in three different strategies. When the source renamed its code, all three had to change — and one was missed. Centralized mapping tables are the standard Qeasy pattern: one edit, every strategy updated.

  5. Header and body released together. The source returns a header plus a nested line array. A common mistake is filling main_params correctly but forgetting to point extend_params_1 at itemList; the header inserts fine, the line table stays empty, and finance sees the right totals with no rows. The safe pattern is phased go-live: ship header-only first, verify djbh / wldwid / hsje, then enable the body.

When to Use and When Not To

Use it when: there is a single distributor; inbound order volume is moderate (hundreds to a few thousand per day); the source supports time-windowed incremental queries; the target is SQL Server with custom tables; header and body must land in separate tables. Do not use it when: you need cross-distributor tenant isolation inside one strategy; the source only provides file exports with no API; the target is a SaaS that does not allow custom table structures.

Original content. Please credit the source when reposting: https://www.qeasy.cloud/insights/solutions/strat-sql-server-p158a24-4991-nf6be2bf7-65a40f1d

Comments