Rules Syntax

Rules Syntax
lac32
Formula rules, validation rules, and event rules are simply JavaScript code. You can use the following JavaScript code examples or explore the examples that are available in API Creator.
For more information:
  • About the JavaScript context variables, see Quick Reference.
  • About the JavaScript code examples that you can access and insert into the code editor in API Creator, see Code Examples.
In this article:
 
 
Formula Rules
Formula rules must return a value which is appropriate for its column. For example, a formula rule for a string column must return a string. Numeric types are automatically converted as needed.
If a formula rule returns nothing, then the value of the column is unchanged:
if (row.value > 1000) // Do not change
return;
Example 1
 
return (row.price * row.quantity) * 1.1;
 
Example 2
 
var basePrice = row.price * row.company.discount;
if (basePrice > row.company.discount_floor)
return basePrice * row.company.discount_pct;
else
return basePrice;
Formula rules can access the values of the current row, the old row, and the current values of parent rows. They must not access the values of grandparent rows.
For more information:
  • About how to access row attributes and parent-relationship attributes using Expression help, see Customize your API.
  • About the list of code examples that are available for formula rules, see Code Examples.
Validation Rules
Validation rules must evaluate to a boolean value. If a validation rule does not return a boolean value, an error results. Like formula rules, validation rules can access the values of the current row, the old row, and any parent rows.
 
Example 1
 
return row.balance <= row.credit_limit;
 
Example 2
 
// Small balances are OK
if (balance < 10)
return true;
// We cannot accept very large balances
if (balance > 1000000)
return false;
// We do not accept large increases
if (logicContext.verb == "UPDATE" && row.balance/oldRow.balance > 2)
return false;
// Now see if the balance is acceptable, depending on whether the company has preferred status
var limit = row.company.credit_limit;
var fuzzyFactor = row.company.preferred ? 1.1 : 1.0;
return row.balance <= limit * fuzzyFactor;
For more information:
  • About validation rules, including how to create validation rules, see Validation Rule Type.
  • About the list of code examples that are available for validation rules, see Code Examples.
Event Rules
Event rules (and their close cousins early events and commit events) are JavaScript that get executed whenever a row is inserted, updated, or deleted (when you have enabled the rule for that type of event). Event rules are not required to return a value. If they do, that value is ignored. Like formula rules and validation rules, event rules can access the values of the current row, the old row, and any parent rows.
For more information:
  • About how to access row attributes and parent-relationship attributes using Expression help, see Customize your API.
  • About logic event rules, including how to use event rules, see Logic Event Rule Types.
  • About the list of code examples that are available for event rules, see Code Examples.
JavaScript Examples
For more information about JavaScript syntax, see JavaScript.