Formulas JavaScript V2 Specs 🆕 v16
V2 formulas are a major enhancement to v1 formulas. Improvements include:
Intellisense while typing the formula using VS Code
Promise support
Ability to stop the formula from running
Ability to set other fields from a formula
Ability to add Comments before the formula
Ability to get App Settings in your formula
Ability to return Validation on a field
V2 Function Specs
// new formula syntax - see https://go.2sxc.org/formulas
v2((data, context) => {
return data.value;
});
All functions must be called
v2
, it must be lower case
This is important because when APIs change, we will use v3, etc.The first line in the code must start with the function name
v2
.
You can put comments or empty lines before thev2
, but they must always use//
and not/* */
The
v2(...)
function takes 1 parameter, namely the function to execute. This is different from v1.The reason we need an inner function is so that VS Code can provide intellisense while you type.
Your function must look like one of these
v2(() => { ... });
v2((data) => { ... });
v2((data, context) => { ... });
The parameter names are anything you want, but the order of what you get is always
data
and thencontext
. You can also writev2((d, c) => { ... });
V2 data
and context
Objects
👉🏼 See data
👉🏼 See context
V2 Returned Values
👉🏼 See Return Values
Tips and Tricks
- You can always add a
console.log(data, context);
or similar in your code to see in real time what's being processed. - You can also add a line
debugger;
and the browser will stop at this line so you can inspect the variables and watch your code. - For now, we strongly recommend to use Formulas as pure functions, but with experience the recommendation may change.
History
- Introduced in 2sxc 12.01
- Added
data.parameters
in v13.10 - Added
context.features
,context.app
,context.user
,context.sxc
etc. in v13.10 - Massively improved in v2 in 2sxc 16.00