Script Class
Contains the createScript() method to load DataWeave scripts and the
execute() method to obtain script output in a
DataWeave.Result object.
Namespace
This example runs a DataWeave script from Apex and retrieves the resulting script output. First deploy the script to the org as ContactsToJson.dwl.
1%dw 2.0
2input records application/java
3output application/json
4---
5{
6 users: records map(record) -> {
7 firstName: record.FirstName,
8 lastName: record.LastName
9 }
10Then, execute the script from Apex.
1List<Contact> data = [SELECT FirstName, LastName FROM Contact WHERE LastName LIMIT 5];
2Map<String, Object> args = new Map<String, Object>{ 'records' => data };
3DataWeave.Script script = DataWeave.Script.createScript('ContactsToJson');
4
5DataWeave.Result result = script.execute(args);
6string jsonOutput = result.getValueAsString();Script Methods
The following are methods for Script.
createScript(scriptName)
Loads a DataWeave 2.0 script from the .dwl metadata file that is
deployed in an org. The script can then be run using the Script.execute
method.
Signature
public static createScript(String scriptName)
Parameters
- scriptName
- Type: String
- The name of the deployed metadata .dwl script (not including the file extension).
Return Value
Type: DataWeave.Script
DataWeave script that is used as a parameter in the Script.execute() method.
createScript(namespace, scriptName)
Loads a DataWeave 2.0 script from a specified namespace. The script can then be run
using the Script.execute method.
Signature
public static dataweave.Script createScript(String namespace, String scriptName)
Parameters
Return Value
Type: DataWeave.Script
DataWeave script that is used as a parameter in the Script.execute() method.
execute(parameters)
Executes the DataWeave script that is loaded using the
createScript() method and returns the script output.
Signature
public execute(Map<String,Object> parameters)
Parameters
- parameters
- Type: Map<String,Object>
- Input to the DataWeave script. The keys correspond to the input directive names defined in the DataWeave header.
- See Input Directive and DataWeave Header.