Keeping Totals Correct
Defining a calculation does not make it happen. Each datastore that should calculate needs a table rule, and the timing matters.
Where to find it
Architect Panel → ERP - Setup:
- Calculated Fields — the definitions themselves
Architect Panel → Automation:
- Tasks — where the recalculation sweep is enabled
- Task Log — what the sweep corrected
The two rules
On the datastore, add a table rule with the action Run PHP Function.
- Same-row formulas — function
AMformula_rule, running before the save, because the values it produces are part of the record being written. - Rollups — function
AMformula_rollupRule, running after the save, because it totals rows that must already be stored.
Both names are typed by hand and are not validated when the rule is saved, so a typo means nothing calculates. Check spelling first when a new definition appears to do nothing.
Where to attach the rollup rule
The step most often got wrong. A rollup is defined on the parent, but the total goes stale when a child changes — so the child datastore also needs a rule, so that adding, editing or deleting a line updates the invoice it belongs to.
A calculation that works on creation but not on edit is almost always this.
Evaluation order
Several definitions on one datastore run in Order sequence, lowest first. A line with net, then tax from net, then gross adding the two, only works if each has a lower Order than the one that consumes it.
Number them 10, 20, 30 so you can insert one later without renumbering everything. Order applies within a single datastore; the parent-child relationship is handled by rule timing, not by Order.
The recalculation sweep
Table rules cannot catch changes arriving by other routes — imports, data extraction, direct database edits, a restored backup. Enable Calculated Field Recalculation under Automation → Tasks.
It reads the audit trail for changed child rows, maps them back to their parents, and recalculates those. After a large import, run it by hand rather than waiting for the schedule.
When a number looks wrong
Calculations fail quietly by design: a bad definition leaves its column untouched and logs the reason rather than writing a wrong number or blocking the save. So a broken calculation looks like nothing happening.
- Nothing calculates — the table rule, the function name, or the timing. Then check Enabled.
- Works on create, not on edit — the rule is on the header but not the lines.
- Keeps its old value — could not be evaluated. Check the error log for a renamed column, a misspelled function or unbalanced brackets.
- Empty result — a division by zero, or an input empty rather than zero.
- Close but not exact — rounding. Total the rounded column, not the unrounded one.
- Wrong after an import — run the recalculation task.
Worked example
An invoice total is right when raised and wrong after a line is edited. The header has the rollup rule; the lines have the formula rule but nothing telling the header to recalculate. Adding the rollup rule to the lines datastore fixes it, and running the sweep once corrects the invoices already affected.
Recommendations
- Attach the rollup rule to the child as well as the parent.
- Test create, edit and delete — all three, every time.
- Enable the sweep, but treat frequent corrections as a missing rule.
- Check the error log first when a column keeps a stale value.