Newer Version Available

This content describes an older version of this product. View Latest

Implementing DataWeave in Apex

Create DataWeave scripts as metadata and invoke them directly from Apex. Use class methods and exceptions in the DataWeave namespace to load and execute the scripts.

DataWeave Namespace

The DataWeave namespace provides classes and methods to support the invocation of DataWeave scripts from Apex. The Script class contains the createScript() method to load DataWeave scripts from .dwl metadata files that have been deployed to an org. The resulting script can then be run with a payload using the execute() method to obtain script output in a DataWeave.Result object. The Result class contains methods to retrieve script output using Script class methods. For more information on these classes and methods, see DataWeave Namespace.

For every DataWeave script, an inner class of type DataWeaveScriptResource.ScriptName is generated. The inner class extends the DataWeave.Script class. You can use the generated DataWeaveScriptResource.ScriptName class instead of using the actual script name via the createScript() method. DataWeave scripts that are currently being referenced via this inner class can't be deleted. To make the generated DataWeaveScriptResource class global, set the isGlobal field in the DataWeaveResource metadata object.

1<?xml version="1.0" encoding="UTF-8"?>
2<DataWeaveResource xmlns="http://soap.sforce.com/2006/04/metadata">
3<apiVersion>58.0</apiVersion>
4<isGlobal>true</isGlobal>
5</DataWeaveResource>

The catchable System.DataWeaveScriptException exception is available for error handling. Runtime script exceptions that occur within DataWeave are exposed to Apex with this exception type.

DataWeave scripts support logging using the log(string, value) function. Log messages that originate from DataWeave are reflected in Apex debug logs as DATAWEAVE_USER_DEBUG events, under the Apex Code log category at the DEBUG log level.

Supporting Information

These tools support the development of DataWeave scripts.

Versioned Behavior Changes

These versions of DataWeave script syntax are supported in Apex.