Using Business Libraries
Business libraries are useful for writing JavaScript business rules once and referencing these rules again later even across multiple business rules. For more information, refer to the Business Library topic in this documentation here.
Structuring Business Libraries
Use libraries to encapsulate common code which allows it to be reused by multiple business rules. Split libraries according to their overall theme:
- WorkflowUtilities
- PackagingUtilities
- NumberFormattingUtilities
- ApprovalUtilities
Limiting Business Libraries
A business rule is compiled each time it is executed. Generally, it takes about 500 milliseconds to compile about 8,500 lines of code at each business rule execution. If a business rule depends on a library, the library is compiled as well. To expand it further, if a business rule only uses a single function within a library, the whole library is still compiled.
If a library depends on another library, the other library needs to be compiled as well.
Important: To reiterate, libraries are compiled every time the business rule is executed, which is especially burdensome to performance when libraries depend on each other. Dividing a large library into multiple libraries, but keeping the dependencies, does not resolve the issue.
The system caches the compiled scripts instead of recompiling them before each execution. By default, 100 business rules are cached (Script.CacheSize=100). When the cache is filled up, the least-used business rules are evicted from the cache.
The cache reduces performance impact, but it is still recommended to keep business rules libraries small to reliably improve performance.
An alternative to business rule libraries is business functions. A business function is one specific function, so unlike a library, there is not any additional unused functions included and compiled.
Business Functions
Business functions, introduced in the 9.0 release, were designed to be used for certain areas of the system where business actions or conditions was insufficient due to their lack of returning a result.
Since business functions can be used as part of a business action or condition, they also prove quite useful as a single function library. This use may be preferred to library since the function itself can be named to make sense in what it does and that the entire function is used each time. This approach opposes a library where often only parts of it is used at a given situation. One downside to a business function is that it cannot be used to alter data stored in the database.
A business function can be thought of as a JavaScript method with an input and an output. To that respect, the naming of the business function should be carefully considered so that the business function name reflects the functionality it offers.
Once a proper naming convention has been established, the description field of the business function should also be filled out. It is also advised to establish any limitations to the business function, if there are any, as well as indicating what the returned element is.
If there are input parameters to the business function, these should be properly named and described using the proper Description field when adding the parameter. Remember that this information is what the user of the business function must rely on to be able to choose the correct input parameter when calling the business function. It is important to also state assumptions / limitations if there are any, like 'parameter must not be null' or similar.
The business function code should adhere to good coding practices with comments where necessary. It is recommended to use JavaScript functions or binding other business functions to structure code. Calling the business function is done by parsing a JSON object containing the input parameters to the evaluate function.
Business functions are recommended for obfuscating code, as the business function can be used as a black box, like when using libraries. Unlike libraries, a business function's purpose is more obvious. On the downside, the business function runs in non-transactional scope, and thus, cannot alter the database in any way.
One additional advantage of using the business function over the library is that the business function offers 'Usage' information about which business rules have binds to it.