Postman collection
Customers Write

pixiMergeCustomers

Merges a duplicate customer into another customer by moving all related records to the target and deleting the source customer.

Moves everything that belongs to customer CustRefFrom to customer CustRefTo, then deletes CustRefFrom.

Validation. The checks run in this order. The first one that fails ends the call with ReturnCode = ERROR:

  1. CustRefTo and CustRefFrom must differ.
  2. The pair must be listed in Addresses_Duplicates. Either direction counts (CustRef1/CustRef2).
  3. Both customers must exist in Customers.
  4. Neither customer may have an order with an OrderHeaderAttribute of type 3 (archived orders).
  5. The base currencies (Shops.BaseCurrencyId) of both customers' shops (Customers.CustomerOf) must be the same.

Merge. Inside a transaction, the customer reference is changed from CustRefFrom to CustRefTo in these tables:

  • orders and order lines: OrderHeader, Orderlines, BundleOrderlines, OrderHistory
  • invoices and reminders: Invoices, Invlines, InvoicesDeleted, InvlinesDeleted, Reminders
  • interface tables: C_Int_OrderHeader, C_Int_Orderlines, C_Int_Addresses
  • addresses and history: Addresses, AddressesHist, CustomersHist
  • customer account: CustomerAccount, CustomerAccountLog, CustomerAccountManualBookings, CustomerAccountManualBookingsLog
  • payments and direct debits: CCPTranLog, DTAUS, DTAUSLog
  • other: Voucher, eMaillogs, AM_Mail, AM_MailHist, Boxes, pixi.Offer, lastsdetail

In Addresses and C_Int_Addresses, CustomerNrExternal is also set to the external customer number of the target customer.

After that, the duplicate entries of CustRefFrom are removed from Addresses_Duplicates and the source row is deleted from Customers.

Finish. This step always runs, also after a failed validation or an error. Rows with CustRefFrom are deleted from Addresses_Duplicates and tmp.Addresses_Simple, and an event is logged. One status row with ReturnCode and ErrorMessage is returned.

Notes

  • Pitfall: the final cleanup always runs. All Addresses_Duplicates and tmp.Addresses_Simple entries of CustRefFrom are deleted even when validation fails. If both parameters are equal, this removes the duplicate entries of that customer.
  • If an outer transaction is open, the call uses a savepoint and does not commit. Otherwise it opens and commits its own transaction. On error it rolls back, logs the error via log.EventInsert and log.InsertError, and returns Unexpected error: "…" with the error text cut to 80 characters.
  • The final cleanup and logging run after the merge transaction has been committed, or inside the caller's transaction if one is open.
  • XACT_ABORT is on.
  • CONTEXT_INFO is set to several values during the merge to bypass triggers on OrderHeader, Addresses, CCPTranLog, Invoices, Invlines, Orderlines and CustomerAccount. The triggers are not attached, so their skipped logic is unknown.
  • After the merge, the code tries to refresh the customer account sums with a dummy update on CustomerAccount. The update filters on CustRefFrom, whose rows have already been moved, so it probably affects no row.
  • If one of the two customers has no matching shop, the currency is NULL and the currency check does not stop the merge.
  • Validation reads use NOLOCK. The rows to be moved are collected with UPDLOCK.
  • The tables written by the logging procedures log.EventInsert and log.InsertError are not visible because their definitions are not attached.
  • The call is irreversible: the source customer is deleted.

Tables used

Addresses_Duplicates · Write Customers · Write Shops · Read OrderHeader · Write OrderHeaderAttribute · Read Voucher · Write CustomersHist · Write AddressesHist · Write Addresses · Write CCPTranLog · Write C_Int_Addresses · Write C_Int_Orderlines · Write C_Int_OrderHeader · Write Invlines · Write Invoices · Write InvoicesDeleted · Write InvlinesDeleted · Write Reminders · Write BundleOrderlines · Write OrderHistory · Write Orderlines · Write eMaillogs · Write CustomerAccountManualBookingsLog · Write CustomerAccountLog · Write CustomerAccount · Write CustomerAccountManualBookings · Write DTAUSLog · Write DTAUS · Write AM_MailHist · Write AM_Mail · Write Boxes · Write Offer · Write lastsdetail · Write Addresses_Simple · Write
SQL object:
dbo.pipiMergeCustomers
Updated at 2026-10-30