Skip to Content

JavaScript Jitterbit and common functions in Integration Studio

Jitterbit JavaScripts can access—in addition to the standard JavaScript functions that are part of ECMA-262 v5.1—these Jitterbit-specific functions. As ECMA-262 v5.1 is an older version of JavaScript than what is commonly available in browsers and other JavaScript engines, we've included tables of the common and standard JavaScript functions that are available in Jitterbit JavaScripts.

Important

For enhanced performance, we recommend using JavaScript only when a Jitterbit Script equivalent is not available.

Note

A maximum of 50,000 loop iterations is allowed for each JavaScript script. To increase the allowed number of iterations per script, see Loop iterations under JavaScript.

Jitterbit functions

Jitterbit.DbExecute

Declaration

array Jitterbit.DbExecute(string databaseId, string sql)

int Jitterbit.DbExecute(string databaseId, string sql, string outputVariable,...)

Syntax

Jitterbit.DbExecute(<databaseId>, <sql>)

Jitterbit.DbExecute(<databaseId>, <sql>, <outputVariable>,...])

Required parameters

  • databaseId: A string reference path to a Database connection in the current project
  • sql: The SQL command to be executed against the database
  • outputVariable: (Second form) An output parameter that is matched to fields returned in the SQL command. Additional arguments can be specified as required.

Description

Executes a SQL statement on a database and returns the results. See the Jitterbit Script DBExecute function for details.

The database used in this function call must be defined as a Database connection in the current project. For more information, see the instructions on inserting endpoints under Endpoints in JavaScript.

Jitterbit.DbLookup

Declaration

string Jitterbit.DbLookup(string databaseId, string sql)

Syntax

Jitterbit.DbLookup(<databaseId>, <sql>)

Required parameters

  • databaseId: A string reference path to a Database connection in the current project
  • sql: The SQL command to be executed against the database

Description

Executes a SQL statement on a database and returns the first field of the first result matching the specified criteria. See the Jitterbit Script DBLookup function for details.

The database used in this function call must be defined as a Database connection in the current project. For more information, see the instructions on inserting endpoints under Endpoints in JavaScript.

Jitterbit.GetVar

Declaration

string Jitterbit.GetVar(string jitterbitVariableName)

Syntax

Jitterbit.GetVar(<jitterbitVariableName>)

Required parameters

  • jitterbitVariableName: The name of a Jitterbit variable

Description

Returns the value of a Jitterbit variable (a predefined global variable that is built into the agent and begins with jitterbit.). The available variables can be seen in Integration Studio and are documented under Jitterbit variables.

Warning

It is also possible, but not recommended, to use the Jitterbit.GetVar function to retrieve the value of a user-defined global variable.

The Jitterbit.GetVar function is designed to allow the use of variables that contain periods within the variable name. However, using periods in a user-defined global variable name is not recommended. As the value will be converted to a string when the variable is set, these functions cannot be used with complex data types such as arrays, dictionaries, or JSON objects. Instead, it is recommended that you create global variables without periods and instead use underscores in place of periods and use the standard dollar sign $ syntax as described in Global variables.

Examples

// Retrieves the value of the Jitterbit variable "jitterbit.api.request"
var request = Jitterbit.GetVar("$jitterbit.api.request");

Jitterbit.ReadFile

Declaration

string Jitterbit.ReadFile(string sourceId[, string fileFilter])

Syntax

Jitterbit.ReadFile(<sourceId>[, <fileFilter>])

Required parameters

  • sourceId: A string reference path to an activity associated with a file-type endpoint in the current project

Optional parameters

  • fileFilter: File filter or filename to override the activity configuration

Description

Reads the contents of a file from a source.

The source used in this function call must be defined as an activity associated with a file-type endpoint in the current project. These include configured File Share, FTP, HTTP, Local Storage, and Temporary Storage activities. For more information, see the instructions on inserting endpoints under Endpoints in JavaScript.

The method returns the contents of the file pointed to by the source. If the source filter selects more than one file, the first one will be used. It is recommended to specify a source that uniquely identifies a single file.

The parameter fileFilter is optional and can be used to override the file filter used in the activity configuration. A filename can be used. Alternatively, a global variable can be used to override the file filter in the activity configuration. Global variables are referenced as [de_name] in the activity configuration.

If the ReadFile function fails, the operation does not fail. A script will abort, a warning added to the operation log, and the operation will continue.

This method can be used to read data from an HTTP source. In that case, all Jitterbit $jitterbit.source.http.* variables will be populated.

Warning

This function doesn't work reliably with files that have binary content, as it will usually read only a portion of the file. If the file has binary content, use the Jitterbit Script function Base64EncodeFile instead to read the entire file contents.

Examples

// Reads the first file retrieved from a source
var fileContents = Jitterbit.ReadFile("<TAG>activity:ftp/FTP Endpoint/ftp_read/FTP Files</TAG>");

// Use the filename "test.txt" instead of what is defined in the source
var fileContents = Jitterbit.ReadFile("<TAG>activity:ftp/FTP Endpoint/ftp_read/FTP Files</TAG>", "test.txt");

Jitterbit.SetVar

Declaration

Jitterbit.SetVar(string jitterbitVariableName, string value)

Syntax

Jitterbit.SetVar(<jitterbitVariableName>, <value>)

Required parameters

  • jitterbitVariableName: The name of a Jitterbit variable
  • value: A value to be assigned to the variable

Description

Sets the value of a Jitterbit variable (a predefined global variable that is built into the agent and begins with jitterbit.). The available variables can be seen in Integration Studio and are documented under Jitterbit variables.

Warning

It is also possible, but not recommended, to use the Jitterbit.SetVar function to set the value of a user-defined global variable.

The Jitterbit.SetVar function is designed to allow the use of variables that contain periods within the variable name. However, using periods in a user-defined global variable name is not recommended. As the value will be converted to a string when the variable is set, these functions cannot be used with complex data types such as arrays, dictionaries, or JSON objects. Instead, it is recommended that you create global variables without periods and instead use underscores in place of periods and use the standard dollar sign $ syntax as described in Global variables.

Examples

// Sets a Jitterbit variable
Jitterbit.SetVar("$jitterbit.scripting.db.max_rows", 1000);

Jitterbit.WriteFile

Declaration

void Jitterbit.WriteFile(string targetId, type fileContents[, string filename])

Syntax

Jitterbit.WriteFile(<targetId>, <fileContents>[, <filename>])

Required parameters

  • targetId: A string reference path to an activity associated with a file-type endpoint in the current project

Optional parameters

  • fileContents: Data to be written to the file
  • filename: Filename to override the activity configuration

Description

Writes the fileContents to the target specified by targetId. If fileContents is of type binary, the binary data is written to the file. In all other cases, a string representation of the data is written. The file is automatically flushed and closed by the function.

The target used in this function call must be defined as an activity associated with a file-type endpoint in the current project. These include configured File Share, FTP, HTTP, Local Storage, and Temporary Storage activities. For more information, see the instructions on inserting endpoints under Endpoints in JavaScript.

The third parameter, filename, is optional and can be used to override the filename used in the activity configuration. Alternatively, a global variable can be used to override the filename in the activity configuration. Global variables are referenced as [de_name] in the activity configuration.

This method can also be used to write/post data to an HTTP target. In that case, $jitterbit.target.http.* variables will be populated.

If the WriteFile() function fails, the operation does not fail. A script will abort, a warning added to the operation log, and the operation will continue.

Examples

//  Writes the value of $contents to the file defined by a target
Jitterbit.WriteFile("<TAG>activity:ftp/FTP Endpoint/ftp_write/FTP Files</TAG>", $contents);

// Use the filename "test.txt" instead of what is defined in the target
Jitterbit.WriteFile("<TAG>activity:ftp/FTP Endpoint/ftp_write/FTP Files</TAG>", $contents, "test.txt");

SetScriptOutput

Declaration

void SetScriptOutput(string data)

Syntax

SetScriptOutput(<data>)

Required parameters

  • data: Data to be returned by the script

Description

Sets the value returned by a script.

The return value of a script can be accessed as the return value of the RunScript function.

Examples

// In the calling Jitterbit Script script:
$result = RunScript("<TAG>script:CalculateSomething</TAG>", value_to_be_calculated);

// In the Jitterbit JavaScript script "CalculateSomething":
var calculated_value = ...;
...
SetScriptOutput(calculated_value);

SetScriptResult

Declaration

void SetScriptResult(string data)

Syntax

SetScriptResult(<data>)

Required parameters

  • data: Data to be returned by the script

Description

An alias for the SetScriptOutput function. See SetScriptOutput for details.

WriteToOperationLog

Declaration

string WriteToOperationLog(string message)

Syntax

WriteToOperationLog(<message>)

Required parameters

  • message: A string message

Description

Writes a message to the operation log.

Examples

// Writes a message to the operation log
WriteToOperationLog("The source field 'Price' has an invalid value.");

JavaScript common functions

These common JavaScript functions—part of ECMA-262 v5.1—are supported in Jitterbit JavaScripts.

Common Property Description
Array See the JavaScript array properties and functions
Date See the JavaScript date properties and functions
decodeURI Decodes a URI
decodeURIComponent Decodes a URI component
encodeURI Encodes a URI
encodeURIComponent Encodes a URI component
eval Evaluates a string and executes it as if it were JavaScript code
isFinite Tests if a value is a finite and legal number
isNaN Tests if a value is an illegal number
JSON.parse Parses a JSON string and returns a JavaScript object
JSON.stringify Converts a JavaScript object into a JSON string
Math See the JavaScript math properties and functions
Number Converts an object's value into a number
parseFloat Parses a string and returns a floating point number
parseInt Parses a string and returns an integer
String See the JavaScript string properties and functions