Sub topic 8
Transforming and mapping data between systems
Every integration has to turn one system's data shape into another's. This chapter covers where that work happens, which kinds of mismatch you will meet, and when Salesforce's native tools can do it. It also covers when a middleware layer such as MuleSoft is the better home.
This chapter assumes you know Salesforce objects, fields, and API names. It also assumes you can read JSON and know what a REST payload is. You do not need prior DataWeave experience.
Why mapping is the hidden cost of integration
Connecting two systems is usually easy. Agreeing on what the data means is harder. An ERP calls a record a customer with a company_name. Salesforce calls it an Account with a Name. The two systems also disagree on date formats, status codes, and how addresses are nested.
Transformation is the work of converting a payload from the source system's shape into the target's. Mapping is the field-by-field rule set that drives it. A mapping is not a one-time chore. It changes whenever either system changes a field, so where you keep it matters.
Kinds of mismatch you will meet
| Mismatch | Example | Typical fix |
|---|---|---|
| Field names | `company_name` vs `Name` | Rename in the mapping |
| Structure | `address.city` nested vs flat `BillingCity` | Flatten or nest |
| Data types | `"1250.50"` string vs Currency | Cast |
| Formats | `31/12/2026` vs `2026-12-31` | Parse and reformat |
| Code values | `"A"` vs `"Active"` | Lookup table |
| Cardinality | One order with many lines vs separate records | Split or aggregate |
| Identity | Source `id` vs Salesforce `Id` | External ID field |
| Missing data | Null, empty string, absent key | Defaults and validation |
Identity deserves special attention. Salesforce generates its own 15- or 18-character Id. The source system has its own key. Store the source key in an External ID field, such as External_Id__c. That lets you upsert by the source key and never keep a separate cross-reference table.
Transformation inside Salesforce
Salesforce can transform data natively in three places.
- Flow has Assignment, Transform, and Formula elements. These suit light field-to-field copying inside Salesforce.
- Apex gives full control. You deserialize JSON into a class or a
Map, then buildsObjectrecords. - Data Loader and Import tools map CSV columns to fields. This is one-off or scheduled batch loading.
Here is a typical Apex mapping. It receives the ERP shape and builds Accounts for an upsert.
public class CustomerMapper {
public class ErpCustomer {
public String id;
public String company_name;
public ErpAddress address;
}
public class ErpAddress {
public String city;
}
public static List<Account> toAccounts(String json) {
List<ErpCustomer> customers =
(List<ErpCustomer>) System.JSON.deserialize(json, List<ErpCustomer>.class);
List<Account> accounts = new List<Account>();
for (ErpCustomer c : customers) {
accounts.add(new Account(
Name = c.company_name,
BillingCity = c.address == null ? null : c.address.city,
External_Id__c = c.id
));
}
return accounts;
}
}You would then call Database.upsert(accounts, Account.External_Id__c, false). This only works if External_Id__c exists on Account and is marked as an External ID.
This works well for one source with a stable shape. Problems appear as the mapping grows:
- The mapping lives in Apex, so a field rename in the ERP means a Salesforce deployment, tests, and release.
- Apex runs under governor limits, including CPU time and heap size. Large payloads or heavy loops can hit them.
- The same mapping logic cannot be reused by a second system that needs the same ERP data.
- Only developers who know Apex can maintain it.
Transformation in middleware
A middleware platform moves the mapping out of Salesforce and into a dedicated layer. In MuleSoft, the language for this is DataWeave. It is a functional language that reads a payload in one format and writes it in another.
The following mapping converts a list of ERP customers into Salesforce Account field names:
%dw 2.0
output application/json
---
payload map (customer) -> {
Name: customer.company_name,
BillingCity: customer.address.city,
External_Id__c: customer.id
}payload is the incoming data. map loops over each element, and the object after -> is the output record. DataWeave also handles format conversion. The same script can read XML, CSV, or JSON and write any of them by changing the output line.
A more realistic mapping
Real mappings need cleaning, lookups, and defaults. This version trims text, converts a date, translates status codes, and strips non-numeric characters from phone numbers:
%dw 2.0
output application/json
var statusMap = { "A": "Active", "I": "Inactive", "S": "Suspended" }
---
payload map (customer) -> {
Name: trim(customer.company_name default ""),
BillingCity: customer.address.city default null,
Phone: customer.phone replace /[^0-9+]/ with "",
Customer_Since__c: (customer.signup_date as Date {format: "dd/MM/yyyy"})
as String {format: "yyyy-MM-dd"},
Status__c: statusMap[customer.status] default "Unknown",
External_Id__c: customer.id as String
}Notes on the behavior:
defaultsupplies a value when a key is missing or null. Without it, a missing key givesnull.- The date line parses a string into a
Dateusing the source pattern, then writes it back out as an ISO string that Salesforce accepts. statusMap[customer.status]looks up the code. An unexpected code returnsnull, sodefaultcatches it.- If
signup_dateis missing, the date cast fails and the whole transformation raises an error. You would guard it withif (customer.signup_date != null)or adefault, depending on whether a missing date is acceptable.
Where the mapping lives
The same data can flow through different layers. The choice of layer decides who owns the mapping and how many places must change when a system changes.
In the middleware route, Salesforce receives data that already matches its field names. A second consumer can reuse the same ERP connection with its own mapping, and the ERP shape is known in one place only.
Canonical models and their trade-off
When many systems exchange data, teams sometimes define a canonical data model. This is a single neutral format, such as a standard Customer shape. Each system maps to and from the canonical form. With N systems, you write about 2N mappings instead of N×(N−1) direct ones.
The cost is that the canonical model is one more thing to design, version, and govern. It also tends to grow into a lowest-common-denominator format that fits no system well. Use one when you have many systems sharing the same entities. For two systems, a direct mapping is simpler.
Common errors
- Silent truncation or null overwrite. A mapping that sends
nullfor a missing source field can overwrite good data in Salesforce. Decide for each field whether a missing value means "leave as is" or "clear it". Omit the key rather than sending null when you want to leave the value untouched. - Time zone drift. A source date-time without a zone gets read in the wrong zone. State the zone explicitly in the mapping.
- Picklist mismatch. Writing a value that is not in a restricted picklist fails the record. Map to known values and route unknowns to an error path.
- Number precision. Currency and decimal values passed as floating-point numbers can pick up rounding errors. Pass amounts as strings or decimals.
- Hard-coded lookups. Code tables embedded in scripts go stale. Keep ones that change often in a table or configuration property.
- Untested mappings. Mappings are code. Test them with sample payloads, including empty lists, missing keys, and unexpected codes.
Deciding where to transform
Use the following signals as a guide. Later chapters build a full decision framework.
- Keep it native when there is one source, the shape is simple and stable, the mapping is a handful of fields, and the Salesforce team owns both sides.
- Move it to middleware when several systems need the same data in different shapes, formats such as XML, EDI, or CSV must be converted, mappings change often, payloads are large, or non-Salesforce teams need to maintain the logic.
The cost side matters too. Middleware means another platform to license, secure, and operate. If the mapping is small and Salesforce can host it, moving it out can add more overhead than it removes.
Exercise
An ERP sends order data with the date "15/03/2026" and a status code "P". Salesforce needs the date as "2026-03-15" and the status as "Pending". Write the DataWeave expressions for the two output fields, then name one place where this mapping could live instead of middleware and one condition under which you would still choose middleware.
Show answer
Date: (payload.order_date as Date {format: "dd/MM/yyyy"}) as String {format: "yyyy-MM-dd"}. Status: ({ "P": "Pending", "S": "Shipped" })[payload.status] default "Unknown". The mapping could live in an Apex class or Flow inside Salesforce when there is one source with a stable shape. You would still choose middleware if a second system needs the same ERP data in a different shape, if the ERP sends XML or EDI, or if mappings change often and should be maintained outside Salesforce release cycles.