All articles

Platform

DUPLICATES_DETECTED: handle duplicate rules in Apex

Inspect duplicate-rule behavior, report rejected records and avoid treating allowSave as a universal bypass for integration writes.

A retry can become a duplicate create when the caller doesn’t know the first request succeeded. Start by separating transport retries from new business records.

When a duplicate rule rejects a write, inspect its matching criteria, conditions and configured action. The right outcome might be to use an existing record, ask for review or reject the request. Automatically bypassing the rule removes that decision.

Read the rule and its action together

Matching logic identifies potential duplicates; the duplicate rule determines the applicable behavior. Review both with the data owner. A broad name match may flag legitimate separate customers, while a reliable external identifier can support a different integration design.

The DuplicateRuleHeader reference documents allowSave for rules configured with the Alert option. It is not a universal override for a rule that blocks saves. Its sharing-related option also affects which records can participate in duplicate handling.

Don’t return inaccessible duplicate records to the caller merely because detection found them. Separate the permission to create a record from the permission to inspect a possible match.

Handle every attempted record

For collection writes, decide whether the operation permits partial success. If it does, inspect every SaveResult and associate errors with their input rows. The database error guide documents that response pattern.

A successful HTTP response from an integration endpoint should not imply that every item saved. Include a stable item identifier and a bounded status so the caller can distinguish confirmed writes, rejected duplicates and uncertain operations.

If the business process supports updating an existing record, use an approved lookup or upsert strategy with a reliable key. A fuzzy duplicate match shouldn’t silently choose which customer record receives an update.

Test retries as well as matching

Create a test matrix containing a unique record, a known duplicate, a legitimate similar-name record and a record the caller cannot access. Exercise the actual rule configuration and integration identity in a suitable test environment.

Then send the same logical request twice using the intended idempotency mechanism. Confirm that the second attempt returns the first operation’s result or performs a safe update, rather than creating a second record.

For an agent action, make the duplicate outcome explicit. The agent can explain that review is required, but it shouldn’t invent a new spelling or email address to get past the rule.

Keep bypasses narrow, approved and observable when the business process requires them. The useful integration contract explains when a create is appropriate, when an update is appropriate and when a person must resolve the ambiguity.

Keep reading

All articles