Script Class

Contains the createScript() method to load DataWeave scripts and the execute() method to obtain script output in a DataWeave.Result object.

Namespace

DataWeave

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  }
10

Then, 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

  • namespace:

    Type: String

    The namespace name for the deployed script. If the namespace name is null, the caller namespace is used. If the namespace name is empty, the org namespace is used.

  • 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.

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.

Return Value

Type: DataWeave.Result

The DataWeave.Result object contains the script output.

toString()

Returns the name of the script.

Signature

public String toString()

Return Value

Type: String