Docs‎ > ‎API Creator‎ > ‎Specify your Business Rules‎ > ‎

Count Rule Type

The Count rule declares an attribute to be maintained as the count of a designated child role, with an optional qualification to filter child objects. For example:

Derive orders.item_count as count(lineitemsList)

Count rules are useful as chid existence checks. For example, you can ensure that orders have at least one line item entry using the item_count in a (commit!) validation.

  1. In API Creator, select Manage, Rules, and click the Create New Rule.
  2. Select the Count rule type, the table to which it applies, and click Create Rule.
  3. Define the parameters of the rule:

    Count name

    The name of the count rule. 

    Tip: Leave this field blank for the system default.

    Table/column

    The name of the parent table and column representing the count result.

    Topics

    The topic associates with this rule.

    Children

    Defines child role (first combo box) and attribute (second combo box) being counted.

    For more information about roles and foreign key-based relationships, see Data Sources.

    Qualification

    Boolean JavaScript expression designating the child rows that contribute to the count; syntax notes:
    • JavaScript means you are using logical operators such as && for "and", || for "or"
    • Equals can be specified either as "=", or "=="

    Optional: 
    Yes

    Active

    Select to activate the rule (The definition must be complete).

    SQLable

    Indicates whether you can optimize the rule by executing it in SQL instead of in JavaScript.

    Just In Time

    You can indicate formulas and aggregates as Just in Time (JIT) rules. You can define JIT rules on persistent attributes and on rules of type formula, sum, count, min and max.

    If the value in the database is null, the rule is executed on the first read and the value is stored and returned. To prevent rules with null return values from executing on every read, the rule should not return a null value.

  4. Click Active and Close to return to the list of rules.
The count rule is created.

Effect

The value of the attribute is updated to reflect the count of the specified attribute in the child objects whenever necessary. This includes child objects being added to and removed from the parent object, as well as modifications to the children objects that change their qualification in the Count. Observe that count processing is triggered by changes to the child, as visible in the log.

A count is not recalculated from scratch, but rather adjusted as necessary. For instance, when you add a new (qualifying) child to the parent, the count attribute is incremented by 1. The performance impact is small.

For more information about the log, see Debug your API Project.

Usage Notes

To maintain high performance, count values are assumed to be correct on disk. For proper operation, if you define new counts on existing data, initialize these using SQL tools.

For more information about synchronizing data with logic using a SQL tool, see Data Sources.