# Workflow rules

> Write a rule on invoices, bills, expenses and other records: a trigger (created, edited, status change or a date), a condition and an action; test it on real records before you enable it, read every run, undo one, and enable the pack's templates for payment reminders, the month-end revaluation, accruals and the schedule roll-forward. A rule never approves, posts or sends by itself.

Source: /help/en/settings/workflow-rules

**Settings → Automation** (`/settings/automation`) lists the organisation's rules in four tabs: **Workflow**, **Validation**, **Templates** and **Runs**. A workflow rule watches one record type and does one thing when its trigger and its condition hold. This page covers workflow rules and the templates; [Validation rules](/help/en/settings/validation-rules) covers the rules that refuse a save or a submit with your own message.

The **Workflow** tab shows each rule's **Name**, **Kind**, **Record type**, **Trigger**, **Status** (*Draft*, *Active*, *Paused*), **Last run** and **Runs today**. The row menu holds **Open**, **Enable** or **Pause**, **Test**, **Duplicate** and **Delete**. Owners and admins write and change rules; every member reads them.

A rule page has six cards:

- **Trigger**: when the rule looks at a record.
- **When created**, **When edited** (an edit also sees a status change) or **When the status changes**, with **From status** and **To status** when you want one change only.
- **On a date**, in one of three ways: **A date of the record** with a number of days after it (negative: before it), for example three days before the due date; **Days of the month**, with day numbers from 1 to 28 and **The last day of the month**, and optionally a number of **Days before**; or, for invoices, **The reminder levels** of **Settings → Reminders**.
- **Condition**: the fields of the record the rule reads (**All of these**, **Any of these**, **Not**, and groups inside groups). With no condition every record matches. Each condition needs a field, an operator and a value (except **is empty**, **is set** and **changed**): **Test** and **Save** stop at a row that lacks one and mark it, so fill it in or remove it.
- **Action**: what the rule does (see [the list below](#actions-a-rule-may-take-and-what-a-person-still-approves)).
- **Test**: the rule run against records without changing anything.
- **Runs**: every time the rule ran, with what it did, the change batch behind it and the approval it waits for.
- **History**: who saved, enabled, paused and changed the rule.

## Walkthrough: hold a bill without a PO number and alert the buyer

The rule: when a bill above 5,000 is created without an order reference, set its sub-status to "Needs PO" and tell the admins.

1. Add the sub-status. **Settings → Documents → Sub-statuses** → **Document kind** **Bills** → label `Needs PO` (and an Arabic label) → **Add sub-status**. Bills take a sub-status the way quotes and order confirmations do; it shows as a chip beside the status on the bill page.
2. **Settings → Automation** → **New workflow rule**. **Name**: `Hold bills without a PO number`; **Record type**: **Bills**.
3. **Trigger**: **When created**.
4. **Condition**: **All of these** → **Add a condition**: **Total** **greater than** `5000`; **Add a condition**: **Order reference** **is empty**.
5. **Action**: **Set a field**; **Field to set**: **Sub-status**; **Value**: **Needs PO**. **Save**. The rule is saved as a *Draft*: a draft never runs.
6. One rule carries one action. For the alert, **Duplicate** the rule, name the copy `Tell the buyer about bills without a PO number`, set its **Action** to **Notify**, **Notify**: **Admins** (or the role or the named members who buy), **Message**: `Bill {{invoice_number}} from {{supplier_name}} has no order reference.` and the Arabic message. A message may read a field of the record between double braces. **Save**.
7. Test before you enable. On the rule page, the **Test** card → **Test on the newest fifty** (or tick last month's bills and **Test the selected**). The table lists each record with **Matched** or **Did not match** and, for a match, **What the rule would do**. Nothing changes in a test; the summary reads, for example, "50 tested, 3 match".
8. **Enable** in the header of both rules. The status reads *Active*.
9. Record a bill of 6,000 without an order reference (**Bills** → new bill, an uploaded bill, the API or the assistant's `create_bill`). Within five minutes the bill page shows the chip **Needs PO**, and the admins get the notification **Rule alert** with the rule's name, the message and a link to the bill.
10. Open the rule → the **Runs** card (or the **Runs** tab of the list for every rule). The run reads *Done*, triggered by **Event**, with the bill, the result and the **Change batch**.
11. **Undo** on the run, then confirm. The change batch is reversed and the bill has no sub-status again; the run's batch reads undone. Only a run with a change batch that is still applied can be undone: an alert or a webhook call has nothing to undo (`nothing_to_undo`).

To let another system know as well, add a third rule with the action **Call a webhook**: the event `rule.ran` reaches the organisation's webhook subscriptions (the **Webhooks** tab of **API access**, see [Webhooks](/help/en/account-and-setup/webhooks-overview)) with the rule, the record and its fields. The delivery is listed under the webhook's **Deliveries**.

## Actions a rule may take and what a person still approves

| Action | What it does |
|---|---|
| **Set a field** | Sets one field of the record through the same function a person uses. Only these fields can be set: an invoice's category and cost centre; a bill's sub-status, due date and order reference; an expense's category, cost centre, project and billable switch; a document's sub-status; a contact's category and payment terms. Any other field is refused with `rule_field_not_writable`. |
| **Notify** | Sends the in-app notification **Rule alert** to **Owners and admins** (the default), a role, **Named members** or **The rule's creator**. |
| **Send a template** | **Rule alert (to members)** emails the alert to the members you choose, or to the record's contact. **Payment reminder letter** on an invoice writes the reminder; it waits for approval first. |
| **Call a webhook** | Emits the event `rule.ran` to the organisation's webhook subscriptions. |
| **Run a function** | Runs one function from a fixed list, with arguments that may read the record's fields between double braces. |

The functions, and how each one applies:

| Function | Mode |
|---|---|
| Post the currency revaluation | **Waits for approval** |
| Run the schedules (prepaid, accrual, deferred revenue, depreciation) | **Waits for approval** |
| Run a recurring invoice now | **Waits for approval** |
| Send the invoice by email | **Waits for approval** |
| Propose accruals | **Runs directly**: it only proposes; nothing posts until a person accepts a proposal |
| Request approval of the invoice | **Runs directly**: it only asks people |

What stays with a person:

- **A rule never approves.** The action "approve" and any function that approves are refused when the rule is saved (`rule_action_forbidden`, `rule_function_not_allowed`).
- **A rule never posts or sends by itself.** A function that would post or send is run as a preview and stored as an approval request marked **Rule** as its source. It appears in the Approvals inbox with what it would post. When the amount reaches a [posting approval tier](/help/en/settings/posting-approvals) for the source Rule, that tier's approvers decide; otherwise the owners, admins and approvers do. The member who made or enabled the rule is not one of them, with one exception: when that member is an owner and no other member may approve (no other owner, admin or approver), the owner approves the run. The exception ends as soon as another approver joins.
- **A rule runs as the member who made it**, with that member's rights and nothing more. If that member is deactivated, the rule's runs are refused; an owner or admin then duplicates the rule (the copy is theirs), enables the copy and deletes the old one.
- **Every run that changes something is one change batch**, listed under **Compliance → Batches**, and **Undo** reverses it.
- **A write into a locked period becomes a proposal** for the period, as it does for a person (see [Proposed changes](/help/en/close/run-a-month-end-close#proposed-changes)).
- **Rules do not feed each other without end.** A rule never reacts to its own change, and a chain deeper than three rules is stopped: the run reads *Refused* with `rule_loop_cut`. Narrow a condition so the rules stop triggering each other.

A run's status is one of *Done*, *Waiting* (for an approval), *Refused*, *Failed*, *Skipped* (dismissed, or nothing to do) and *Dry run* (a test). **Dismiss** on a waiting run asks once and takes a **Reason (optional)**, kept as the run's note and in the audit log; the approval request is withdrawn and nothing is posted or sent.

## Templates: reminders, revaluation, accruals, roll-forward

The **Templates** tab lists the rules the country pack ships. **Enable** copies a template as your own rule, which you may edit after (its time of day, its condition, its days); **Open the rule** opens the copy. Each runs once a day at 06:00 in the organisation's time zone unless you change the time.

| Template | When | What it prepares |
|---|---|---|
| **Payment reminders** | Each morning | For every unpaid invoice that reaches one of the organisation's reminder levels (the days after the due date under **Settings → Reminders**), a reminder that waits for approval. Approving it writes the reminder letter as a draft document, which you then send like any document. A customer with **Payment reminders** switched off on the contact is left out, and a level an invoice already reached is not repeated. See [Payment reminders](/help/en/documents/payment-reminders#automatic-reminders). |
| **Month-end revaluation** | The last day of the month | The foreign currency revaluation as of that day, as an approval request. It posts only when an approver other than the member who enabled the rule approves it (or that member, when they are an owner and the only one who may approve). A month with no foreign balance records "nothing to prepare". |
| **Month-end accruals** | The 25th and the last day of the month | The period's accrual proposals from each vendor's past bills and recurring profiles. The proposals wait on the close page; nothing posts until a person accepts one. |
| **Schedule roll-forward** | Three days before the month ends | The next period's entries of every prepaid, accrual, deferred revenue and depreciation schedule, as an approval request that shows what would post. |

What the templates prepared is listed on the close page before the period is opened: see [Prepared before you open the period](/help/en/close/run-a-month-end-close#prepared-before-you-open-the-period).

## When a rule runs

- A rule on **created**, **edited** or **status change** runs within five minutes of the change: the scheduler looks for pending work every five minutes.
- A rule **on a date** is looked at once a day at its time (**At**, 06:00 by default) in the organisation's time zone. It runs once per record and day, and for reminders once per invoice and level.
- A *Draft* or *Paused* rule never runs.

## Plans

Workflow rules and the templates are in **Professional** and above. On another plan the page says "Workflow rules are in Professional" and the **Workflow** and **Templates** tabs are read-only. [Validation rules](/help/en/settings/validation-rules) are available on every plan.

## API and assistants

- `GET /automation/rules`, `POST /automation/rules`, `GET /automation/fields?entity=`, `GET /automation/functions`: the rules, the fields a rule may read and set, and the function list.
- `POST /automation/rules/{id}/test` with optional `entityIds`: the dry run.
- `GET /automation/templates`; `POST /automation/rules/{key}/enable-template` enables the template with that key.
- `GET /automation/runs?rule=&status=&entity=`; `POST /automation/runs/{id}/undo` and `POST /automation/runs/{id}/dismiss`, each with an optional `reason`. Dismissing a run that does not wait is refused with `invalid_status`.
- `POST /bills/{id}/substatus` with `key` (or `null` to clear it).
- MCP: `list_automation_rules`, `save_automation_rule`, `preview_automation_rule`, `test_automation_rule`, `list_rule_fields`, `list_automation_templates`, `enable_automation_template`, `list_automation_runs`, `undo_automation_run`, `set_bill_substatus`. An assistant saves a rule as a draft only, and it may not enable a template: a person enables both (an API key may enable a template).

Reference: docs.alyabooks.ai.

## Related

- [Validation rules](/help/en/settings/validation-rules)
- [Posting approvals](/help/en/settings/posting-approvals)
- [Run a month-end close](/help/en/close/run-a-month-end-close)
- [Payment reminders](/help/en/documents/payment-reminders)
- [Webhooks](/help/en/account-and-setup/webhooks-overview)
