Add a server validation rule
A server validation rule is a plugin step that refuses a save and says why. We added one from scratch, "a claim over 10,000 needs a purchase order", then watched the editor catch a misspelt function and the form show the step's message when we saved a claim that broke it.

The last article read a server rule someone else wrote. This one writes one: a claim over 10,000 needs a purchase order, refused on the server whatever saves the claim.
We started from the Claim Approval sample, which already has four rules of this kind. Import
it from samples/solutions/business-rules-server-claim-approval to follow along. It adds a
Claims — Server Plugin Steps app with a deliberately plain form: no form rules at all.
What's there already
In the maker portal, the sample's Plugin steps:

The sample is a managed solution, installed rather than built here, so you don't add to it. Your rule goes in your own solution. We used Main, the environment's unmanaged solution. A step in one solution can watch a table from another.
The step
In Main, we chose Plugin steps and then New:

| Box | What we chose | Why |
|---|---|---|
| Display name | A claim over 10,000 needs a purchase order | Say what the rule is, so the list reads as a list of rules |
| Table | Claim | The picker searches every table in the environment |
| Write | Create | When the rule applies. A rule that must hold for edits too needs an Update step as well |
| Stage | Before the write | Only a step before the write can refuse. After it, the record is already stored |
| Runs | Before the response | The person saving waits for the answer, which is what a refusal needs |
| Order | 1 | Among steps on the same table, write and stage, the lowest runs first |
We saved it. The panel only describes when the step runs. What it does is its script.
The script
We selected the new step and chose Edit code. Our first attempt had a typo in it:

Function throwValidationEror not found. The editor checks every name against what a plugin step can call, and says so as you type, before anything is saved.
With the spelling fixed:

// Claims are for small, everyday spending. Anything bigger goes through purchasing.
if (getValue('par_amount') > 10000) {
throwValidationError('A claim over 10,000 needs a purchase order. Raise one in Purchasing instead.');
}
Two functions do all the work:
getValuereads a field of the record being saved.throwValidationErrorrefuses the save. Its sentence is the whole of what the person saving is told, so write it for them: what's wrong, and what to do instead.
We saved the script. The step runs from the next save on; there's nothing to publish.
The sample's four steps, side by side
The steps that were already there are worth reading, because between them they show the three things a step before the write can do: fill something in, refuse, and refuse a change.
| Step | Write | Script |
|---|---|---|
| Default the currency | Create | if (isEmpty(getValue('par_currency'))) { setValue('par_currency', 'GBP'); } |
| Reject a negative amount | Create | if (getValue('par_amount') < 0) { throwValidationError('A claim cannot be for a negative amount.'); } |
| A rejected claim needs a reason | Update | if (getValue('par_status') == 'rejected' && isEmpty(getValue('par_rejection_reason'))) { throwValidationError('Say why the claim was rejected.'); } |
| A decided claim cannot change amount | Update | if (getPreValue('par_status') == 'approved' && getValue('par_amount') != getPreValue('par_amount')) { throwValidationError('An approved claim cannot have its amount changed.'); } |
Each is one line, and each says one thing. The first fills in: a step before the write can change what is stored,
and the change is part of the same save. The next two refuse based on the record as it will be. The last
refuses a change, using getPreValue, the value before this save, to tell amount changed after approval from
amount was always this.
The step as a document
The step you made in the panel is stored like this, in Main:
{
"logicalName": "new_claim_over_limit",
"displayName": "A claim over 10,000 needs a purchase order",
"publisherPrefix": "new",
"entity": "par_claim",
"message": "Create",
"stage": "PreOperation",
"executionMode": "Synchronous",
"rank": 1,
"implementation": {
"type": "BsLangScript",
"script": "if (getValue('par_amount') > 10000) {\n throwValidationError('A claim over 10,000 needs a purchase order. Raise one in Purchasing instead.');\n}\n"
},
"isEnabled": true
}
The new_ prefix comes from Main's publisher, the environment's default. In a real project the step would live in
your own solution with your own prefix, so it travels to Test and Production with the rest of your work.
Publishers and solutions explains the difference.
A step in another solution's table
The sample is managed, so its tables can't be changed here, and yet your step watches one of them. That is allowed on purpose: a step belongs to the solution it is made in and names a table by its logical name. Your rule travels with your solution; the sample's table stays the sample's. If the sample is ever uninstalled, your step has nothing left to watch, so install order matters: import the sample first, then your solution.
Before the response, or in the background?
The Runs box decides whether the person saving waits for the step.
| Runs | The save waits | The step can refuse | Use it for |
|---|---|---|---|
| Before the response | Yes | Yes, before the write | Every rule that must stop a bad save |
| In the background | No | No: the record is already stored | Slow follow-up work, after the write |
A validation rule is always before the write and before the response. A step in the background can't refuse, because by the time it runs the save has finished and the person has moved on. When background work fails covers what happens to background steps that go wrong.
Rules a step isn't the best tool for
Some rules look like validation steps but have a better home:
- No two claims with the same reference: a uniqueness rule on the table, which the database enforces even when two saves arrive at once. No duplicates, no double bookings.
- Only the owner may change a draft: a data policy, which decides who may write at all. Data policies.
- A field that must never be empty: still a step, because required on a field is a form setting. Required, read-only and default controls explains why.
- Somebody must approve it first: a process, because a step can't wait for a person. A two-step approval.
Messages, from good to better
The step's sentence is the whole of what the person saving is told, so it deserves a second draft:
| First draft | Better | Why |
|---|---|---|
| Invalid amount. | A claim over 10,000 needs a purchase order. Raise one in Purchasing instead. | Says what is wrong and what to do |
| par_rejection_reason is required. | Say why the claim was rejected. | The person's words, not the column's |
| Error: limit exceeded (10000). | A claim over 10,000 needs a purchase order. | No jargon, and the number in the same form people type it |
When a limit is written in more than one message, build the message from the same function as the check, as the Expense Limit sample does, so the number in the words can't fall out of step with the number in the rule.
A save it refuses
In the Shell, we opened Claims — Server Plugin Steps, started a claim for 12500 and pressed
Save:

The form has no rule of its own, so it sent the claim. The server refused it, and the form showed
the step's sentence. Nothing was stored. We changed the amount to 850 and saved, and the claim
went through, with its currency filled in as GBP by one of the sample's own steps.
The same step refuses the same claim from an import, the API or a script. That's the difference from a form rule: it isn't on the form, so nothing gets around it.
Good messages
- Say what to do. "Raise one in Purchasing instead" is more use than "Invalid amount".
- Use the person's words. They see "claim" and "purchase order", not
par_amount. - One rule, one step. Four small steps with four clear messages are easier to read, reorder
and switch off than one step with four
ifs.
A fix from writing this
The typo above wasn't caught at first. The editor's checker came from the interpreter, and like the
interpreter it looked only at the branch of an if that would run. It couldn't know whether
getValue('par_amount') > 10000 was true, so it skipped the if's body, which is where every
validation step puts its throwValidationError. It now checks both branches of every if,
whatever the condition. We checked every sample's scripts against the change: none had a hidden
mistake, and none gained a false one.
Try it
Add the same rule for Update, so a claim can't be edited up past 10,000 either. It's the same script with a different Write. Then give it an Order of 3, after the sample's two Update steps, and try an approved claim: which message wins?
Next: plugin steps in more depth: before and after a save, and the order steps run in.