Home SalesforceApex Data Transformation with DataWeave in Salesforce Apex

Data Transformation with DataWeave in Salesforce Apex

by Dhanik Lal Sahni
Data Transforrmation using DataWeave | SalesforceCodex

While integrating Salesforce with other third-party applications (API), we need to transform our data into the required format. Maybe other applications will return data in XML/CSV format and we need to convert it to JSON format for our usage. We have to write data wrapper classes to convert this. Now we don’t need to write a separate wrapper class for the serialization of data. We can use MuleSoft DataWeave in Salesforce Apex. This post will explain how to use data transformation with DataWeave in Salesforce Apex.

What is DataWeave?

DataWeave is the MuleSoft expression language for accessing, parsing, and transforming data. We can use this language to transform data into any format like XML to JSON, JSON to CSV, CSV to XML, etc.

Benefits of using DataWeave

  1. We can use DataWeave to Serialize data in the required format. This can serialize the Apex reserve keyword as well. So no property name change is required at serialization.
  2. Code maintenance will become easy, as we need to put data transformation logic in a separate file. No code change will require when we need to add/update fields/properties.
  3. Multiple developers can work on the task as 1 developer can work on Apex and another can work on Data Weave usage.

How to use DataWeave in Apex?

To use DataWeave in Apex we have to create a separate configuration file that will hold data transformation logic. Similar to LWC we can not create a data wave file in the Developer Console, we will use VS Code for creating a data weave file.

Note: This feature is introduced in Spring’23 so you should have Spring ’23 org.

By visiting Salesforce Trust, you can check whether your org has Spring’23 updates. Get your Org’s instance name from Setup->Company Information and check the detail on Salesforce Trust. If your Org does not have Spring’23 changes then create a new Org from Spring’23 Pre Release.

Below steps are required to use DataWeave in Apex

  1. Setup VS Project For DataWeave
  2. Create a Data Weave configuration file
  3. Use DataWeave File in Apex Code
  4. Test Code

1. Setup VS Project For DataWeave

DataWeave right now can only be tested using Dev Hub and Scratch Org combination. Perform the below steps to set up the DataWeave project in VS Code.

a. Create folder dataWeave in your local folder and open it in Visual Studio Code

b. Create a new project with the name dataWeaveTest in the same folder. It will create a Salesforce boilerplate code structure.

DataWeave VS Code Project | SalesforceCodex

c. Authorize your project with Dev Hub. You can use SFDX: Authorize Dev Hub from the command palette or can execute the below command in a terminal window

DataWeave Authorize Dev Hub  | SalesforceCodex
sfdx force:auth:web:login --setalias dataWeaveTest --setdefaultdevhubusername

d. Change the scratch org definition file (project-scratch-def.json) located in the config folder. It should be like this code.

DataWeaveInApex is only supported when the SFDX CLI version is above 7.15. So update the CLI version before working on this project. My version is 7.186.2.

e. Create a default scratch org using the above scratch definition file. You can use SFDX: Create a Default Scratch Org from the command palette or can execute the command

sfdx force:org:create -f config\project-scratch-def.json --setalias dwOrg --durationdays 30 --setdefaultusername --json
DataWeave Create Scratch Org  | SalesforceCodex

This will create a scratch org with DataWeave feature support.

2. Create a Data Weave configuration file

As mentioned above we have to create a data weave folder named dw parallel to lwc folder in VS project. It will be created under force-app\main\default.

DataWeave Project Folder Structure | SalesforceCodex

Let us take an example we need to convert one file which has a tax rate in XML format. This file can hold multiple XML records. Here is a sample of 1 XML record which we will transform into JSON.

Now based on the requirement, create two files – one for data transformation and the other for the metadata of that file.

First, create DataWeave file – taxRateXmlToJson.dwl which will convert tax rates XML data to JSON. We have a sample tax rate file on GitHub.

DataWeave file has certain rules to convert into another format. You can check all those rules/specifications available at DataWeave Scripts

DataWeave Transformation files:

Push the above files to scratch org using View -> Command Palette->SFDX: Push Source to Default Scratch Org or using the below command

sfdx force:source:push

3. Use DataWeave File in Apex Code

We have a data transformation dwl file now. Let us create an apex code that will use that configuration file and convert XML file data into JSON array. For testing purposes, we will upload our XML file in the Case record. To upload a file, open scratch org using the command palette. Download the file from GitHub and upload it to the Case record.

You can use View -> Command Palette -> SFDX: Open Default Org to open default scratch org

Transformation Logic:

We will use Script.createScript and execute methods for data transformation.

createScript(scriptName) – This will load DataWeave 2.0 script from the .dwl metadata file.

execute(parameters) – Executes the DataWeave script that is loaded using the createScript() method and returns the script output.

//taxRateXmlToJson is dwl file name. So put same name here
Dataweave.Script script = Dataweave.Script.createScript(
    'taxRateXmlToJson'
);

//Execute method takes in a map of input parameters. payload is variable which data will be passed. This is referenced in dwl file. 
DataWeave.Result dwresult = script.execute(new Map<String, Object>{
    'payload' => ver.VersionData.toString()
}); 

Complete Apex Code

Push apex files to scratch org using View -> Command Palette->SFDX: Push Source to Default Scratch Org or using the below command

sfdx force:source:push

4. Test Code

Now let us update the apex file located under \scripts\apex\hello.apex and put the below code to execute

taxRateTransformationService tran=new taxRateTransformationService();
tran.transform();
tran.transformFile('5005D00000930NXQAY');
Execute Code in VS Code  | SalesforceCodex

Test Video

References:

Script Class

DataWeave Scripts

DataWeave Examples

DataWeave Reference

Set Up Your Salesforce DX Environment

Extract License Plate Number from Image In Salesforce

Need Help?

Need some kind of help in implementing this feature, connect on linked-in profile.

You may also like

4 comments

Quick Text in Salesforce - SalesforceCodex June 12, 2023 - 9:38 am

[…] Data Transformation with DataWeave in Salesforce Apex […]

Reply
Apex Enhancement in Salesforce Winter ’23 - SalesforceCodex June 12, 2023 - 9:45 am

[…] Data Transformation with DataWeave in Salesforce Apex […]

Reply
Post Chatter Feed Using FeedItem in Flow - SalesforceCodex June 13, 2023 - 11:34 am

[…] Data Transformation with DataWeave in Salesforce Apex […]

Reply
What is Salesforce Genie? - Salesforce Codex June 13, 2023 - 11:34 am

[…] Data Transformation with DataWeave in Salesforce Apex […]

Reply

Leave a Comment