Skip to content

Smart Script: the language of Beezr

Concept8 minPractitioner

Smart Script is how you tell Beezr to work something out: a default value, a check before save, a filter for a list of records, a number on a dashboard. It is JavaScript with two additions, tokens that bring in your data, and a helper library called FunctionManagerFunctionManagerThe Smart Script helper library exposed by the interpreter.Open glossary ↗ that reads and writes records. If you can read a few lines of JavaScript, you can read any Smart Script.

The engine behind Smart Script is Beezl.

Anywhere a box says Smart Script, or hints at a script. These are the places we checked in the code.

PlaceWhat the script gives backWhere you write it
Default valueThe starting value of a new record’s fieldFields, field editor, Default value
Smart fieldThe field’s value, worked out when the record is shownFields, Script
Rule Booktrue to pass, anything else to fail, before saveForms, Form rules, Rule Book
Disabled If and Visible Iftrue or false for a form buttonForms, button component
Form parameters and button parametersThe value that prefills another formForms, parameters
Custom HTML componentThe HTML the form showsForms, Custom HTML
Load data stepA WHERE filter for the records to loadWhich records?, Smart Script tab
Record manager, Approval task, Send email, Loop, JSON parserA value for a field, a person, a recipient, a count or a payloadThe step’s value boxes
Custom function stepNothing. The script runs for what it doesSmart Script box
Dashboard MetricThe number or text the block showsValue
Dashboard Custom HTMLOne value per ${variable}Variables tab
Data quality rule of type SCRIPTtrue or falseData tools
The Load data step with the Smart Script tab open: a WHERE clause is written in a code box under Which records?, with a note that explains table names and quotes.
    The Smart Script tab holds a filter written as a WHERE clause.
    Each column carries its module’s table name, for example cm_customers.tier.
    The notes under the box list what the step does when nothing matches.

The Decision step is different: it builds its conditions from a field, an operator and a value.

Four things happen, in this order.

  1. Tokens become values
  2. Script runs as JavaScript
  3. Last expression is the result
  4. Result becomes text
  • Tokens become values. Before anything runs, Beezr replaces every $table$.$field$ with the data it stands for. Text arrives in quotes, numbers arrive as they are. Tokens and placeholders lists every kind.
  • The script runs. It runs as JavaScript, with FunctionManager ready to use.
  • The last expression is the result. There is no return. The script total; gives back whatever total holds.
  • The result becomes text. true becomes "true", 42 becomes "42". The place that asked for the value reads the text.

Check a new script with Run test where the editor offers it. If the result is your own script text, a name in it is misspelled.

Four rules cover most broken scripts.

  • getCurrentValue() returns a string. Even for numbers and dates. Use parseInt(value, 10) or parseFloat(value) before you add, and parse dates before you compare them.
  • loadRecordsByClause returns null when nothing matches, not an empty list. Check it before you loop.
  • Loop with .forEach. The lists are Java lists, and .forEach is how you go through them.
  • Write WHERE clauses with the full table.field name, such as cm_purchase_orders.status. Text values need quotes. Filter lookup fields by their id, and use resolveLookupIdValue when you need the text people see.

Dashboard scripts have one more: they end with an expression, with no return. Script a dashboard metric shows it in a finished script.

For evaluateCount, evaluateSumFunction and evaluateAverageFunction, write the clause exactly as it must run, starting with WHERE.

Write three small scripts in Write your first Smart Scripts. Keep Tokens and placeholders open next to it. Every function FunctionManager offers is on Smart Script functions.

Check yourself

Run test on a metric returns the script text itself. What should you check?
Show the answer

The module and field names in the script. A result that equals the script means the engine could not run it, most often because of a misspelled name.

A script reads an amount of 1200 with record.getFieldValueMap().get("amount").getCurrentValue() and adds 50. What does it give?
Show the answer

120050. getCurrentValue() returns a string for every field type, so the plus joins text. Wrap the value in parseFloat or parseInt before you add.

Which of these steps builds its conditions from a field, an operator and a value instead of a script?
Show the answer

Decision. Decision builds conditions from a field, an operator and a value. Load data, Record manager and Custom function take scripts.

What's next

Last checked