JavaScript task

The JavaScript task lets you write custom JavaScript code snippets for your integration.

Using the JavaScript Editor, you can code complex data mapping logic for your integration, perform variable assignments, and add or modify integration variables.

The JavaScript Editor supports the following features:

  • A minimap that displays a high-level overview of the script code and helps with quick navigation.
  • Syntax/code highlighting, indentation, and line numbering for easier code readability and structure recognition.
  • Error highlighting to find and track errors in code.
  • Folding to expand and collapse regions of the script code.
  • Click-to-copy option to copy the script code to the clipboard.

Configure the JavaScript task

To add a JavaScript task to your integration, perform the following steps:

  1. In the Apigee UI, select your Apigee Organization.
  2. Click Develop > Integrations.
  3. Select an existing integration or create a new integration by clicking Create Integration.

    If you are creating a new integration:

    1. Enter a name and description in the Create Integration dialog.
    2. Select a Region for the integration from the list of supported regions.
    3. Click Create.

    This opens the integration in the integration designer.

  4. In the integration designer navigation bar, click +Add a task/trigger > Tasks to view the list of available tasks.
  5. Click and place the JavaScript element in the integration editor.
  6. Click the JavaScript element on the designer to view the JavaScript task configuration pane.
  7. Click Open script editor to view and edit the JavaScript Editor.
  8. Write your JavaScript code inside the executesScript(event) function that is automatically created in the JavaScript Editor. Close the editor once complete, any changes will be autosaved.

    For information about accessing integration variables and the supported functions, see Using the JavaScript Editor.

    For information about viewing the generated execution logs, see Execution logs.

The following image shows a sample layout of the JavaScript Editor: image showing javascript editor image showing javascript editor

Using the JavaScript Editor

To view and edit the JavaScript Editor, go to the JavaScript task configuration pane and click Open script editor. The JavaScript Editor by default contains a function named executesScript(event).

Where:

  • executesScript() is the function that is called when Apigee Integration executes the JavaScript task during an integration's run.
  • event is the in-memory object of Apigee Integration.

    For information about the supported methods of the event object, see Access integration variables.

The JavaScript execution engine that Apigee Integration uses is based on Rhino 1.7.14 which doesn't support all ES6 syntax. Only ES5 syntax autocompletion is supported. To view all of the supported ES6 syntax, see Rhino ES2015 Support.

Access integration variables

Variables defined in your integration can be accessed from the JavaScript Editor using the Apigee Integration in-memory event object.

The following methods are supported to access integration variables from your JavaScript Editor:

  1. getEventExecutionInfoId
  2. getGoogleCloudProjectId
  3. getIntegrationName
  4. getParameter
  5. getRegion
  6. log
  7. setParameter
Function name Description Usage

getEventExecutionInfoId

Returns the integration's run execution ID.

Return type: String

Syntax: event.getEventExecutionInfoId()

Example:

function executeScript(event) {
  event.getEventExecutionInfoId();
  }
      

getGoogleCloudProjectId

Returns the Google Cloud project ID.

Return type: String

Syntax: event.getGoogleCloudProjectId()

Example:

function executeScript(event) {
  event.getGcpProjectId();
  }

getIntegrationName

Returns the current name of the integration.

Return type: String

Syntax: event.getIntegrationName

Example:

function executeScript(event) {
  event.getIntegrationName();
  }

getParameter

Returns the value of the provided integration variable.

Return type: Data type of the integration variable

Syntax: event.getParameter("value")

Input parameter: Integration variable name.

Example:

function executeScript(event) { 
  event.getParameter("var1");
  }

getRegion

Returns the name of the integration region.

Return type: String

Syntax: event.getRegion()

Example:

function executeScript(event) {
  event.getRegion();
  }

log

Writes the specified value to the execution logs.

For information about viewing the generated execution logs, see Execution logs.

Syntax: event.log(value)

Input parameter: Any variable or function that is used in the script.

Example 1:

function executeScript(event) {
  event.log(event.getParameter("var1"));
  }

Example 2:

function executeScript(event) {
  event.log("Lorem ipsum");
  }

setParameter

Sets or updates the value of an integration variable.

Syntax: event.setParameter(value, value)

Input parameter: This function takes the following arguments:

  • First argument: Variable name.
  • Second argument: Value.

Example:

function executeScript(event) {
  event.setParameter("var1", "NewStringValue");
  }

Error handling strategy

An error handling strategy for a task specifies the action to take if the task fails due to a temporary error. For information about how to use an error handling strategy, and to know about the different types of error handling strategies, see Error handling strategies.