Calling a Business Function from a JavaScript Business Rule

Below, you will find an example of a business action that will send objects to a previously configured business function to generate the desired output. To set up a business action that calls business functions, refer to the Business Action: Execute JavaScript topic in the Business Action documentation here.

Note: To evaluate a business function from another JavaScript-based business rule, the business function must be bound to a JavaScript variable.

The bind provides an object implementing the BusinessFunctionScriptingProxy interface for which a user may use the following methods to evaluate the bound business function. These methods are:

  • evaluate()
  • evaluate(Manager manager, java.util.Map<java.lang.String,java.lang.Object> inputParameters)
  • evaluate(java.util.Map<java.lang.String,java.lang.Object> inputParameters)

Refer to the scripting API Javadoc in the Technical Documentation for more information.

While it is possible to instantiate the Java Map in JavaScript, an easier option is to create a JavaScript object for the input parameters as this will be converted to a Map upon execution. Information about the expected input parameters and the output that the function will produce can be found in the bind section.

Note: When calling business functions with Double or Integer input parameters, it is recommended to instantiate the appropriate Java objects instead of relying on the JavaScript-number-to-Java-type conversion. For example:

var params = {};
params.aDoubleParameter = new java.lang.Double(2.4);
params.anIntegerParameter = new java.lang.Integer(7);
var result = bf.evaluate(params);

When properly configured, the business action can look like the following:

Notice in the Parameter field in the Binds section for the called Business Function, the business function details what type of output can be expected, what variables to provide values for, and what type they must be.

var params = {};
params.accReferenceType = refType;
params.accDescAttribute = descAttribute;
params.product = product;
var text = bf.evaluate(params);
logger.info(text);
// Code setting the produced value omitted

Testing this business action shows the proper results:

In the 'Log' field, the value of the called attribute is shown. In the above example, the value returned from the business function and associated with text is logged. On the AC-P7000-65 product, there are two optional accessories, a 7.1 sound bar and a deluxe TV stand:

For the 7.1 Acme Sound Bar product, the short item description is:

For the Deluxe TV Stand, the short item description is:

In the test, both of these attributes are returned per the JavaScript function as shown in the Log parameter on the Test & Time Business Rule image above.