Skip to Content
Droot Developers

Forms on-change: change rules

Change rules: forms that fill lines, warn and react as the user edits, before Save.

A change rule makes a form react while the user edits it. When the user changes one of the rule's trigger fields or table cells, the kernel runs the rule over the unsaved form and the form shows the result at once: values, table rows and messages. Nothing is written until the user saves, and Save is unchanged: it is still the only place that refuses a value.

Rules are metadata in the app's bundle (metadata/change-rules.json, kind change-rule). The platform knows field types, lookups, tables and conditions, never a document type, company or industry, so the same rule types serve order to delivery, order to invoice, quote to order, request to purchase order and any other "fill this form from the record I picked" case.

A rule

{
  "key": "purchasing.receipt-lines",
  "modelKey": "purchasing.receipt",
  "label": "Lines from the order",
  "order": 10,
  "on": { "fields": ["order"], "cells": { "lines": ["quantity"] }, "open": true },
  "action": {
    "kind": "copy-from",
    "source": "order",
    "map": { "vendor": { "from": "vendor" }, "deliverTo": { "from": "deliverTo" } },
    "onlyIfEmpty": ["deliverTo"],
    "lines": {
      "from": "lines", "to": "lines", "key": "orderLine",
      "map": { "item": { "from": "item" }, "unit": { "from": "unit" } },
      "quantity": { "from": "quantity", "to": "quantity" },
      "when": [{ "field": "status", "operator": "neq", "value": "cancelled" }],
      "taken": [{ "model": "purchasing.return", "field": "order", "table": "lines", "key": "orderLine", "quantity": "quantity" }]
    }
  }
}
Property Meaning
on.fields Header fields whose change runs the rule
on.cells Table cells (table → columns) whose change runs the rule
on.open Also run once when a new record's form opens with a trigger field already filled (a flow action, a link)
order Rules run in order, then key; each sees what the earlier ones returned
action One of copy-from, check, script (below)
isActive A tenant may switch a rule off, or move it in the order, by delta; never change what it does

Rules run in one pass: a value a rule sets never triggers another rule, so there are no chains and no loops.

Actions

copy-from

Copies header fields and table lines from the record a lookup (source) names. source may be a multi-lookup: the lines of every record picked are copied, in pick order, header fields come from the first record (a warning sources_differ says when another record disagrees), and each source line's open quantity is worked out per line, so a document naming several orders takes from all of them. A taken model's link may be a multi-lookup too. Maps have the same shape as a flow's create-from effect ({ "from": "<field>" }, { "fromNow": true }, { "value": … }) and are type-checked when the app installs. onlyIfEmpty keeps a value the user already chose.

map fills a header field only while it is blank or still holds what on-change last put there: a value the user entered (or the form loaded with) is never overwritten, and the form says how many it kept. onlyIfEmpty goes further: the rule never replaces that field once it has a value.

lines.key is the column that keeps each row's source line id. The form merges rebuilt tables by it, so re-picking the source updates the rows the user didn't touch, drops the untouched rows of lines no longer on the source, keeps the rows the user edited and never touches rows typed by hand. An edited row whose line is not on the newly picked record stays, flagged "This line is not on the picked record" with Remove, until the user removes it or picks its record again.

With lines.quantity, each line takes what is still open on its source line: the source quantity less what the live records of this model (other than the one being edited, and only those meeting lines.when) and of each taken model already took of it. A line with nothing open is not copied. The form warns when a quantity goes above what is open, and Save refuses it (422 over_open) with the same calculation, so the screen and the stored record cannot disagree. Quantities in messages and errors are said as a person reads them: a scaled integer column (thousandths) that stores 10000 says 10, and the rule is refused at install when a copy goes between columns of different scales (it would turn 10 into 10000). A line whose source line is not on the named record at all is refused as 422 not_on_source ("Line 3 is not on the picked record"). A stored line whose key and quantity are unchanged is not checked again.

check

Evaluates the model's save rules now (rules, or by default the assert rules attached to the trigger fields) and shows their messages as warnings. Save still enforces them.

script

Calls a function script, read-only and as the user:

// ctx.input = { values, before, changed, opening, recordId }
const v = ctx.input.values;
return {
  values: { note: v.party ? `For ${v.party}` : null },
  messages: [{ level: "warning", text: "Check the delivery address", field: "deliverTo" }],
};

Only the model's fields are taken from values. A script that tries to write is refused by the script governor; the form then says the rule couldn't run. A script's error text never reaches the form.

The call

POST /api/views/:formView/onchange
{ "recordId": "…" | null, "values": { … }, "changed": [{ "field": "order" } | { "table": "lines", "row": "<row>", "field": "quantity" }],
  "opening": false, "seq": 17 }
→ { "seq": 17, "generation": 412,
    "patch": { "fields": { "vendor": "…" }, "tables": { "lines": { "key": "orderLine", "rows": [ … ], "removed": ["<key>"] } } },
    "labels": { … }, "messages": [{ "level": "warning", "code": "over_open", "text": "…", "values": { "open": "80" }, "field": "lines", "row": "<key>", "rowKey": "orderLine" }],
    "timings": { "<rule>": 12 } }
  • Scoped to the form view: the user's rights on that form apply. Values for fields the user may not write there (read-only, computed, not on the form) are ignored; the answer holds only fields the user may read on the form.
  • Reads run as the user (company, row and field access). Open quantities are summed with the system reader: they are a business fact, returned only as a number.
  • Nothing is written: no record, number, job, run log or audit entry.
  • A rule that throws, or takes over 1 second, adds an info message and the other rules still run.
  • Limits: 120 calls a minute and 2 at once per user, 1,000 rows per table in an answer.
  • The form manifest lists the triggers (onChange: { fields, cells, open }), so the form calls only when one changes, waits 400 ms after the last change, and applies only the latest answer.

Messages carry a code the form translates (over_open, nothing_open, not_readable, not_on_source, rule_failed, rule_timeout, too_many_rows); a save rule's or a script's message shows its own text.

Production notes

  • Tolerance. lines.quantity.toleranceBp names a source line column holding how much more than is open the line may take, in basis points (an over-receipt tolerance). The form copies what is open, warns and refuses only above open plus the tolerance, so a rule matches an app that allows over-receipt.
  • Read-only columns. A form can't save a read-only field or column (the kernel drops a client's value for it, and the app fills it itself, if at all). The rule is refused at install when its key or quantity column is read-only, because the line would lose its source and Save could not enforce anything: keep the source line in an editable text column. Other read-only targets are allowed and listed as a note in Try It.
  • Scales. A copy between columns stored in different scales is refused (it would turn 10 into 10000); quantities are said as a person reads them.
  • Speed. The open quantity searches the live records that name a source record. The index manager derives a dx_cr_<field> index on every lookup a rule searches by (this model's source, and each taken link), and a saved rule builds it in the background.
  • Concurrency. Save locks each source record (a row written in the save's own transaction) before it reads what is open, so two saves against the same line serialise: the later one retries and sees the first.
  • Duplicates and negatives. Rows naming one source line share what is open on it: they are checked together, so two rows can't each take the whole open quantity. A negative quantity is refused on Save (negative_quantity) and never counts as giving back for other records.
  • Many sources. A multi-lookup copies from at most 50 records at once (the rest are ignored with a message on the form; Save refuses more, too_many_sources); one search serves all of them.
  • Slow rules. A rule that times out can no longer change the answer: each rule works on its own scope and is taken over only if it finishes in time.
  • Overlap. Two active copy-from rules that fill the same table or field on the same change run in order; the later one wins. Setup tells the author when a rule overlaps another.

Fill and formulas

A lookup's fill rules (copying single fields from the picked record) and formCompute formulas (amounts, totals) already run in the browser with no call; use them for those. Change rules are for what needs the server: table lines, open quantities, checks and logic in scripts.