Parsware
All articles

Business rules

Business logic in Parsware: which one to use

Parsware has several places to put a rule. Form rules make a form respond as you type. Plugin steps decide what may be saved, however it's saved. Custom APIs answer other systems, scheduled jobs run on a clock, and processes carry work between people. This guide says which one to reach for, with a real example of each.

The Leave Approval process in the process designer, with Employee, Manager and HR lanes, next to the title "Business logic in Parsware: which one to use"

"When an expense is over 1,000, it needs an approval note." Where does that rule go?

In Parsware there are a few places, and they aren't interchangeable. They're all written in BS Lang, but each one runs at a different moment, in a different place, and can be skipped or not. This guide is about choosing.

The short answer

You want to… Use Runs
Show, hide, require or fill in a field as someone types Form rule In the browser, on the form
Do something when someone presses a button Command button In the browser
Refuse a save, or change a record as it's saved, whatever saved it Plugin step On the server, inside the save
Let another system ask or tell you something Custom API On the server, when it's called
Do something every night, or every hour Function On the server, on a schedule or when started
Hand work from one person to the next, and wait for them Process On the server, for days if it must

The one question that settles most cases: can it be skipped? A form rule runs only on a form. Someone who imports a spreadsheet, calls the API or uses another screen never sees it. If the rule must always hold, it belongs on the server.

Form rules: make the form respond

A form rule runs in the browser, on one form. It can show or hide a field, enable or disable it, make it required, fill in a value, or stop a save before it's sent. It's instant, because nothing goes to the server.

You find them on the form, under Events:

The Events list of the Expense form: Apply the decision rules when the form opens, Apply the decision rules when the status changes, and Default the currency on a new claim, with the text: A business rule runs in the browser and changes how the form behaves. It never decides what may be saved: that is a server plugin step, which runs whatever the browser did.

Each rule answers one moment: when the form opens, when a field changes, or when it's saved.

The business rule designer for Apply the decision rules when the form opens: Runs when OnLoad, and a two-line script that imports par_expense_decisions and calls applyDecisionFields and clearReasonWhenNotRejected

Use a form rule to make a form pleasant. Never use it as the only thing standing between bad data and your table.

(Sample: business-rules-expense-approval.)

Command buttons: do something on request

A command button does something when someone presses it. Most need no code: they open a view, start a record or open an address. When the button depends on what the user ticked or typed, it runs a short script. Like a form rule it runs in the browser, so anything it changes still goes through the normal save, and the server's rules still apply.

(Sample: mda-support-ticket-commands, in Buttons that do something.)

Plugin steps: the rule that always holds

A plugin step runs on the server, inside the save. It runs whether the record came from a form, the API, an import or another script. That makes it the only place a rule is really enforced.

The Plugin steps of the Invoices — Credit Check solution: Refuse an invoice over the credit limit, on Invoice, Create, Before the write, Inside the write, order 1, Running

A step names a table, a write (create, update or delete) and a stage:

  • Before the write, it can change the record or refuse it. A refusal means nothing is stored.
  • After the write, it can do the follow-up: create a related record, send an email.
  • In the background, it runs after the save has finished, so a slow call never holds anyone up. It can't refuse anything, because the record is already saved.

The usual pattern is both: a form rule so the form tells you as you type, and a plugin step so the rule holds anyway. Write the logic once, as a solution script, and import it into both.

(Samples: business-rules-server-claim-approval, business-rules-order-stock, and business-rules-query-documents, shown above.)

Custom APIs: answer another system

A Custom API is an address you publish, like POST /api/par/v1/orders/needs-approval, with a script that answers it. Use one when something outside Parsware needs to ask a question or hand something over, and you'd rather it called one contract than wrote to your tables directly.

It runs as the caller, so it can't do anything they couldn't. And saving a record from an API still runs that table's plugin steps.

(Samples: custom-api-object-storage-export, in Files from a script.)

Functions: run on a clock, or for a long time

A function is a script that runs by itself on the server, on a schedule such as every night at 01:00, or when somebody presses Run now, or when a Custom API or a plugin step starts it. Close stale tickets every night, write the month's orders to storage, send a weekly summary. Nobody waits for it, it can take longer than a save is allowed to, and each run is recorded with its log and its result, so you can see what it did.

A function at one in the morning has nobody signed in, so it runs for the tenant rather than for a person, and the files it writes are recorded as written by the function.

(Sample: functions-nightly-export-to-storage.)

Processes: work that moves between people

A process is for work that waits. An employee asks for leave, a manager decides, HR updates the balance. It can take days, it has steps assigned to people, and each step knows who's next.

The Leave Approval process in the process designer: lanes for Employee, Manager and HR, with Start, Submit Request and Review Request, and an outline listing Approved?, Tell Employee: Approved, Tell Employee: Rejected and Deduct Balance

If your rule is really "then someone else has to do something", it's a process, not a plugin step. A plugin step is over in a moment; a process can wait for a person.

(Sample: bpms-leave-approval-process.)

One rule, written three ways

Take the rule from the top of this article, an expense over 1,000 needs an approval note, and see where each part of it goes.

The rule itself, once, in a solution script:

func needsApproval(amount: number): bool {
  return amount > 1000;
}
export needsApproval;

The form's half, a form rule on load and on change of the amount, so people see it as they type:

import 'par_expense_rules';

var over = needsApproval(getValue('par_amount'));
setVisible('par_approval_note', over);
setRequired('par_approval_note', over);

The server's half, a plugin step before the write on create and update, so it holds however the claim is saved:

import 'par_expense_rules';

if (needsApproval(getValue('par_amount')) && isEmpty(getValue('par_approval_note'))) {
  throwValidationError('An expense over 1,000 needs an approval note.');
}

Three small scripts, one number. The form rule makes the form pleasant; the plugin step makes the rule true; the solution script makes sure they agree. A real script would also handle an empty amount, as the Expense Limit sample does with its claimAmount helper. When the limit itself should differ between environments, read it from an environment variable; when business people change it, keep it in a table, as the Capital planning sample does with its approval bands.

Two more you'll meet

  • Data policies decide whether somebody may read or write a record at all, with a sentence when they may not: only the claim's owner may change it while it is a draft. They sit beside security roles, for rules a role can't express. Data policies covers them.
  • Notifications send an email when a record in a table is created, changed or deleted, to the addresses a rule names, with no script at all. Reach for a script only when the message depends on the record's values.

A quick way to decide

Ask these in order, and stop at the first yes:

  1. Does a person have to do something, possibly days later? A process.
  2. Must it hold however the record is saved? A plugin step, plus a form rule if people should see it as they type.
  3. Does somebody press something for it? A command button.
  4. Does another system need to ask or tell? A Custom API.
  5. Does it run on a clock, or take a long time? A function.
  6. Is it only about how the form behaves? A form rule.

Three mistakes to avoid

  • A form rule as the only check. It's the first thing an import or an integration skips.
  • A plugin step that waits for a person. A step runs inside a save and has seconds, not days. Start a process instead.
  • The same rule written in two places. The day it changes, one copy gets missed. Put it in a solution script and import it.

Try it

Import business-rules-expense-approval and business-rules-server-claim-approval side by side. Enter the same bad value in each: the first form tells you as you type, the second refuses when you save. Then try the second one through the API.

Next: a client rule in detail: an expense approval that shows, hides and requires fields as you type.