Parsware
All articles

BS Lang

What a script can see

Every Parsware script is BS Lang, but a form rule, a plugin step, a Custom API and a step in a process can't call the same functions. What each one gets depends on where it runs and what it has in front of it, a form, a record being saved, a caller or a running process. Here is each list, why it differs, and what happens when you call a function from the wrong one.

The process designer with the What a script can call panel open on a step, showing groups for the process, the instance, the token and the variables, next to the title "What a script can see"

Every script in Parsware is written in BS Lang, with the same variables, loops and functions. What changes from one kind of script to the next is what the script can call: the functions the platform gives it to read and change things.

We call that list the script's scope. It depends on where the script runs, and on what it has in front of it when it does. This article compares four kinds of script using the eye button from the helpers panel, which shows each script's scope.

A form rule: the form in front of you

A form rule runs in the browser while somebody has a record open on a form. We opened one from the Expense Limit — Solution Script sample and pressed the eye:

A business rule's editor with the What a script can call panel listing getValue, setValue, setVisible, setDisabled, setRequired, eventType, changedField, isNewRecord, cancelSave and isEmpty

Ten functions, and every one is about the form:

  • getValue and setValue read and change a field on the form;
  • setVisible, setDisabled and setRequired change how a field behaves;
  • eventType and changedField say why the rule is running: the form opened, a field changed, or somebody pressed Save;
  • cancelSave stops the save and shows a message.

What's missing matters as much. A form rule can't read other records, and it can't refuse a save for good. cancelSave stops this form, but the record can still be saved some other way, through the API or an import, and the rule never runs there. Its own description says so: Not enforcement.

A plugin step: the record being saved

A plugin step runs on the server, inside the save itself, however the record was saved. This is the one from the Invoices — Credit Check sample:

The plugin step's editor with the panel listing getValue, getPreValue, setValue, throwValidationError, isEmpty, getEnvironmentVariable, message, stage, entityName, userId, depth, recordId, then getRecord, queryRecords and more

getValue and setValue are here too, spelled the same, but now they mean the record being saved, not a form. There's no setVisible, because there's no form to hide anything on. Instead a step gets:

  • getPreValue: what a field held before this save;
  • throwValidationError: refuses the save, whoever made it, and rolls it back;
  • message, stage, entityName and depth: which save is happening and where the step is in it;
  • getRecord, queryRecords, runQuery and the rest: reading and writing other records;
  • now and today, callService and sendEmail.

A Custom API: the caller

A Custom API answers an HTTP call from another system. It has no record in front of it, just a request:

The Custom API's editor with the panel listing getInput, setOutput, setHeader, setBody, setStatus, setContentType, throwValidationError, isEmpty, getEnvironmentVariable, userId, then getRecord and the other record functions

So there's no getValue at all. The request comes in through getInput, and the answer goes back through setOutput, setBody and setStatus. The record functions are the same as a plugin step's, spelled the same. It also has portalCustomerId and the other portal… functions, which say who is signed in when a portal makes the call. They refuse the call when nobody is.

A step in a process: the run

A script on a process step runs when a business process reaches that step. We opened Deduct Balance in the Leave Approval sample:

The process designer with Deduct Balance selected and the panel open over its On enter script. It starts with groups PROCESS (processName, processTitle), INSTANCE (instanceId, instanceStartedBy, instanceStartedAt, instanceOrgUnit), TOKEN (tokenId, tokenNode) and VARIABLES (getVariable("approved"), boolean)

A process script gets groups the others don't have:

  • Process and instance: which process this is, which run, who started it and when;
  • Token: which path through the process this script is on;
  • Variables: the values the process carries from step to step. The panel lists this process's own variables by name, so getVariable("approved") is right there.

It also gets the record functions, getRecordRef for the record the run is about, fail to stop the run with a message, and date helpers like addDays and minutesBetween.

The same name means the same thing

Put the four lists side by side and the rule is clear. A function is only offered where it can work, and where two scripts share a job, they share the spelling:

Function Form rule Plugin step Custom API Process step
getValue, setValue the form the record being saved — —
setVisible, cancelSave ✔ — — —
throwValidationError — ✔ ✔ —
getInput, setOutput — — ✔ —
getVariable, setVariable — — — ✔
getRecord, queryRecords, runQuery — ✔ ✔ ✔
now, today, isEmpty isEmpty only ✔ ✔ ✔

A command button, from the helpers panel, is a fifth list: it runs in the browser like a form rule, and adds openView, openRecord, notify and the other things a button does.

Calling a function from the wrong list

The editor checks each script against its own list. Here we added a getRecord call to the form rule:

The form rule's editor with var policy = getRecord('par_policy', getValue('par_policyid')); on line 7 underlined, and one problem: 7:14 Function getRecord not found. E030

Function getRecord not found, on the line, before saving. The function exists; it just isn't in this script's scope. If a form needs to know something about another record, that's either a lookup field on the form, or a check in a plugin step on the server.

Solution scripts: shared by several

A solution script is one that other scripts import, like par_claim_limits on line 2 above. It doesn't run on its own; it runs inside whatever imported it. So its editor accepts the functions of form rules, command buttons, plugin steps and Custom APIs all together, because it can't know yet which script will import it.

That means the check is looser in a solution script. If a shared script calls getRecord and a form rule imports it, the editor won't catch it, and the call fails when the rule runs. When you write a solution script, decide which kind of script it's for and keep to that list. A later article in this series, on shared logic, comes back to this.

Why the rule is running

Two of the scripts above can run for more than one reason, and each has a function that says which.

A form rule asks eventType(), which answers "OnLoad", "OnChange" or "OnSave". One shared function can then behave differently depending on the moment:

// Show the reason field whenever it applies, but only CLEAR it when somebody changes the status.
setVisible('par_rejection_reason', getValue('par_status') == 'rejected');
if (eventType() == 'OnChange' && getValue('par_status') != 'rejected') {
  setValue('par_rejection_reason', '');
}

Showing a field is right at any moment; clearing one is an edit, and an edit belongs to the moment a person made a change. On load, the stored value must survive.

A plugin step asks message() and stage(). message() is the write, "Create", "Update" or "Delete"; stage() is where in the save the step runs, "PreValidation", "PreOperation" or "PostOperation". Most steps are registered for one of each, but a step registered for several can tell them apart:

// Refuse lowering a credit limit by more than half, but say nothing on a new customer.
if (message() == 'Update') {
  var before = getPreValue('par_credit_limit');
  var after = getValue('par_credit_limit');
  if (!isEmpty(before) && !isEmpty(after) && after < before / 2) {
    throwValidationError('A credit limit can be lowered by at most half at once.');
  }
}

getPreValue is empty on a create, which is why the step checks it. Plugin steps: before and after a save explains the stages.

Steps that trigger steps

A plugin step that writes another record can set off that record's own steps, and they can write records in turn. depth() says how many writes deep the current one is: 1 for the save a person made, 2 for a write a step made during it, and so on. A step that might trigger itself, directly or round a loop of tables, guards against it:

// Keep the account's open-order total up to date, but never act inside our own update.
if (depth() == 1) {
  // … recount the account's open orders and update the account here …
}

The platform stops a script that runs too long anyway, but a depth guard stops a loop at its first turn, with nothing half-written.

Who a script runs as

Every script runs as somebody, and that person's permissions apply to everything it reads and writes.

  • A form rule and a command button run in the browser of whoever is using the form.
  • A plugin step runs as whoever made the save: the person on the form, the person running an import, or the app registration of an integration. userId() says who.
  • A Custom API runs as its caller, so a partner whose token can't read invoices gets nothing back from a query, however the script is written.
  • A process step runs on behalf of the run, and instanceStartedBy() says who started it.

So getRecord in a step returns nothing for a record the saving person may not read, exactly as it would for them on screen. A script never lends anybody more access than they already had. That is what makes it safe to let a maker write one.

What a refusal looks like, from each side

Script Refuses with The person sees
Form rule cancelSave('…') on save The message on the form; the record isn't saved from this form
Plugin step throwValidationError('…') The message wherever the save came from: form, import or API
Custom API throwValidationError('…') or setStatus The caller gets the refusal in the response
Data policy deny('…') The operation is refused with that sentence

throwValidationError stops the script on the spot, and a try/catch around it doesn't change that. So refuse before writing anything: a write earlier in the same script has already happened. Data policies covers the last row, a kind of script that decides whether a read or a write may happen at all.

Questions that come up

Why is getValue in both lists, if it means different things? Because the job is the same, the value of this field on the record I'm dealing with, and one spelling means one thing to learn. The record is on a form in one case and being saved in the other.

Can a form rule use a value from another record? Not by reading it. Put a lookup on the form, or show the value with a field filled in by a server rule, or check it on the server in a plugin step.

Where do I see the full list for my script? Press the eye button in its editor. It is generated from the same list the checker uses, so it is always right for that script.

Try it

Open a form rule, a plugin step and a Custom API in a development environment, press the eye in each, and compare. Then copy a line from one into another and see what the editor says.

Next: query records from a script: getRecord, queryRecords and runQuery, in a step that refuses an invoice.