Dataweave in Apex

Processing text-based data in Apex is quite a common task, particularly in integrations. And yet, handling JSONs, XMLs and CSVs often requires bulky code, and causes performance issues – in one word: It is painful. Enter Dataweave for Apex (in Beta as of spring 23): Salesforce now allows us to process text data with Mulesoft’s transformation language. With just two lines of code, all common text data formats can be processed with a Dataweave script.

What is DataWeave in Apex?

DataWeave in Apex uses the Mulesoft DataWeave library to read and parse data from one format, transform it, and export it in a different format. You can create DataWeave scripts as metadata and invoke them directly from Apex. Like Apex, DataWeave scripts are run within Salesforce application servers, enforcing the same heap and CPU limits on the executing code.

What the fuzz is about

Basically: Any transformation of semi-structured data can now go into two lines of code

DataWeave.Script dwscript = new DataWeaveScriptResource.csvToContacts();
DataWeave.Result dwresult = dwscript.execute(new Map<String, Object>{'records' => inputCsv});

What’s Dataweave?

Mulesoft’s proprietary expression and data transformation language. A functional programming language that is designed to run in a JVM. Variables are immutable. Functions can be declared as functions, variables / lambdas or anonymously

Data Processing Superpowers with Dataweave

The session is going to run you through the basics so that you learn how to create and use Dataweave scripts in Apex, and we’ll have a look a performance, benefits and tradeoffs, so that you can learn how to run data transformations in Apex faster and better in the future.

YouTube video

DataWeave Limits & Considerations

  • 50 DataWeave scripts per org
  • Hefty CPU time cost for initialization (we’ll see later)
  • Limitations in filetype support, no import of modules / mappings (limited reusability)

How and where to start

ry out a few examples in the Dataweave playground at
https://dataweave.mulesoft.com/learn/playground

Install the Dataweave Extension for VS Code (Beta) and check code here.

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 459

Leave a Reply

Your email address will not be published. Required fields are marked *