Examples for working with DataWeave scripts from Apex.
This functionality is currently in Dev Preview in Winter '23. It can be selectively enabled in scratch orgs.
For feedback and questsions, please see the DataWeave in Apex Trailblazer community group.
If you want to experiment with the possibilities of DataWeave scripts, there is the interactive DataWeave playground.
The DataWeave extension for Visual Studio Code is available and syntax highlighting other additional tooling support.
Slides for the Dreamforce '22 Presentation Unleash Powerful Data Transforms in Apex with DataWeave
- Create a scratch org with the "DataWeaveInApex" scratch org feature. See /config/project-scratch-def.json as an example
- Push the source to the scratch org - requires SFDX CLI 7.151.9+
See HelloWorldTest.cls
for a minimal version for calling a DataWeave script from Apex that only logs "Hello World".
The following example is from the Dreamforce `21 Develop Enterprise Applications with Apex presentation and starts at the 15 minute mark.
See CsvToJsonConversionTest.cls
for an example of transforming CSV data stored in a static resource to a JSON string.
See the csvSeparatorToJson()
method in CsvToJsonConversionTest.cls
for an example of transforming CSV data using ;
as the separator to a JSON string.
See the convertCsvToObjects()
method in CsvToObjectTest.cls
for an example of transforming CSV data directly to an Apex POTATO (Plain Old Typed Apex Transient Object - Apex equivalent of a POJO)
See ExceptionHandlingTest.cls
for an example of handling an exception orginating from a DataWeave script execution
See ObjectProcessingTest.cls
for examples of passing objects to and receiving objects from DataWeave scripts.
See MultipleInputsTest.cls
for an example of handling multiple inputs to a single DataWeave Script.
See PluralizeTest.cls
for an example of using DataWeave built in String functions
See DWJsonToSObjectsListTest.cls
for the example from TDX '22 of dynamic JSON mapping to sObjects
See JsonDateFormatTest.cls
for an example of custom DateTime formatting in JSON output.
See ReservedKeywordTest.cls
for an example of handling reserved Apex keywords