Skip links

Salesforce Winter ’23 Release: DataWeave in Apex

Something interesting awaits us in the Salesforce Winter ’23 Release!

If data transformation capabilities in Apex set your nerves on edge, you would love the new DataWeave in Apex feature. This feature uses Mulesoft’s DataWeave library to extract data from one format, transform it, and output it in a required format.

With DataWeave in Apex will be able to:

  • parse and transform CSV data
  • serialize Apex objects with custom date formats
  • serialize and deserialize JSON with Apex reserved keywords
  • do custom transformations to remove or add namespaces or remove __c suffixes

Where to find

Important: for now, the feature is in Developer Preview. It is available in scratch orgs where the DataWeaveInApex feature is enabled, in both Lightning Experience and Salesforce Classic experience.

How it works

You can create DataWeave scripts as metadata and invoke them directly from Apex.

Here is an example of how to transform an input CSV file into Contact SObjects:

// CSV data for Contacts\n
String inputCsv = 'first_name,last_name,email\nCodey,"The Bear",[email protected]'; 
DataWeave.Script dwscript = DataWeave.Script.createScript('csvToContacts');
DataWeave.Result dwresult = dwscript.execute(new Map<String, Object>{'records' => inputCsv});
List<Contact> results = (List<Contact>)dwresult.getValue();
Assert.areEqual(1, results.size());
Contact codeyContact = results[0];
Assert.areEqual('Codey',codeyContact.FirstName);
Assert.areEqual('The Bear',codeyContact.LastName);

The CSV data is transformed into objects with the DataWeave script csvToContacts.dwl.

%dw 2.0
input records application/csv
output application/apex
---
records map(record) -> {
 FirstName: record.first_name,
 LastName: record.last_name,
 Email: record.email
} as Object {class: "Contact"}

We can’t wait for DataWeave in Apex to become generally available! Have you tested it already?