Script Class
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)
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)
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)
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.