Java vs. JavaScript

When writing JavaScript Business Rules it is important to understand that the public Scripting API is a Java API. Thus all returned objects will be Java objects, not JavaScript objects.

For example, consider getting the length of the value for the attribute 'Description'. The following script will not work because the Java String returned by getSimpleValue() does not have a length property.

Copy
var length = node.getValue("Description").getSimpleValue().length;

To achieve this, you must use one of the following options:

  • Use the Java length() method
Copy
var length = node.getValue("Description").getSimpleValue().length();
  • Convert the value to a JavaScript string first
Copy
var value = node.getValue("Description").getSimpleValue() + "";
var length = value.length;

In general, be careful when making comparisons. As illustrated below, especially when using the 'strict' comparison operators, this can very easily lead to errors. The value for attribute with ID 'attID' on current object is '3' (validation base type will not matter as the value is output as a String), and the variable 'node' holds current object.

Notice the result of each test as indicated by the commented text (following //):

Copy
var value = node.getValue("attID").getSimpleValue(); 
logger.warning(value == 3); // WARNING: true
logger.warning(value.equals(3)); // WARNING: false
logger.warning(value.equals("3")); // WARNING: true
logger.warning(value === 3); // WARNING: false
logger.warning(value === "3"); // WARNING: false
logger.warning(value > 2); // WARNING: true
logger.warning(value <= 3); // WARNING: true