At runtime, the wrapper that calls the decision service uses the configured custom functions. You export them using the following syntax:

module.exports={name of function as used in rulesheet:reference to custom function};

For example:

module.exports={getSessionData:getSessionData};

which can be abbreviated as module.exports ={getSessionData};

The following code shows a more complete example with two custom functions:

const sessionData = new Map();
sessionData.set('key1', true);
sessionData.set('key2', 'my session string');
sessionData.set('key3', 12);
																	
function getSessionData ( helper, name ) {
	if ( !sessionData.has(name) )
		return 'ERROR - no session data for ' + name;
	else
		return sessionData.get(name);
}
											
function getMoreData ( helper, name ) {
	return name;
}
																	
 module.exports = { getSessionData, "from session data": getMoreData };							  

These definitions enable you to use getSessionData from session data as custom function names in any of the simplified extended operators.

The custom functions can be:

  • Shared across several decision services.
  • Developed and managed with your own build pipeline.

The custom functions are specified in the implementation object using the attribute "customFunctions".

This attribute points to an array of object literals. Each object literal contains the information for one custom function.

The syntax of the object literal is:
{ <name of function as used in rulesheet>: <reference to custom function> }

For example, here is the implementation file for the custom function examples in previous section:

const sessionData = new Map();
sessionData.set('key1', true);
sessionData.set('key2', 'my session string');
sessionData.set('key3', 12);
sessionData.set('key4', '2021-02-10T00:00:00.000Z');
sessionData.set('key5', 10.23);
 
function getSessionData ( helper, name ) {
    console.log("Custom function called for " + name);
    if ( name === 'key4' )  // example showing how to construct a DateTime
        return helper.dateTime.constructDateTime(sessionData.get(name));
    else if ( name === 'key5' ) {// example showing how to construct a Decimal
        return helper.decimal.constructDecimal(sessionData.get(name));
    }
    else if ( !sessionData.has(name) )
        return 'ERROR - no session data for ' + name;
    else
        return sessionData.get(name);
    }
 
function setSessionData ( helper, name, value ) {
    if ( name === 'key4' )  // example showing how to construct a DateTime
         sessionData.set('key4', helper.dateTime.outputToJson(value));
    else if ( name === 'key5' ) {// example showing how to construct a Decimal
        sessionData.set('key5', helper.decimal.outputToJson(value) );
    }
    else
        sessionData.set(name, value);
}
For example, the following is a implementation for the custom function example:
const configuration = {
	logLevel: 0,
	customFunctions: [  {"getSessionData": getSessionData}, 
				 	 {"from data store":getMoreData}
				   ]
};

Get custom function signature

A custom function must have the following signature:

function <functionName> ( helper, name )

The name parameter contains the second argument string as entered in the Rulesheet editor.

For example, in the Rulesheet editor:
Ent1.str1 = getString('getSessionData','key2')

The getSessionData custom function name parameter will contain the string key2.

The helper function is an object literal containing references to Decimal and DateTime operators.

It has the following syntax:
helper={'decimal': <all decimal operators>,
        'dateTime': <all dateTime operators> }

How to write the implementation of custom functions for runtime

You can implement the body of a custom function with whatever logic your use case requires.

The only constraints are:

  • The custom function has the proper signature.
  • You return the proper object type. A getDecimal call needs to return a Decimal, likewise a getDateTime needs to return a DateTime.
  • You use the helper object to create or operate on Decimal and DateTime objects.

Custom GET functions

The following code shows how to construct and return a decimal and a dateTime:
const sessionData = new Map();
sessionData.set('key1', true);
sessionData.set('key2', 'my session string');
sessionData.set('key3', 12);

function getSessionData ( helper, name ) {
	if ( name === 'key4' ) { // example showing how to construct a DateTime
		return helper.dateTime.constructDateTime('2021-02-10T00:00:00.000Z');
	}
	else if ( name === 'key5' ) { // example showing how to construct a Decimal
		return helper.decimal.constructDecimal('10.23');
	}
	else if ( name === 'key6' ) { // example showing how to use additional built-in
							operators like now and addDays
		const dt = helper.dateTime.now();
		const dt2 = helper.dateTime.addDays(dt, 7);
		return dt2;
	}
	else if ( !sessionData.has(name) )
		return 'ERROR - no session data for ' + name;
	else
		return sessionData.get(name);
}
function getMoreData ( helper, name ) {
	return name;
}
module.exports = { getSessionData, "from session data": getMoreData };