Table of Contents

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.07
    • appId
    • zoneId
    • isContent
    • isSite
    • isGlobal
  • cache - an object which is only for this function and will be persisted across calls - use it to save temporary values
  • debug - a true/false toggle if the form is in develop/debug mode - Use this to show/hide really advanced fields. new in 12.02
  • target - everything about the target of the formula - the current field
    • type = What the function processes Field.Value or Field.Settings (Future: Form.Variable etc.)
    • name - field name or setting-name, so FirstName or Visible
    • entity
      • id - the id of the entity - 0 if it's new
      • guid - the GUID of the entity, always provided
      • type - entity type information
        • guid
        • name
  • culture
    • code
    • name - this will return undefined scenarios where no languages are activated
  • features new v13.10
    • isEnabled('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.
  • form new v13.10
    • runFormulas() (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.07
  • user new in v13.11
    • email new v16.00
    • guid new v16.00
    • id user id or -1 if anonymous
    • isAnonymous - true if the user is not logged in
    • isContentAdmin new v16.00
    • isSiteAdmin
    • isSystemAdmin
    • name new v16.00
    • username new 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.app in v13.10
  • Added context.user in v13.11
  • Added context.form.runFormulas() in v13.11

Shortlink to here: https://go.2sxc.org/js-fcontext