Using DataWeave in Salesforce

DataWeave by Mulesoft is a powerful tool for data transformation available to us on the Salesforce platform. But how can we use it in a real-life scenario?

Transforming Incoming Data with Reserved Keywords

One of the most useful cases is transforming incoming data that contains reserved keywords (for example, ‘number’ or ‘date’).

Prior to DataWeave, we had a couple of options – a quick and dirty replace of any text containing ‘number’ to something like ‘numeric’ in the JSON string prior to deserialization or build a custom JSON parser yourself to do the job.

The string replacement was unreliable, and the JSON parser required a lot of work and extra code to maintain.

Enter Dataweave!

DataWeave

A fairly simple script can be created that can handle this scenario – and it’s more reliable and much easier (after you figure out how to use DataWeave) than the alternatives.

Salesforce provides a jumping-off point by providing a repo of examples. They even have a reserved keyword example that works…for one object. Not an array, which is, unfortunately, a common response type.

%dw 2.0 
input payload application/json
output application/json
fun renameKey(key: Key) = key match {
    case "private" -> "isPrivate"
    case "object" -> "obj"
    case "currency" -> "currency_x"
    else -> (key)
}
--- 
payload map (record) -> (
    record mapObject (value, key, index) -> {
        (renameKey(key)) : value
    }
)

The structure of the code is as follows:

  • The DataWeave version number
  • Input output definitions (with explicit data type specifications)
  • Functions – in this case, the actual mapping from a reserved keyword to something acceptable to Salesforce.
  • The actual invocation of the input and resulting output.

DataWeave scripts are functional, i.e., they look weird and are unintuitive unless you are a math major.

Suffice it to say, the function at the end of the script above should be modified with care!!

Handling Multiple Objects

Onto the updated version that handles multiple objects:

%dw 2.0
output application/json
input data application/json
fun renameKey(key: Key) = key match {
   case "number" -> "identifier"
   case "private" -> "secret"
   else -> (key)
}

fun recurseObject(obj) =
   if (obj is Object)
     obj mapObject ((value, key) -> (renameKey(key)) : recurseObject(value))
   else if (obj is Array)
     obj map ((item) -> recurseObject(item))
   else
     obj

---
recurseObject(data)

As you can see, this has a similar structure to the first version. It has the rename key section but now has a new function that iterates on the input.

It’s a small recursive function that checks the input type and only changes the key if it’s an object.

If it’s an array, it just continues deeper into the object.

Finally, it’s invoked right at the bottom with the JSON data.

To use this, you could invoke by passing in the HttpRequest body JSON:

public override String preprocessJson(String bodyJson) {
   DataWeave.Script script = new DataWeaveScriptResource.reservedKeywords();
   DataWeave.Result result = script.execute(new Map<String, Object>{ 'data' => bodyJson });
   return result.getValueAsString();
 }

Note one important thing – in the execute script, you must pass in an object with the key named the same as your input declaration in the main script. In this case, the key is called “data.”

Further Customizing Your Salesforce Capabilities

This is just one example of how our team of Salesforce experts helps organizations achieve more with their Salesforce org. If you need insights on how to do more on the platform, our team of experienced architects can help. Contact us to talk with a consultant today.

Leave a Comment

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

We're celebrating 20 years! Read about our journey here.

Party horn and confetti
Scroll to Top