Skip to content

Calculate a value with a smart field

How-to8 minPractitioner

Before you start

Use a smart field when a value should be worked out from other values: a line total, a full name, an amount invoiced so far. You write a Smart ScriptSmart ScriptBeezr's scripting language (engine name Beezl) used in fields, rules, filters, workflow values and dashboards.Open glossary ↗ once, and Beezr calculates the result for every record.

  1. On the Fields page, click Add field.
  2. Type the Label, for example Line total.
  3. Open Type and choose Smart field from the Smart group. The editor shows a Script box.
  4. Write the script. Use Ctrl + Space for suggestions.
  5. Click Save field.

Field added. appears. Write a script before you save: every smart field needs one.

The field editor for a new Smart field called Line total, with the Script box holding a short Smart Script and the hint under it.
    Choose Smart field in the Type grid.
    The Script box holds the Smart Script. Field tokens are highlighted.
    Virtual field under Storage decides when Beezr calculates it.
    Save field adds the field.

Follow three conventions and most scripts work the first time:

  • Write the full token, $table$.$field$, even for the record you are on. Copy the table name from the Modules page.
  • For arithmetic, wrap each number field in parseInt() or parseFloat(). Don’t put quotes around the token.
  • The last expression is the result. There is no return.

A line total, rounded to 2 decimal places:

Math.round(parseFloat($cm_purchase_order_lines_base$.$unit_price$) * parseInt($cm_purchase_order_lines_base$.$quantity$) * 100) / 100

A full name from two Text fields. Text tokens arrive already in quotes, so you add none:

$cm_vendors_base$.$first_name$ + ' ' + $cm_vendors_base$.$last_name$

An amount invoiced so far on a contract, read from another module with FunctionManager. The sum comes back as text, so convert it, and fall back to 0 when nothing matches:

var total = FunctionManager.evaluateSumFunction("payment_amount", "cm_invoices_base", "WHERE contract_pr_id = " + $cm_contracts_base$.$contract_pr_id$);
parseFloat(total) || 0

Number tokens are put in as typed. Test the script on a record that has a value in every field it reads.

The Virtual field switch under Storage decides.

Virtual fieldWhen Beezr calculates itStored?
OffWhen the record is saved, new or updatedYes, as text in the module
OnWhen records are loadedNo

Switch Virtual field on when the value depends on records that change without this record being saved, such as the invoices in the example. Choose the stored kind when you need to report on the value.

If you want a visual, such as a progress bar, an age that turns red or a due-date strip, build an insight field. Beezr writes the script for you. Use a smart field when you need a value, not a picture.

Check yourself

Which script multiplies a Unit price field by a Quantity field and follows the Smart Script conventions?
Show the answer

parseFloat($cm_lines_base$.$unit_price$) * parseInt($cm_lines_base$.$quantity$). Use the full $table$.$field$ token, wrap each number in parseFloat or parseInt, and leave the tokens without quotes.

Sara switches on Virtual field for a smart field. What changes?
Show the answer

Beezr stores nothing and works the value out when records are loaded. A virtual field is computed at display time and has no stored column.

What's next

Last checked