A server rule: an expense limit
A claim over 1,000 needs an approval note. The form can show that as you type, but only a plugin step makes it true, because it runs on the server whatever saved the claim. We imported a spreadsheet of claims past the form and watched two rows refused with the step's message, then looked at how one script holds the limit for both sides.

The last article ended on a gap. A form rule makes the form helpful, but an import, the API or another app never opens the form, so the rule never runs for them.
This one closes that gap for one rule: a claim over 1,000 needs an approval note, and a claim over 5,000 needs a second approver as well. The form shows it as you type. The server makes it true.
We used the Expense Limit sample; import it from
samples/solutions/business-rules-expense-limit-solution-script to follow along. It adds a
Claims — Shared Script app.
On the form
We started a new claim and typed an amount of 1200.50. Approval note appeared, required:

At 7500, Second approver appeared too:

That's two form rules, one running when the form opens and one when the amount changes, just like the expense approval. Useful, and skippable.
Past the form
To save claims without the form, we used the Claims list's Import data with a small CSV:
| Description | Amount | Approval note |
|---|---|---|
| Taxi to the airport | 64.20 | |
| Conference booth | 1500 | |
| New laptops for the team | 7200 | Budgeted in Q3 |
| Printer paper | 38.90 |
The booth is over 1,000 with no note. The laptops have a note but are over 5,000 with no second approver. Neither would get past the form. The import doesn't use the form.

Two rows imported, two refused, each with a sentence saying why. The row numbers are the file's lines, with the headings as line 1. Download the rows that failed gives you just those rows to fix and import again.
The plugin steps
What refused them is two plugin steps. In the maker portal we opened the solution and chose Plugin steps:

A step names a table, a write and a stage. These two run before the write, so a refusal means nothing is stored. There's one for Create and one for Update, because a step watches one kind of write, and a claim edited later must obey the limit too. On an update, the step sees the stored record with the changes on top, so it knows the whole claim, not just the fields that changed.
We selected the first and chose Edit code:

import missingApproval from 'par_claim_limits';
var problem = missingApproval();
if (!isEmpty(problem)) {
throwValidationError(problem);
}
throwValidationError refuses the save and sends its sentence back to whoever saved: the import
panel, the form's message bar or the API's response. The step doesn't decide anything itself. It
asks the same script the form uses.
One limit, two scripts
The limits live in one solution script, Claim limits:

func approvalLimit(): number {
return 1000;
}
func needsApproval(amount: number): bool {
return amount > approvalLimit();
}
func needsSecondApprover(amount: number): bool {
return amount > approvalLimit() * 5;
}
func missingApproval(): string {
var amount = claimAmount();
if (needsSecondApprover(amount) && isEmpty(getValue('par_second_approver'))) {
return `A claim over ${approvalLimit() * 5} needs a second approver.`;
}
if (needsApproval(amount) && isEmpty(getValue('par_approval_note'))) {
return `A claim over ${approvalLimit()} needs an approval note.`;
}
return '';
}
export claimAmount, missingApproval, needsApproval, needsSecondApprover;
(claimAmount reads the amount and treats an empty field as 0.)
The form doesn't import this script directly. It imports a second one, Claim form, which imports Claim limits and does the showing:
import 'par_claim_limits';
func applyLimits() {
var amount = claimAmount();
var over = needsApproval(amount);
setVisible('par_approval_note', over);
setRequired('par_approval_note', over);
var large = needsSecondApprover(amount);
setVisible('par_second_approver', large);
setRequired('par_second_approver', large);
}
export applyLimits;
Why two? Because the steps run on the server, and the server has no form. setVisible and
setRequired don't exist there. A script a step imports may call only what a step can call, so
the split is along that line:
- Claim limits uses
getValueandisEmpty, which both sides have. The form script and the steps import it. - Claim form uses
setVisibleandsetRequired. Only the form rules import it.
The editor holds you to this. When the sample kept everything in one script, the step's editor underlined its import with Errors found in module 'par_claim_limits': the step was importing a script that called form-only functions. We split it while writing this article.
A step, as the solution stores it
A plugin step is a document like everything else in a solution. The create step, without its script:
{
"logicalName": "par_claim_limits_enforce_on_create",
"displayName": "Refuse a new claim over the limit without its approvals",
"publisherPrefix": "par",
"entity": "par_claim",
"message": "Create",
"stage": "PreOperation",
"executionMode": "Synchronous",
"rank": 1,
"implementation": { "type": "BsLangScript", "script": "…" },
"isEnabled": true
}
Read it as a sentence: on the Claim table, when a record is created, before it is written, inside the save,
first in line, run this script. The update step is the same document with "message": "Update". rank orders steps
that share a table, a write and a stage: rank 1 runs before rank 2. executionMode says whether the save waits for the
step, which a step that may refuse must; Plugin steps: before and after a save
goes through the choices.
Why the update step matters as much
It is tempting to register only a create step: the check happens when a claim is entered, and that feels like enough. It isn't. A claim saved at 800 with no note is fine, and the next day somebody edits the amount to 1,800. Without an update step, that claim now breaks the rule and nothing objected.
Because the update step sees the stored record with the changes on top, it checks the whole claim as it will be after the save, not only the fields that changed. So editing the amount, removing the note, or both at once, is caught the same way.
When a rule should apply only to a change, not to the record as a whole, a step can compare before and after with
getPreValue:
// Once a claim is approved, its amount may not change.
if (getPreValue('par_status') == 'approved' && getValue('par_amount') != getPreValue('par_amount')) {
throwValidationError("An approved claim's amount cannot change. Raise a new claim for the difference.");
}
The refusal from each side
The same sentence reaches whoever saved, in whatever shape suits them:
| Saved through | What they see |
|---|---|
| The form | The message across the top of the form; nothing is saved, and what they typed stays on screen |
| A spreadsheet import | The row refused with the message beside its line number, and the other rows imported |
| The record API | The update refused, with the message in the response's detail |
| Another script, such as a Custom API | The script stops at its write, and its caller gets the message |
That last row is easy to forget. A Custom API that creates a claim runs this step too, because it saves through the same path. So an integration can't create a claim over the limit by going through an API instead of the table: the rule is on the table, and every road leads there.
Testing a server rule
A server rule is only as good as the roads you tried. For a rule like this one, a short checklist covers them:
- The form: an amount under the limit saves; over it without a note is refused; over it with a note saves.
- An edit: save a small claim, then raise its amount over the limit without a note. Refused.
- An import: a CSV with one good row and one bad row. One imported, one refused with the message.
- The API, if anything integrates: the same bad claim, posted. Refused with the message.
- The boundary: exactly 1,000. The rule says over 1,000, so exactly 1,000 needs no note. Make sure that is what the business meant.
The last one finds more mistakes than the other four together. Over and at least differ by exactly the value people type most often.
Questions that come up
Does the form rule still matter, if the server enforces it? Yes. The form rule tells people before they press Save, which is friendlier than a refusal afterwards. The server rule makes it true for everybody. The two share one script, so they never disagree.
What if two steps refuse the same save? The first refusal stops the save, so its message is the one shown. Order
steps by rank so the most useful message comes first.
Can a step fix the record instead of refusing it? Yes. Before the write, setValue changes what will be stored, for
example to fill in a missing currency. Refuse when only a person can supply what is missing, such as an approval note;
fill in when the platform can work it out.
Change it once
Open Claim limits and change return 1000; to return 250;, then save. The form now asks
for a note over 250, the steps refuse over 250, and their message says "over 250", because the
message is built from the same function. One line, four places.
Try it
Import the sample and the four-row CSV above. Then fix the two failed rows, by adding a note and a
second approver, and import just those. Then try the API: post a claim for 2000 with no note to
/data/par_claim and read the refusal.
Next: adding a server validation rule of your own, from the New button to a refused save.