Creating APIs‎ > ‎Logic‎ > ‎

Rules Syntax

Formulas, validations, and event rules are simply JavaScript code.

For more information about the JavaScript context variables, see Quick Reference.

Formulas

A formula must return a value which is appropriate for its column. For example, a formula for a string column must return a string. Numeric types are automatically converted as needed.

If a formula 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;

Formulas 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.

Validations

Validation rules must evaluate to a boolean value. If a validation rule does not return a boolean value, an error results. Like formulas, validations 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 always OK
if (balance < 10)
    return true;
// We can't accept huge 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 rule types, including how to create validation rules, see Validation Rule Type.

Events

Events (and their close cousins Early Events and Commit Events) are simply JavaScript that gets executed whenever a row is inserted, updated, or deleted (when you have enabled the rule for that type of event). Events are not required to return a value. If they do, that value is ignored. Like formulas and validations, events 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 rule types, including how to use events, see Logic Event Rule Types.

JavaScript Examples

For more information on JavaScript syntax, see JavaScript.