Calling a Business Function from a JavaScript Business Rule

Below is an example of a business action that sends objects to a previously bound 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.

Note: As an alternative configuration to using a bind, business functions can be dynamically called and evaluated by ID using the getBusinessFunctionByID() method via getBusinessRuleHome(). However, since a business function returns data, carefully evaluate that data to ensure the expected outcome.

Refer to the 'Scripting API' section of the Technical Documentation accessible at [system]/sdk or from the Resources section of the system's Start Page.

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)

For details, refer to the 'Scripting API' section of the Technical Documentation, available at [system]/sdk or accessible from the Start Page.

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 as illustrated in the example below, it is recommended to instantiate the appropriate Java objects instead of relying on the JavaScript-number-to-Java-type conversion.

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:

In the Parameter field 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:

Note: When testing a JavaScript business function in the workbench using the Test & Time Business Rule dialog, the file size is limited to 1 MB. This limit only applies to the testing process and is not enforced when running the business function outside of the Test & Time Business Rule dialog.

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.