Custom Expression Function Scope

View as Markdown

Summary

  • Custom Expression Functions are available in every relevant Module by default
  • Limit an individual function with its own isAvailable property
  • Use isExpressionFunctionAvailable for one global rule covering many functions

By default a Custom Expression Function is available in all Modules that host that kind of Expression.

There are 2 ways in which this default scope can be restricted:

  • using the isAvailable property in the Function object
  • doing a global setting using the isExpressionFunctionAvailable property in Expression Options

Hint

  • Use isAvailable to keep the rule next to the function it applies to
  • Use the global override for one rule that covers many functions (including System functions)

Using isAvailable

The Function object allows you to configure which Modules can access your Custom Function.

This is done by using the isAvailable property.

Hint

Omit isAvailable (or return true from the callback) to keep the default: available in every relevant Module

The isAvailable property can be one of 2 types:

  • a hard-coded list of Modules
  • a function that receives the context and returns true / false

Hint

  • Use the function option when a hard-coded list of Modules is not enough
  • e.g. when the decision depends on context.module, context.functionName, or (for Column Filter) context.column
Developer Guide

Limiting Custom Expression Functions to specific Modules

expressionOptions: {
  customStandardFunctions: {
    THIS_BUSINESS_YEAR: {
      handler(args, context) { /* ... */ },
      returnType: 'boolean',
      operands: [['date']],
      isAvailable: ['Alert'],
    },
    USD_CONVERT: {
      handler(args, context) { /* ... */ },
      returnType: 'number',
      operands: [['number', 'text']],
      isAvailable: (context) => context.module === 'CalculatedColumn',
    },
  },
}
1
Register the Custom Expression Functions

Define the Custom Functions in expressionOptions.customStandardFunctions

2
The two example functions

In this example we provide 2 functions: THIS_BUSINESS_YEAR and USD_CONVERT

3
Limit them with isAvailable

In this example we use both forms of isAvailable:

Using a Global Override

The other way to limit which modules can access your function is by setting a global override.

This is achieved by leveraging the isExpressionFunctionAvailable property in Expression Options.

The callback receives GlobalExpressionFunctionAvailabilityContext (the per-function context plus defaultAvailability).

Note

  • This runs after each function's own isAvailable, and receives that result as context.defaultAvailability
  • It has the final say: it can hide a function, and it can also re-enable one that isAvailable excluded
// hide every Custom Function from the Grid Filter, while leaving all other decisions unchanged
expressionOptions: {
  isExpressionFunctionAvailable: (context) => {
    if (context.isCustom && context.module === 'GridFilter') {
      return false;
    }
    return context.defaultAvailability;
  },
}

Hint

  • Use it for a cross-cutting rule (hide a System operator everywhere, or vary Column Filter operators per Column)
  • Return context.defaultAvailability to keep the per-function result
AdapTableQL: Custom Functions Scope
Fork
  • This example creates 2 Custom Expression Functions, each limited to one Module using a different mechanism:
    • THIS_BUSINESS_YEAR — Alert only, via its own isAvailable property (hard-coded list)
    • USD_CONVERT — Calculated Column only, via the global isExpressionFunctionAvailable override in Expression Options
Try It Out
  • Open an Alerts Expression — THIS_BUSINESS_YEAR is in the Custom menu; USD_CONVERT is not
  • Open the Dollar Price Calculated Column — USD_CONVERT is listed; THIS_BUSINESS_YEAR is not
  • Open Format Column or Grid Filter — neither custom function is listed
Loading demo…

Caution

  • In both use cases, context.column is set only when AdapTable is resolving operators for a specific Column Filter
  • It is undefined on the module-level pass — guard before using it