Business Libraries
A Business Library is a set of JavaScript functions that can be reused in multiple business rules. The following should be reviewed when considering the use of a business library:
- When creating multiple JavaScript based business rules, a business library should be used to hold common functions. This makes maintaining the related business rules easier since the JavaScript code exists in a single location. For details, refer to Creating a Business Rule, Function, or Library here.
- Libraries cannot be called independently, but must be referenced from other actions or conditions.
- Any number of JavaScript business rules can be used to define library functions.
- It is not possible to bind STEP objects when working with libraries, but they can be passed in as arguments.
- For business rules where library functionality is needed, you must declare a dependency to the libraries on the Dependencies tab, as described in the Editing a Business Library documentation here.
As an example, the following library function could be used to check if a product is below another product:
// Checks whether a Product is below another Product
function isProductBelow(prod, checkProdID) {
if(!prod instanceof com.stibo.core.domain.Product) throw "Function only works with Products";
if(checkProdID == "Product hierarchy root") return true;
if(prod.getID() == "Product hierarchy root") throw "The top level Product is never below another Product.";
var currentParentId;
var currentProd = prod;
while(true) {
currentParentId = currentProd.getParent().getID();
if(currentParentId == "Product hierarchy root") return false;
else if (currentParentId == checkProdID) return true;
else currentProd = currentProd.getParent();
}
}
Condition using function (Library alias = “lib”, Current Object bound to variable "obj")
return lib.isProductBelow(obj, "ID of STEP Product");