Shared logic: solution scripts
When a plugin step and a Custom API need the same rule, write it once as a solution script and import it into both. We used the Shared Server Logic sample, where an approval limit decides both whether an order saves and what an API tells a partner, then changed the limit in one line and watched both follow. Here is how import and export work, and which scripts can import.

Sooner or later, two scripts need the same rule. A plugin step refuses an order over a limit, and a Custom API tells a partner system whether an order will need approval. Both have to use the same limit. If you paste the number into each, they agree on the day you write them, and stop agreeing the first time somebody changes one and forgets the other.
A solution script fixes that. It holds the rule once, and the step and the API each import
it. We used the Shared Server Logic sample; import it from
samples/solutions/business-rules-shared-server-logic to follow along.
The script
Open the solution and choose Scripts. There's one, Order rules:

Its code is short:
func approvalLimit(): number {
return 1000;
}
func needsApproval(amount: number): bool {
return amount > approvalLimit();
}
export needsApproval;
Two functions, and one line that matters more than it looks: export needsApproval;. A script
that imports this one gets only what it exports. So other scripts can ask does this amount
need approval?, but they can't read the number itself. If the limit changes, only this line
changes.
The module path, par_order_rules, is the name other scripts use to import it.
Two scripts that import it
The plugin step, Require an approval note over the limit, runs when an order is created:

The Custom API, Will this order need approval?, answers POST /api/par/v1/orders/needs-approval:

Both start with the same line, import 'par_order_rules';, and both call needsApproval. Neither
one has the number 1000 in it.
Trying it
In the Platform Shell, open Orders — Shared Server Logic and add orders:
- 500, no note: saved.
- 1500, no note: refused.

- 1500 with a note: saved.
Then we called the API with the same two totals:
{ "total": 500 } → { "needsApproval": false }
{ "total": 1500 } → { "needsApproval": true }
The step and the API agree, because they're asking the same function.
Change it in one place
Now the part the sample is for. Back in Scripts, we opened Order rules, changed return 1000;
to return 2000; and saved:

We didn't touch the step or the API. An order of 1500 with no note now saves:

And the API answered { "needsApproval": false } for 1500. Both changed at once, because the rule
was only ever written in one place. (We set it back to 1000 afterwards.)
Export keeps things private
What happens if the step tries to read the limit directly? We added approvalLimit() to the step's
script, without saving:

Function approvalLimit not found. It exists in Order rules, but it isn't exported, so to the step it doesn't exist. Export only what other scripts should call, and keep the rest as the script's own business.
Two ways to import
The line both scripts start with imports everything the module exports:
import 'par_order_rules';
BS Lang also lets a script name exactly what it takes:
import needsApproval from 'par_order_rules';
Naming the functions is worth it in a long script, because a reader can see at the top where needsApproval
comes from without opening anything. Either way, only exported functions can be imported, and the editor checks
each name: one the module doesn't export is reported as not found in module, before you save.
A script can import a script
Solution scripts can build on each other. In the Expense Limit sample, the limits live in one script and the form behaviour in another that imports it:
par_claim_limits needsApproval, needsSecondApprover, claimAmount, missingApproval
^ ^
| +---- par_claim_form (imports the limits, adds applyLimits for form rules)
| ^
| +---- two form rules: on load, and when the amount changes
+---- two plugin steps: refuse a claim missing its approval note or second approver
That split is deliberate. par_claim_limits calls only getValue and isEmpty, which every script has, so both the
form and the server can import it. par_claim_form calls setVisible and setRequired, which only a form has, so
only form rules import it. The limit is written once, at the top, and the browser and the server both obey it.
The BS editor: completion shows both scripts in full.
Organising solution scripts
A few habits keep shared code easy to find as a solution grows:
- One script per subject. Order rules, Claim limits, Delivery dates. A single script called Helpers becomes the place every function goes and nobody can find any.
- Module paths with your prefix.
par_order_rulescan't collide with another publisher'scon_order_rules, the same way every other component name works. - Export questions, not numbers.
needsApproval(amount)lets every caller ask the same question the same way. Exporting the limit itself invites each caller to compare it differently,>here and>=there. - Keep host-specific code out of shared scripts. A function that shows or hides fields belongs in a script only form rules import; a function that queries other records, in one only server scripts import.
- Comment the why. The sample scripts explain each choice in comments, such as why a parameter is
numberand notint. The next person to change the limit reads those first.
A number that changes per environment
The limit in Order rules is written in the code, so it is the same everywhere the solution is imported. That is right for a business rule that should be the same in Test and Production. When a value really differs per environment, such as an address or a threshold that Test keeps low to make testing easy, it belongs in an environment variable, which the script reads by name:
var limit = getEnvironmentVariable('par_approval_limit');
if (limit == '') {
throwValidationError('The approval limit is not set in this environment.');
}
The value comes back as text, and as empty text when the environment hasn't set it, so check before using it and say so plainly. Each environment sets its own value, and the code doesn't change. Environment variables are read on the server, so this version of the script is for plugin steps and Custom APIs. Environment variables and Environment variables across environments explain how they are set. When a limit is business data that people change often, such as approval bands per department, a table is better still, as the Capital planning sample does with its approval thresholds.
Changing a shared script safely
Changing a solution script changes every script that imports it, the moment it is saved. That is the point, and it deserves the same care as changing a saved query:
- Look for every script that imports it: search the solution's rules, steps and APIs for its module path.
- Make the change, and keep exported names and parameters as they were, so callers don't break.
- Try one caller of each kind: a form, a save, an API call.
If a function's parameters must change, add a new function with the new shape, move the callers over one by one, and remove the old one last.
Questions that come up
Does a solution script travel with the solution? Yes. It is a component like any other, so the step, the API and the rule they share arrive in Test together.
Can a script import a script from another solution? Yes, if both solutions are installed in the environment. The importing solution then depends on the other being there, so say so with a requirement on the package.
Is an imported script run every time? It runs as part of whatever imported it, each time that runs. There is nothing separate to start or stop.
Which scripts can import
| Script | Can import a solution script |
|---|---|
| Form rule | ✔ |
| Command button | ✔ |
| Plugin step | ✔ |
| Custom API | ✔ |
| Scheduled job | ✔ |
| Process step | ✔ |
A form rule importing a script runs it in the browser; a plugin step runs it on the server. The same script can be imported by both: it's the same language on each side, and a shared test suite holds the browser and server interpreters to the same answers. Just keep to functions both can call: what a script can see has the lists. The completion article shows a form rule importing one.
A process step's script can import too: the engine runs it with the environment's solution scripts. (An earlier version of this article said it couldn't. The engine could, but the process designer underlined the import line as Module not found. We fixed the designer while making the video.)
While writing this we also fixed two screens that said a script can be imported only from "a rule or a command". Both now name plugin steps and Custom APIs too, as the pictures above show.
Try it
Import the sample, try the three orders, then change the limit and try them again. Then add a
second exported function to Order rules, say approvalLimitText() returning
`Orders over ${approvalLimit()} need a note`, and use it in the step's message.
Next: calling an outside service from a script, with callService.