Formulas
Formulas are text templates sent to the JavaScript engine after parsing (i.e. after all merge field tokens have been replaced with real data). The JavaScript engine executes the formula and returns a single value to the caller.
Simple Formula Example
This simple formula represents an expression:
{!amount}*{!price}*(1-{!discount_proc}/100)
In more complex cases, you can define intermediate variables, flow-control operators, etc. For example, the following formula calculates monthly payments on a mortgage:
var q = {!mortgage_rate}/12/100; var A = parseInt('{!amount}',10); if (A<=0 || q<=0) return null; var N= parseInt('{!loan_type#code}',10); if (N<=0) return A*q; return A*q/(1-Math.pow(1+q, -N));
In such a case, the formula's body will be wrapped into a JavaScript function and the resultant function will represent the formula's result.
Including Your Own Functions in a Formula
You can include your own functions in a formula:
function formatNum(x) { if (x instanceof Number && !isNaN(x)) return x.toFixed(2); return ""; } function calcStr() { var y={!amount}*{!discount}; return formatNum(y); } calcStr();Important: If you are using custom-defined functions, your Formula cannot be automatically wrapped and hence cannot include a return statement outside of a function. In this case place exactly one function call at the end of your Formula as shown above. The result of this call will be used as the result of the entire Formula. The Formula's resulting value must be of a certain type that depends on where the Formula is used:
Using custom Java classes in formulas
In Platform Private Cloud, you can use custom Java classes in formulas. These
classes must be specified using the shared property CustomClassFilter
in the Shared Properties file.
See Shared Properties for more information.
Creating and Processing Errors
To interrupt normal flow of JavaScript execution you can throw exceptions. An error message will be displayed on the screen if UI is involved. For example:if ({!update_blocked}) throw "Update is blocked"; On the other side if you wish to process errors generated by part of your formula (for instance, Server-side API) you can use try / catch block. Example: try { rbv_api.update(...); // Call which may throw Exception } catch (e) { // Process Exception without terminating further execution }