MuleSoft is a growing platform that is now integrated into the Salesforce ecosystem. You may already hear about it if you’re looking for content related to systems integration. MuleSoft embodies DataWeave in most of its components, in which we can write DataWeave scripts and expressions to convert the data, similarly to what is done with other programming languages; however, it removes some of the extra complexity by providing useful pre-built functions.
Fig. 1: DataWeave with the most common formats possible to process.
DataWeave allows the development from simple everyday solutions to complex use case for data integration: reading and translating data from one format to another, structuring its content thoroughly, and aggregating or deconstructing the data. Below, find the table of contents for this article:
- How to explore and start using DataWeave
- DataWeave and Salesforce
- Practical examples
- Final words
- DataWeave useful links
1. How to explore and start using DataWeave
DataWeave was created and developed specifically for Mulesoft, but it’s on the road to being open source! There is a large and very active community with many tutorials, videos and forums where we can learn and share.
Mulesoft has an online playground to try out the DataWeave scripts and explore functionalities. You can use the playground to test and validate your DataWeave code, or even better, there’s now an extension for VisualStudio Code to use this language! It’s effortless to install and start using it; you need to follow a tutorial.
And if you’re a Salesforce developer, you’ll be happy to know that Salesforce is bringing DataWeave scripts to APEX! This is a significant advantage that will save a lot of work for Salesforce developers, as it will be demonstrated below.
2. DataWeave and Salesforce
With the Winter ‘23 Salesforce Release, we can now call DataWeave scripts using APEX.
This feature enhances native Apex data transformation support by integrating Mulesoft’s DataWeave library into the Apex runtime. It makes data transformation easier to code, more scalable, and more efficient. With this feature, Apex developers can focus more on solving business problems and less on addressing the specifics of file formats.
This means you can create DataWeave scripts as metadata and invoke them directly from Apex, bringing the data transformation power from DataWeave into your Data Transformation needs in Salesforce.
Like Apex, DataWeave scripts are run within Salesforce application servers, enforcing the same heap and CPU limits on the executing code, however more efficiently.
Here are some of its capabilities:
- Ability to parse CSV to JSON
- Serializing Apex Object with custom time formats
- Serializing and deserialising JSON with Apex Reserved Words
- Performing custom transformations like removing or adding namespaces. Or removing “__c”
- Performing RFC-compliant CSV parsing and writing
- Improved CPU performance without much code
3. Practical examples
Let’s imagine we have an organisation, for this example, it is called SyRog Energy Solutions, where they have a database as the source of truth, and where all the information about partners and the business is stored.
SyRog uses Salesforce as the CRM system that accesses and operates the data in the database. The business logic is arranged and enforced in a different program that connects to the database to feed the program and show the information to the company workers.
This connection is made with an intermediate layer to orchestrate the data, where best practices are used to develop their business in a scalable way for the future.
Data is exposed by a system layer that directly accesses the database, receives it in JSON format, and relays it to the intermediate layer.
Processing the data with APEX
Now that the data is unlocked, we need to process it. One approach is to make the whole interpretation of the received data and translation into APEX Objects.
This is a repetitive task that takes hours to produce something to start testing (if you’re a developer that had to deal with this already, you know what I mean) and requires several lines of code just to interpret and translate the information received, as we can see in the following two images.
Fig. 2: The Wrapper class to define a structure to translate the received JSON message to APEX Objects.
Fig. 3: Example of a method that makes an HTTP call and translates the response into APEX Object.
Processing the data with DataWeave
An alternative approach is to transform, filter and group the data using DataWeave.
When using DataWeave, it gets a lot simpler to do this translation, having only to specify the output format of the received data:
//DataWeave code in file ‘Customer_to_CustomerWrapper.dw’
%dw 2.0
input payload application/json
output application/apex
—
payload as Object {class: “CustomerWrapper”}
With this simple code, we can have the desired output format with little to no interpretation, ready to be used in APEX :
//apex code
String jsonResponse = ConnectionOutsideService.getMethod(…);
DataWeave.Script dwScript = DataWeave.Script.createScript(‘Customer_to_CustomerWrapper’);
DataWeave.Result dwResults = dwScript.execute(new Map<String, Object>{‘payload’ => jsonResponse});
List<CustomerWrapper> customers = (List<CustomerWrapper>)dwResults.getValue();
It could even be all compressed to a single line like:
List<CustomerWrapper> customers = (List<CustomerWrapper>) DataWeave.Script.createScript(‘Customer_to_CustomerWrapper’).execute(new Map<String, Object>{‘payload’ => jsonResponse}).getValue();
4. Final words
In conclusion, DataWeave is a functional programming language designed by MuleSoft. It allows to access and transform data that travels through a Mule application, which is now Open Source. It can also be included in your APEX developments.
DataWeave allows users to perform a common use case for integration developers efficiently: read and parse data from one format, transform it, and write it out in a different format. For example, a DataWeave script could take a simple CSV file and transform it into an array of complex JSON objects. Also, it could take in XML and write the data into a flat-file format. DataWeave allows the developer to focus on the transformation logic instead of worrying about the specifics of reading, parsing, and writing specific data formats in a performant way.
With this article, we wanted to show you what DataWeave can do for you as a developer and share the first steps to start working with it.
When you’re ready, explore the language with the playground and the VS Code extension, get familiar with it, and you can follow a tutorial to take your first steps. And a more extensive example can be found here.
In a future article, we will talk about taking the most out of DataWeave for data transformation.
In that article, we’ll describe how to create more complex logic in DataWeave, using essential functions of the DataWeave libraries and their operators.
Later, we will also show you how to create scripts, save them and call such scripts, or specific functions, with APEX and other DataWeave scripts.
Until then, feel free to drop us any questions, and we’ll gladly help you through your journey.
5. DataWeave useful links
Below, find some useful links to help you increase your knowledge in this programming language:
Start with DataWeave 2.4.0 for Mule Runtime Engine (Mule v4.4 and future ones): DataWeave Quickstart | MuleSoft Documentation
DataWeave in developers docs: Learn DataWeave with the Online DataWeave Playground and Interactive Tutorial
Learn more: DataWeave Language Guide | MuleSoft Documentation.
See references about the operators and functions: DataWeave Reference | MuleSoft Documentation.
See script examples to solve common transformation cases: DataWeave Examples | MuleSoft Documentation.
Test scripts interactively: DataWeave Interactive Learning Environment | MuleSoft Developers
Tutorial using the playground: https://developer.mulesoft.com/learn/dataweave/tutorial/
Forum with tips and best practices: https://mulesy.com/1-dataweave-tips-guidelines/
Libraries in exchange: https://www.youtube.com/watch?v=jyzIS8c_e34
DataWeave to use with APEX: https://www.linkedin.com/pulse/dataweave-apex-joey-chan/ | https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_DataWeaveInApex_DevPreview.htm&type=5&release=240
Now that you know more about this programming language, feel free to drop any questions. Also, don’t forget to subscribe to our Knowledge Center!
SUBSCRIBE KNOWLEDGE CENTER
Subscribe for free to Knowledge Center's monthly digest to receive the hottest news and newest posts directly on your Inbox.