Guide

Build models

Functions

3 min readOpen in Neblex

Functions are short scripts for what rules cannot express with conditions alone: calculations across many attributes, lookups in your data tables, calls to other systems, and building a whole set of prices or parts at once. A function reads the configuration and returns its effects, which are applied together with the rules' effects. Open them from Config › Functions.

Create a function

  1. Describe it. Click + Function, give it a Name, a Description and a Category (Pricing, Validation, Transformation, Display or Custom).
  2. Choose the models. Under Assignment Scope choose All Models or Selected Models and pick them. One function can serve several models, for example a shipping calculation.
  3. Choose when it runs. List the Watched attributes: the function runs when one of them changes. Leave the list empty to run it on every evaluation. Tick Run on first load when it must also run before the first change, for example to fill in starting values.
  4. Write the script and test it, as described below.
  5. Save it. The test values are run again on save, so a script that fails cannot be saved.
The function list.
The function list.
  1. Create a function.

Writing the script

The script reads the configuration through attributes, for example attributes.Voltage, or attributes['CPU Speed'] for a name with spaces. Pick an Authoring model in the Attribute Picker and click an attribute to insert its reference. The script assigns what it produces to result:

var weight = attributes.Width * attributes.Depth * 0.002;
var casters = weight > 80 ? 'CAST-HD-4' : 'CAST-STD-4';

result = {
  set: { 'attributes.Weight': weight },
  bom: [{ partNumber: casters, quantity: 1 }],
  prices: [{ value: weight > 80 ? 45 : 0, method: 'delta', name: 'Caster upgrade' }],
  message: 'Weight: ' + weight + ' kg'
};

Scripts are JavaScript with a few rules: declare variables with var (not let or const), use function expressions instead of arrow functions, and join text with + instead of template literals. .map(), .filter(), .reduce(), Object.keys() and JSON.parse() are available. Function Library offers ready-made examples to start from.

What a function can return

In resultEffect
setSets attribute values: { 'attributes.Weight': 42 }.
hide, hiddenSectionsHides attributes, or sections of the layout by name: ['Pumps'].
requireMakes attributes required.
disabledOptions, allowedOptionsDisables options of an attribute, or keeps only the listed ones.
recommendRecommends a value per attribute.
message, messagesShows information to the person configuring.
violation, violationsReports a combination that is not allowed; the configuration cannot be added to a quote while it stands.
pricesPrice changes, each with a value, a method (delta, discount or override, as in rules) and a name shown in the price breakdown.
bomParts for the bill of materials, each with a partNumber and a quantity.
leadTimeDaysDays added to the lead time.
derivedValues for other functions to use, without showing them; other functions can watch them as derived.name.

One function can return several effects at once. Anything else in result is ignored, and the test panel warns you about keys it does not recognize.

Data, outside systems and logging

  • dql(...) runs a query over your data tables and returns the rows, each keyed by table and column, for example rows[0]['pump_frames.c_price']. Use ${attributes.Name} placeholders in the query instead of joining values into the text.
  • apiCall(name, params) calls one of the external APIs your administrator has set up, by its name. See External APIs.
  • log(...) writes a line you can read in the test panel's Logs tab.

Test a function

Enter attribute values under Test Attributes (JSON) and click Run Test. The Debug Panel shows the Result, the Logs from log(), a Trace of every query and outside call with its time, and Metrics on how long the script took. Errors point to their line and column; Jump to line takes you there. Revert discards every change since the last save.

Buttons in a model run a function on demand: give a Button attribute an Action Function. See Data types.

On the function list, History shows a function's saved versions, and Export and Import move functions between workspaces. Switch Enabled off to stop a function from running without deleting it.