context Object in Formulas
The context contain additional information about the context we're running in.
app- contains information about the current app new 13.07appIdzoneIdisContentisSiteisGlobal
cache- an object which is only for this function and will be persisted across calls - use it to save temporary valuesdebug- a true/false toggle if the form is in develop/debug mode - Use this to show/hide really advanced fields. new in 12.02target- everything about the target of the formula - the current fieldtype= What the function processesField.ValueorField.Settings(Future:Form.Variableetc.)name- field name or setting-name, soFirstNameorVisibleentityid- the id of the entity - 0 if it's newguid- the GUID of the entity, always providedtype- entity type informationguidname
culturecodename- this will returnundefinedscenarios where no languages are activated
featuresnew v13.10isEnabled('FeatureName')- will return true if this feature is enabled.
Important: only admins users will know about all available features, non-admins will only have a subset marked as public.
formnew v13.10(deprecated in v2) - will run all formulas in the current form. This is typically meant for use in fetch-promises after the data returned and was put in the cache new 13.07runFormulas()
usernew in v13.11emailnew v16.00guidnew v16.00iduser id or-1if anonymousisAnonymous-trueif the user is not logged inisContentAdminnew v16.00isSiteAdminisSystemAdminnamenew v16.00usernamenew v16.00
experimental- this is for internal APIs we're testing, they are not public. You can use them, but expect the APIs to change in near future
Using the context.cache
In some cases you may want to remember a result of intermediate work. For this you can use the context.cache object. A simple exammple would be if you only want to run something once, in which case you could write something like this
v1(data, context) {
// don't do anything on following runs / return existing value
if(context.cache.notFirstRun) return data.value;
context.cache.notFirstRun = true;
return true;
}
History
- Introduced in 2sxc 12.01
- Added
context.features.isEnabled('FeatureName')in v13.10 - Added
context.features.get('FeatureName')in v13.10 - Added
context.appin v13.10 - Added
context.userin v13.11 - Added
context.form.runFormulas()in v13.11
Shortlink to here: https://go.2sxc.org/js-fcontext