Global Object

The Global object contains all functions and fields that are available from any place in the script. This is the lowest scope in any javascript script. Using any of these functions or fields is as simple as typing their name, with parameters if necessary.

You can use the Global API objects in custom Client and Server scripts using Business Rules Editor. The next example demonstrates how a Global object function can be used:

function beforePost(row) {
  log('Scripting: beforePost(row) script function fires.');
  row['CustId'] = newGuid()
  if (row['Discount'] > 50)
  {
    log('Scripting: validation failed.');
    fail('Scripting rule: Discount has to be less than 50.');
  }
  if (row['Discount'] < 0)
  {
    log('Scripting: validation failed.');
    fail('Scripting rule: Discount has to be equal to or more than 0.');
  }
  log('Scripting: validation passed.');
}

The example logs (using Global Object function log) beforePost event messages and fails (using Global Object function fail), if Discount value is out of range. Also newGuid() function is used at the script to create new unique GUID value.

Properties

connections variable

An instance of the ConnectionsInfo object type. Contains a list of all connections available in this DA server. This object can be used to find out what connections are available from this server project even change the currently active connection.

The code snippet below shows, how the connections object can be used:

var SQLiteConnection = connections.find('PCTrade.SQLite');
if (SQLiteConnection != null)
{
   log('SQLite connection exists');
}

isNestedEvent

Returns true if the specified event is nested. Otherwise returns false.

The code snippet below shows, how the isNestedEvent property can be used:

if (isNestedEvent)
  log('Current event is nested');

isServer

Will be true on a server, false on a client. Client side scripts can be marked as both client & server, this field lets the scripts find out where they are running.

The code snippet below shows, how isServer property can be used:

if (isServer)
{
   log('Server side script');
}

lda

Returns an instance of the LocalDataAdapter Object, this can be used to fetch any data from within the script. Scripts would generally use this to fetch additional data, or check if the calling user actually has access to that particular record, during fetching.

The code snippet below shows, how the lda object can be used:

lda.insert("History", { Date = new Date(), RowID = change["ID"], UserID = session.["UserID"] } );

schema

Returns the current schema. Use this to find out how the tables look like and what other tables are available from the script.

The code snippet below shows, how the schema object can be used:

var WorkersDT = schema.findDataTable('Workers');
if (WorkersDT != null)
{
  log('Data table name: '+WorkersDT.name);
  var Fields = WorkersDT.fields;
}

schemas

When using scripting from Relativity Server, this variable will return a object, providing access (by index and name) of all schemas available in the Relativity instance.

The code snippet below shows, how the schemas object can be used:

var SchemaCount = schemas.count
for (i=0; i < SchemaCount; i++)
{
  Schema = schemas[i];
  DataTable = Schema.findDataTable('Workers');
  . . .
}

session

Returns the current session instance. This can be used to get or set any session variables; like user roles, and to store information between calls.

The code snippet below shows, how session object can be used:

log('roles = '+session.roles);
session.addRole('custom role');

Functions

newGuid()

Creates and returns a new, unique GUID value.

The code snippet below shows, how the newGuid function can be used:

row['Id'] = newGuid();

fail(args)

Throws an exception with the args argument. Use this to pass an error back to the client (when on the server), or to cause a validation error in the client.

The code snippet below shows, how the fail function can be used:

fail('Scripting rule: Discount has to be less than 50.');

log(args)

Calls the Log event on the script engine, can be used for diagnostics. This triggers the "LogMessage" event on the which can be used for debugging.

The code snippet below shows, how the log function can be used:

log('Scripting: beforePost(row) script function fires.');