Transform Complex Data like a Boss in Salesforce
3 min readJul 11, 2024
Salesforce now has a data transformation language. This reduces the bloat and drudgery of trying to transform data in Apex and use a language designed for the job.
We will start with two models: 1) a pre-transform model and a, 2) post-transform model. Below is the pre-transformation model called JsonDataContainer because a potential usage example that model can originate from being serialized from JSON.
public with sharing class JsonDataContainer {
public AccountData accountData;
public Class AccountData {
public string name;
public List<ContactData> contacts;
public List<OrderData> orders;
}
public class ContactData{
public string firstName;
public string lastName;
}
public class OrderData{
public string targetDate;
public Integer amount;
}
}
The below post-transform model has Salesforce data, such as Account, Opportunities, and Contacts.
public with sharing class InsertDataContainer {
//We put the standard objects each in their own container class
//This allow us to depict child objects as a lists on the parent
//and allows us to expand usage to hold additional data
public AccountContainer accountData;
public Class AccountContainer {
public Account account;
public List<ContactContainer> contacts;
public List<OpportunityContainer> opps;
}
public Class ContactContainer {
public Contact contact;
}…