Salesforce Decode
Salesforcedecode
Back to questions
ApexIntermediateintegrationupsertdml

Build idempotent upsert integration keyed on external Id and hash

Real World Scenario

Middleware replays same product sync message after network retry creating duplicate Product2 rows with different external keys.

Expected Answer

• Single External_Id__c unique field as upsert key • Content hash field: skip DML if hash matches existing record • Database.upsert with external Id field specified • Duplicate rules complement but do not replace idempotent upsert design • Log skipped duplicates vs updated vs inserted counts per batch • Handle external Id null rows separately—reject or staging quarantine • Test replay: same payload twice assert single record unchanged second time

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Direct answer: Single External_Id__c unique field as upsert key Also consider: Content hash field: skip DML if hash matches existing record In practice: Database.upsert with external Id field specified Balance speed of delivery with maintainability.

Architect Perspective

Integration replay is guaranteed in distributed systems—idempotent upsert is baseline not optional.