Creating APIs‎ > ‎Logic‎ > ‎

Rule Types

The following table lists the rule/logic types:

Rule TypeDescriptionExample

allocateFromTo System Method
Allocates a provider amount to recipients, creating allocation objects (a provider/recipient junction) for each such allocation.
SysLogic.allocateFromTo(
 logicContext,
 SysLogic.findWhere(row.paymentCustomer.ordersList,
    "row.amount_un_paid > 0", "placed_date a"),
 "payment_order_allocations",
 "amount_un_disbursed");
Formula Rule Type
Derive an attribute value using other attributes in that table, or parent table (changes are propagated). For conditional assignment, use JavaScript if/else, or the ternary operator ((boolean)?[true value]:[ else value]). Derive Lineitem.amount as:
return row.part_price * row.quantity_ordered

Derive product_billofmaterials.value as:
return row.kit_number_required * row.product.price

Derive order.DueDate as:
if (row.due_date === null) {
    return new Date(moment(row.placed_date).add('months', 1)
else
    return row.due_date; // no change
Parent Copy Rule Type Derive attribute value from parent attribute value (unlike formula references, parent changes are not propagated)
Derive Lineitem.Price as:
 ParentCopy(product.price)
Sum Rule Type
Derive attribute value by summing designated child attribute, with optional Child Qualification Condition.
Note: Qualification is in in SQL Where syntax.
 Derive Customer.balance as:
Sum(ordersList.amount_un_paid where is_ready = true)
Sum(children where anAttribute != null)

Count Rule Type
Derive attribute value by counting designated child rows using a defined relationship, with optional Child Qualification Condition.
Note: Qualification is in in SQL Where syntax.
 Derive Customer.big_order_count as:
Count(ordersList where amount_total > 100)
Minimum or Maximum Rule Type
Derive value as the min/max of designated child rows, with optional Child Qualification Condition.  Derive Customer.maxOrder as:
max(ordersList where is_ready = true)
Validation Rule Type
Multi-attribute expression of class/parent attributes that must be met for a transaction to succeed - return true (else exception message is thrown); can execute as transaction rows are processed, or at commit time after all rows are processed.
Validate Customer.CheckCredit as:
row.balance < row.credit_limit
Validation Rule Type
Multi-attribute expression of class/parent attributes that must be met for a transaction to succeed (else exception message is thrown); executes at commit time so your validation code sees the results of all rule executions for all rows (for example, sums and counts). Validate Purchaseorder.NotEmpty on commit as:
return row.item_count > 0
Managed Parent Rule Type
Automatically insert a parent object if it does not already exist. Create parent for GroupBy, for example, empsales to track total sales for employee each month.
InsertIntoFrom system method
Copy one or more source rows to a target class. Useful for auditing and deep copy. Auditing example:
if (row.baseSalary != oldRow.baseSalary)
  SysLogic.insertChildFrom(
    "employee_audits", logicContext)
findWhere system method
Returns a filtered, sorted collection (used in events)
SysLogic.findWhere(
  row.paymentCustomer.ordersList,
  "row.amount_un_paid > 0",
  "placed_date a")

Notes

  • Logic is unordered. The business logic engine invokes your rules automatically during transaction processing, ordered by their dependencies. API Server invokes the logic and calls the rules automatically when referenced data changes.

For more information about rules, see Learning Rules.

  • Old values. Formulas, validations, and actions can reference not only row.attributevalues, but use oldRow.attribute values. You can specify state transition logic (for example, logic based on data changes) using formulas, validations, and actions.
  • Null values. You can check for null values.
For more information:
    • About null values and marking a formula as Just In Time (JIT), see Formula Rule Type.
    • About JIT, see the following Just In Time note.
  • Just In Time. You can specify derivation rules (formula, sum, count, and min/max) as JIT. If the value in the database is null, the rule fires on first retrieve and is stored in the database. This is useful, for instance, to recompute values without incurring the cost up front.
Note: Specifying derivation rules as JIT can affect performance whenever new values are computed on the fly.
For more information about JIT, including how to specify to have Live API Creator recompute your rules when/as it detects null vales, see Synchronize Data with Logic.
  • The logicContext objectProvides access to runtime services and state, such as logicNestLevel, setUseCaseName, and services to create, read, update, and delete rows.
For more information about the logicContext object, see The LogicContext Object.
  • Logic design patterns. Use these patterns to meet most requirements, which leverage forward chaining.
For more information:
  • Extensibility. You can meet requirements not solved by reactive logic by defining rules that can invoke JavaScript. Judicious design can result in reusable logic, as illustrated by the pre-supplied BusinessLogic services for allocation and copy. You can add JavaScript libraries.

For more information:

  • Performance. Adjustments are one-row "delta" updates, which require that stored values match the rule definitions. Ensure that your existing data is consistent with the rules, such as with SQL commands like this.
For more information:
  • About performance, see Performance.
  • About how to ensure data consistency with rules, see SQL commands, Data Sources.
  • Just In Time. When you add new derivation rules (formula, sum, count, and min/max), you can specify that Live API Creator recompute the data values. If the value in the database is null, Live API Creator executes the rule on the first read retrieve, stores the value for that column in the database, and returns the value. This is useful, for instance, to recompute values without incurring the cost up front.

Note: Specifying derivation rules as Just in Time can affect performance whenever new values are computed on the fly.

For more information about Just In Time, see Synchronizing Data with Logic.