Parsware
All articles

Business rules

A claim approved by the server

Not every server rule refuses. This one approves. A claim of 50 or less is marked approved as it's saved, by a plugin step that sets a value on the record before it's written. We saved a taxi fare and a hotel bill, watched only one come back approved, then watched the sample's own lock stop the approved one being edited up.

A saved claim, Taxi from the station, for 18.4 GBP with Status approved, filled in by the server, next to the title "A claim approved by the server"

The plugin steps so far have said no: a claim over the limit, an order with no stock. A step can also change the record as it's saved, which is how the server fills in values nobody should have to type and nobody should be able to skip.

The rule here: a claim of 50 or less is approved automatically. Nobody needs to look at a taxi fare.

We used the Claim Approval sample from adding a server validation rule. Import it from samples/solutions/business-rules-server-claim-approval to follow along. It adds a Claims — Server Plugin Steps app whose form has no rules at all, and four steps. One of them, Default the currency, already sets a value: it fills in GBP when the currency is empty.

The step

In Main, the environment's own solution, we added a plugin step: Approve small claims, on Claim, Create, Before the write, with an Order of 3:

The New plugin step panel: Display name Approve small claims, Table Claim (par_claim), Write Create, Stage Before the write, Runs Inside the write, Order 3

Its code:

The step's code: a comment, then if par_amount is 50 or less and par_status is empty, setValue par_status to approved

// Claims of 50 or less are approved as they are saved. Nobody needs to look at a taxi fare.
if (getValue('par_amount') <= 50 && isEmpty(getValue('par_status'))) {
  setValue('par_status', 'approved');
}

setValue changes a field on the record being saved. The server writes the record with the new value, and the person saving sees it when the form comes back.

isEmpty(getValue('par_status')) means the step only fills the status in. If somebody has already set one, the step leaves it alone.

Two claims

In the Shell we started a claim for a taxi, 18.40, and left Currency and Status empty:

A new claim, Taxi from the station, Amount 18.40, with Currency and Status empty

And saved it:

The saved claim, Taxi from the station, 18.4 GBP, Status approved

GBP and approved, both from the server, from two different steps. The form has no rules of its own; it sent what we typed and showed what came back.

Then a hotel bill for 120:

The saved claim, Hotel, two nights in Leeds, 120 GBP, Status empty

The currency is filled in, but the status is empty, because 120 is over the limit. That claim waits for a person.

We also posted a claim for 12 straight to the API, with no status. The record that came back said "par_status": "approved". The rule applies however the claim is saved.

Why the order is 3

The sample has two Create steps already: Default the currency at 1 and Reject a negative amount at 2. Ours runs after both, so a claim for -5 is refused before our step looks at it. Without that, -5 <= 50 would approve it.

In practice the refusal would win either way: a refusal before the write undoes the whole save, including values other steps have set. But a step that relies on another step having run first should say so, and the Order is where it says it.

An approval that sticks

The sample's fourth step, A decided claim cannot change amount, runs on Update. It compares the amount with what was stored before, using getPreValue:

if (getPreValue('par_status') == 'approved' && getValue('par_amount') != getPreValue('par_amount')) {
  throwValidationError('An approved claim cannot have its amount changed.');
}

So we tried to turn our approved taxi fare into a bigger claim, changing the amount to 180:

The taxi claim with Amount changed to 180, Status approved, refused with "An approved claim cannot have its amount changed."

Refused. The server approved the claim, and the server keeps it approved at that amount. Without this step, auto-approval would be a hole: approve something small, then change it.

The Claims list: Hotel, two nights in Leeds, 120 GBP, no status; Taxi from the station, 18.4 GBP, approved

Recording who approved it

A claim approved by the server should say so, so that nobody later wonders which manager let a taxi fare through. Before the write, the same step can fill in a note and the approval's date alongside the status:

if (getValue('par_amount') <= 50 && isEmpty(getValue('par_status'))) {
  setValue('par_status', 'approved');
  setValue('par_approver_note', 'Approved automatically: 50 or less.');
  setValue('par_decided_on', today());
}

Every value set here is part of the same write, so the claim is stored approved, noted and dated, or not at all. (The sample's claim has neither an approver note nor a decided on field; they are here to show the pattern. today() is one of the clock functions a plugin step has, with now().) The audit log, if it is on for the table, records the change too, with the step named as the agent, so the record and the log agree about where the approval came from.

Don't let people approve their own claims

Auto-approval makes the status a field the server sets. The form shouldn't let anybody else set it, or a person could type approved into a 500 claim and save. Two things close that:

  • Make Status read-only on the form, so nobody can type it. That is presentation, and it makes the form honest about who decides. Required, read-only and default controls shows the setting.
  • Refuse a status a person set, on the server, for anything over the limit:
// On Create: a claim over 50 can't arrive already approved.
if (getValue('par_amount') > 50 && getValue('par_status') == 'approved') {
  throwValidationError('Claims over 50 are approved by a manager, not on the form.');
}

Give that step an Order between the negative-amount check and the auto-approval. Orders are whole numbers, so in practice number steps 10, 20, 30 and put a new one at 25. Leaving gaps in the numbering from the start makes room for the next step.

And claims over the limit?

The step leaves the hotel bill's status empty, waiting for a person. That person should be told, and their decision recorded, which is a process: a task for the claimant's manager, with Approve and Reject, that writes the outcome back onto the claim. The server rule and the process divide the work cleanly:

Claim Decided by Recorded as
50 or less The plugin step, as it is saved approved, with the automatic note
Over 50 A manager, through a process task approved or rejected, with the manager's comment

A two-step approval builds a process like that, and Approval by amount sends claims to different people depending on how large they are. A process can also start by itself when a claim is created, so nothing needs to remember to start it: Start a process from a record.

Values worth filling in on the server

Auto-approval is one example of a general pattern: a value the platform can work out shouldn't be typed by a person. Others that suit a step before the write:

Value Worked out from
A default currency, region or category Nothing typed yet, so a sensible default
A total on an order line Quantity times unit price
A due date Today, for an ordered on or received on
A status for routine cases The amount, the type or the customer, as here
A clean-up of what was typed Spaces trimmed with trim(), so ACME and *ACME * aren't two customers

Each one is a setValue behind a condition that checks the field is empty, or that the value really needs changing, so a person's deliberate choice is never overwritten.

Questions that come up

Does the form show the server's values straight away? Yes. The form shows the record as it was stored, so approved and GBP appear as soon as the save comes back.

What if a step sets a value and a later step refuses? The refusal undoes the whole save, values included. Nothing is stored, and the form keeps what the person typed.

Can a step change a field that isn't on the form? Yes. A step works on the record, not on the form, so it can fill in any field of the table, shown or not.

Only before the write

setValue works in one stage: Before the write, the last moment before the record's values are written. Even Before validation, which runs just ahead of it, can't use it.

In any other stage setValue does nothing, silently. We checked: with our step moved to After the write, a claim for 3 was stored with an empty status and no message. If a step that sets values seems to do nothing, check its stage first.

To change a record that's already stored, an after-the-write step can call updateRecord on it instead. That's a second save, with its own plugin steps, so guard it so it doesn't trigger itself again.

Try it

Add Update to the rule: a claim whose amount is lowered to 50 or less gets approved as well. You'll need a second step, because one step watches one kind of write. Then think about what happens to the lock step if the amount is lowered on an already approved claim, and test it.

Next: when background work fails: the list a step lands on when it fails after the save, and how to retry it.