# What data cleansing and transformation practices ensure high data quality during inbound enterprise integrations?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Data Cleansing, Payload Transformation

Read it on the site: https://www.forcechallenges.com/interview-questions/what-data-cleansing-and-transformation-practices-ensure-high-data-quality-during-inbound-enterprise

## Answer

**Basics:** External systems send inconsistent data: extra whitespace, different date formats, unknown picklist values, duplicates. Cleansing means fixing or rejecting that data before it reaches core objects. Transformation means mapping the sender's structure to your schema.

**How it works:**
1. **Schema validation**: parse JSON into typed Apex wrapper classes (`JSON.deserialize` or `deserializeStrict`) so type mismatches fail early.
2. **Normalization**: trim text, standardize case, phone and country formats, convert dates to ISO 8601 UTC, strip invalid characters, enforce field length.
3. **Value mapping**: translate external codes to picklist values or lookup IDs using Custom Metadata mapping tables (queried once, not per record).
4. **Dedup and match**: upsert on an External ID field; use matching and duplicate rules where relevant.
5. **Load with partial success** and capture per-row errors.
6. **Quarantine** failures in a staging or error object for review and replay.

**Advanced / deep dive:** A staging object pattern (land raw payload, then transform asynchronously) decouples intake speed from processing and gives replay and audit. Duplicate rules do run on API/Apex inserts, but behavior depends on the rule's action and `Database.DMLOptions.duplicateRuleHeader`. Validation rules, required fields and triggers also fire for integration users, so design integrations around them or use a bypass custom permission carefully. Use `allowFieldTruncation` on `DMLOptions` only if truncation is acceptable. In middleware (MuleSoft DataWeave) do canonical-model mapping so Salesforce receives clean data. Multi-currency and time-zone fields need explicit handling.

**Code / config example:**
```apex
List<Account> toUpsert = new List<Account>();
for (ExtCustomer c : incoming) {                    // wrapper from JSON.deserialize
    if (String.isBlank(c.extId)) { errors.add(c); continue; }
    toUpsert.add(new Account(
        External_Id__c = c.extId,
        Name = c.name == null ? null : c.name.trim().left(255),
        Phone = c.phone == null ? null : c.phone.replaceAll('[^0-9+]', '')));
}
List<Database.UpsertResult> res = Database.upsert(toUpsert, Account.External_Id__c, false);
for (Integer i = 0; i < res.size(); i++) {
    if (!res[i].isSuccess()) { /* write res[i].getErrors()[0].getMessage() to error log */ }
}
```

**Hidden gotchas & must-remember points:**
- Upsert needs an External ID (or Id); Salesforce error if duplicate external IDs appear in one batch.
- `Database.upsert(records, field, false)` gives partial success; the default all-or-none rolls back everything.
- Text over field length fails with STRING_TOO_LONG unless truncation is enabled.
- Case-insensitive External IDs depend on field settings; mismatched case creates duplicates if not unique/case-sensitive configured.
- Validation rules and flows fire on integration loads.
- Do not query metadata mappings inside loops.

**Best practice:** Land, validate, transform, upsert by External ID, and quarantine rejects with enough context to replay.

---
Source: ForceChallenges · last updated 2026-11-02
