Calculate a value with a smart field
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.
Add a smart field
Section titled “Add a smart field”- On the Fields page, click Add field.
- Type the Label, for example
Line total. - Open Type and choose Smart field from the Smart group. The editor shows a Script box.
- Write the script. Use Ctrl + Space for suggestions.
- Click Save field.
Field added. appears. Write a script before you save: every smart field needs one.
What does a smart field script look like?
Section titled “What does a smart field script look like?”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()orparseFloat(). 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) / 100A 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) || 0Number tokens are put in as typed. Test the script on a record that has a value in every field it reads.
When does Beezr calculate it?
Section titled “When does Beezr calculate it?”The Virtual field switch under Storage decides.
| Virtual field | When Beezr calculates it | Stored? |
|---|---|---|
| Off | When the record is saved, new or updated | Yes, as text in the module |
| On | When records are loaded | No |
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.
Should I use a smart field or an insight?
Section titled “Should I use a smart field or an insight?”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
What's next
Last checked