Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Prevent Large Data Queries in Salesforce with Transaction Security Policies
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Saturday, August 16
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Integration List
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    SalesforceCodex
    Home»Salesforce»Apex»Data Transformation with DataWeave in Salesforce Apex

    Data Transformation with DataWeave in Salesforce Apex

    Dhanik Lal SahniBy Dhanik Lal SahniFebruary 10, 2023Updated:June 11, 20234 Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Data Transforrmation using DataWeave | SalesforceCodex
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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.

    apex apex data tranformation architecture dataweave DataWeave in Apex DataWeave in Salesforce dataweave support in apex DataWeaveInApex dev hub salesforce salesforce apex scratch org script class sfdx sfdx cli spring23
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleGeneric Notification Component in LWC
    Next Article Verify Phone using Flow HTTP Callout
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Prevent Large Data Queries in Salesforce with Transaction Security Policies

    August 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 2025
    View 4 Comments

    4 Comments

    1. Pingback: Quick Text in Salesforce - SalesforceCodex

    2. Pingback: Apex Enhancement in Salesforce Winter ’23 - SalesforceCodex

    3. Pingback: Post Chatter Feed Using FeedItem in Flow - SalesforceCodex

    4. Pingback: What is Salesforce Genie? - Salesforce Codex

    Leave A Reply Cancel Reply

    Ranked #1 Salesforce Developer Blog by SalesforceBen.com
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (117) apex best practices (5) apex code best practice (10) apex code optimization (6) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (24) salesforce (151) salesforce apex (53) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (5) salesforce news (5) salesforce question (5) security (4) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    MailChimp

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Prevent Large Data Queries in Salesforce with Transaction Security Policies
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 10 Salesforce CRM Trends to Watch in 2025 July 18, 2025
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    Archives
    Categories
    Tags
    apex (117) apex best practices (5) apex code best practice (10) apex code optimization (6) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (24) salesforce (151) salesforce apex (53) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (5) salesforce news (5) salesforce question (5) security (4) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.