Newer Version Available

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

Script Class

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

Namespace

Feature is available as a developer preview. Feature isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements. All commands, parameters, and other features are subject to change or deprecation at any time, with or without notice. Don't implement functionality developed with these commands or tools in a production environment. You can provide feedback and suggestions for the DataWeave in Apex feature in the Trailblazer Community.

Note

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

Feature is available as a developer preview. Feature isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements. All commands, parameters, and other features are subject to change or deprecation at any time, with or without notice. Don't implement functionality developed with these commands or tools in a production environment. You can provide feedback and suggestions for the DataWeave in Apex feature in the Trailblazer Community.

Note

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.

execute(parameters)

Executes the DataWeave script that is loaded using the createScript() method and returns the script output.

Signature

Feature is available as a developer preview. Feature isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements. All commands, parameters, and other features are subject to change or deprecation at any time, with or without notice. Don't implement functionality developed with these commands or tools in a production environment. You can provide feedback and suggestions for the DataWeave in Apex feature in the Trailblazer Community.

Note

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

Feature is available as a developer preview. Feature isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements. All commands, parameters, and other features are subject to change or deprecation at any time, with or without notice. Don't implement functionality developed with these commands or tools in a production environment. You can provide feedback and suggestions for the DataWeave in Apex feature in the Trailblazer Community.

Note

public String toString()

Return Value

Type: String