Sub topic 8
Transforming and mapping data between systems
This chapter assumes you know Salesforce objects and fields, REST APIs, and JSON. It covers why data rarely moves between systems unchanged, where the mapping work can live, and the errors that appear most often.
Why mapping is needed
Two systems almost never describe the same thing the same way. An ERP may call a customer company_name and nest the city under address. Salesforce expects Name and BillingCity on an Account. Dates, currencies, picklist values, and identifiers also differ.
Transformation is the work of converting a payload from one system's shape to another's. Mapping is the part of that work that decides which source field feeds which target field.
Kinds of change
Most transformations fall into a few groups:
- Renaming:
company_namebecomesName. - Restructuring: a nested
address.citybecomes a flatBillingCity. - Value translation:
"A"becomes"Active", or a country code becomes a country name. - Format conversion: XML to JSON, or
31/12/2026to2026-12-31. - Enrichment and filtering: adding a default value, or dropping inactive records.
Mapping in middleware
A middleware layer keeps the mapping in one place. Every consumer of the ERP data gets the same Salesforce-shaped result, and a change to the ERP format is fixed once. MuleSoft uses DataWeave for this.
%dw 2.0
output application/json
---
payload map (customer) -> {
Name: customer.company_name,
BillingCity: customer.address.city,
External_Id__c: customer.id
}The map function runs once for each customer in the incoming list. The result is a JSON array of objects that use Salesforce field names. External_Id__c holds the ERP key so later updates can be matched as upserts.
Mapping inside Salesforce
You can do the same work without middleware. Apex can parse the response and build records. Flow can assign values to fields. This works for one or two small mappings.
Map<String, Object> customer = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
Map<String, Object> address = (Map<String, Object>) customer.get('address');
Account acc = new Account(
Name = (String) customer.get('company_name'),
BillingCity = (String) address.get('city'),
External_Id__c = (String) customer.get('id')
);The cost shows up as the mapping grows. The logic lives in Apex classes that need tests and deployments. Each additional source system adds another copy of the mapping.
Where the mapping lives
Common errors
- Null and missing fields. A source record without
addressmakesaddress.get('city')fail in Apex. Check for null first. In DataWeave, usecustomer.address.city default "". - Type mismatches. Salesforce rejects a string where it expects a number or a date. Convert explicitly.
- Picklist values that do not exist. An unmapped code produces an error or a restricted-value failure. Keep a translation table and decide what happens to unknown codes.
- Missing external ID. Without a stable key, a retry creates duplicates instead of updating records.
- Business rules hidden in the mapping. Pricing or eligibility logic buried in a field mapping is hard to find and test later.
Choosing a location
Keep the mapping in Salesforce when there is one source, the shape is close to the Salesforce model, and the team already maintains Apex or Flow. Move it to middleware when several systems share the same mapping, formats differ widely (XML, flat files, SOAP), or non-Salesforce staff must maintain it.
Exercise
An ERP record has no address object, and your Apex mapping throws a null pointer exception. Name two ways to make the mapping safe, and say what the unsafe version also risks in a batch.
Show answer
Check for null before reading nested values, or use a default (default "" in DataWeave). Without this, one bad record can fail the whole batch, so handle each record separately and log the ones that fail.
Exercise
Why does the DataWeave example map customer.id to External_Id__c?
Show answer
It stores the ERP key on the Salesforce record so later loads can upsert on that field. Retries then update existing Accounts instead of creating duplicates.