DataWeave in Apex

DataWeave is a programming language, created by MuleSoft for accessing and transforming data. Salesforce allows to use DataWeave scripts in Apex, to simplify complex transformation…

Have you ever wonder if you can use simpler ways for transforming data in Apex?

Do you know about DataWeave language?

If not, let’s explore it!

DataWeave is a programming language, created by MuleSoft for accessing and transforming data. Salesforce allows to use DataWeave scripts in Apex, to simplify complex transformation processing.

To use DataWeave, you must first create the DataWeave Resource, with the transformation script.

apex
%dw 2.0

//Header, contains all definitions (vars, input, output)

input payload application/xml
output application/apex
---

//Body, contains transformations

payload.Accounts.*Account map(record) -> {
    Name: record.name,
    Phone: record.phone,
    BillingCountry: record.planet,
    BillingCity: record.region,
} as Object {class: "Account"}

This script is getting XML Account data as input, and returning Salesforce Accounts.

Once DataWeave Resource is created, you can use it directly in Apex to transform the data.

apex
String inputXML =
'<?xml version="1.0" encoding="UTF-8"?>\n' +
'<Accounts>\n' +
    '<Account>\n' +
        '<name>Jabba\'s Palace</name>\n' +
        '<region>Desert of Tatooine</region>\n' +
        '<planet>Tatooine</planet>\n' +
        '<phone>(555) 555-1212</phone>\n' +
    '</Account>\n' +
    '<Account>\n' +
        '<name>Mos Eisley Cantina</name>\n' +
        '<region>Mos Eisley</region>\n' +
        '<planet>Tatooine</planet>\n' +
        '<phone>(555) 555-1212</phone>\n' +
    '</Account>\n' +
    '<Account>\n' +
        '<name>Imperial Palace</name>\n' +
        '<region>Coruscant</region>\n' +
        '<planet>Coruscant</planet>\n' +
        '<phone>(555) 555-1212</phone>\n' +
    '</Account>\n' +
'</Accounts>';

DataWeave.Script script = new DataWeaveScriptResource.xmlToAccount();
DataWeave.Result result = script.execute(new Map<String, Object>{'payload' => inputXML});
List<Account> accounts = (List<Account>) result.getValue();

This way, we can avoid complex transformations in Apex, in favor of the DataWeave language that is designed for that purpose.

But should we always use it in case of any transformations?

DataWeave execution is taking noticeable CPU time cost for initialization and transformation (but almost not affected by data volume that is transformed).

✅ When to use it?

Any transformations that are complex or contains huge volume of data Error prone formats

❌ When to avoid it?

Small transformations with a small volume of data Frequent transformations that need to be performed as quickly as possible Most UI-initiated synchronous process

Other Considerations

Available without any additional licenses Maximum 50 DataWeave Resources per org Importing modules/scripts not supported (but allows built in modules, i.e. dw::Core)

Want to try it?

You can use DataWeave Playground site!

https://dataweave.mulesoft.com/learn/dataweave

Text and code were extracted from the original slide. Plain-text version of the whole catalog