Parsware
All articles

BS Lang

BS Lang in ten minutes

BS Lang is the one scripting language in Parsware, used by form rules, buttons, plugin steps, Custom APIs, processes and Studio screens. If you know a little JavaScript you can read it already. Here are variables, lists, objects, functions, conditions and loops in one short script, and the few places it differs.

A BS Lang script in the dark-mode editor, with a list of three courses, a describe function and a loop, next to the title "BS Lang in ten minutes"

Parsware has one scripting language, BS Lang. A rule on a form, a button on a command bar, a plugin step on the server, a Custom API, a step in a business process and a Studio screen are all written in it, in the same editor. Learn it once and it works everywhere, with only the functions each place offers changing.

If you have read any JavaScript, most of BS Lang will look familiar. This article covers the core in one short script, then the handful of places where it's stricter than JavaScript, because those are what trip people up.

Somewhere to run it

A script needs somewhere to run. We used a command button (Buttons that do something): it runs in the browser when somebody presses it, and it can show a message, which is all we need. In the Sales solution we opened the view of the Training Course table, chose Add command, named it Course plan and set When pressed to Run a script:

The View Designer for All Training Courses with a new Course plan command selected in the Commands strip, and its properties: Label Course plan, Type Button, Placement Action bar, When pressed Run a script

Edit code opens the BS editor. This is the whole script:

The BS editor with the Course plan script: a list of three courses, a describe function with an if, and a for loop that adds up the hours and pushes a line per course, ending with notify

Variables, lists and objects

var courses = [
  { name: 'Safety basics', hours: 4 },
  { name: 'First aid', hours: 8 },
  { name: 'Forklift licence', hours: 16 }
];
  • var declares a variable. You can add a type, as in var total: int = 0;, but you rarely need to.
  • Square brackets make a list. Curly brackets make an object, with named members you read with a dot: courses[0].name is 'Safety basics'.
  • Strings can use single or double quotes.

A list has the methods you'd expect: push, pop, length(), join, map, filter, find, some and every. Reading past the end of a list is an error, not a quiet "nothing", so a mistake shows up where it happens.

Functions and conditions

func describe(hours: int): string {
  if (hours >= 8) {
    return 'full day';
  }
  return 'short';
}

A function is declared with func. Its parameters have types, and so does its answer. if and else work as in JavaScript, and so do &&, ||, !, the ternary a ? b : c and ??, which gives a fallback when the left side is null.

There are short functions too, written with an arrow: var double = (n: int) => n * 2;. They see the variables around where they were written, so they can be passed to map or filter.

Loops

var total = 0;
var lines = [];
for (course of courses) {
  total += course.hours;
  lines.push(`${course.name} (${describe(course.hours)})`);
}

for (x of list) walks a list; note there's no var before course. The counting form for (var i = 0; i < 3; i++) works too, and so do while, do … while, break and continue.

Show the answer

notify(`${courses.length()} courses, ${total} hours: ${lines.join(', ')}`);

Save the script, save the view, and press Course plan in the app:

The Training Courses list in the Training app with a message at the top: 3 courses, 28 hours: Safety basics (short), First aid (full day), Forklift licence (full day)

Three courses, 28 hours, each described by the function.

Where it's stricter than JavaScript

These are the rules that catch people. Each one is deliberate.

Text and numbers don't mix with +. 'Total hours: ' + total is refused. The editor underlines it before you save:

The BS editor with notify('Total hours: ' + total) underlined in red and one problem listed: Only numbers or strings are allowed in this expression. The next line, notify with a template, is fine

Put a number into text with a template, in backticks: `Total hours: ${total}`. JavaScript would quietly turn '5' + 1 into '51'. BS Lang won't guess.

Methods need their brackets. It's lines.length(), not lines.length. Without the brackets you've named the method without calling it, and that is an error.

Whole numbers and decimals are different types. 1 + 2 is the whole number 3. 1 + 2.0 is the decimal 3. Mixing them in a sum is fine: a whole number turns into a decimal when it meets one. But a variable keeps the type it started with, and var total = 0; starts as a whole number. When you'll be adding up decimal amounts, such as prices, start with var total = 0.0;.

No minus sign in front of a number. -7 doesn't parse. Write 0 - 7.

== never crashes. It compares numbers as numbers (1 == 1.0 is true) and text exactly, and two values of different kinds are simply not equal.

Empty is not always "nothing". A field with no value reaches most scripts as an empty string, "", not as a missing value. Check it with isEmpty(...), which is what the platform's own functions do.

Text

Strings have a few methods of their own, and like every method they need their brackets:

var name = '  Riley Miller  ';
var clean = name.trim();                 // 'Riley Miller'
var size = clean.length();               // 12
var isMiller = clean.includes('Miller'); // true

trimStart() and trimEnd() trim one side only. Joining text to text with + is fine, 'a' + 'b'; joining text to a number is the error described below, and a template is the answer.

Errors you can catch

A script can stop with an error of its own, and catch one it expects:

try {
  var course = courses[5];   // there are only three
  notify(course.name);
} catch (e) {
  notify('That course does not exist.');
} finally {
  refresh();
}

throw raises an error; try, catch and finally work as in JavaScript. On the server, a rule that must refuse a save doesn't throw a raw error, though: it calls throwValidationError('…'), which refuses the save and shows its message to whoever was saving, on the form, in an import's results or in an API's answer. Add a server validation rule shows one.

A script can't run away

Every script runs within a budget. A loop that never ends, or a function that calls itself for ever, is stopped rather than left to freeze a form or tie up the server:

while (true) { }   // stopped, with an error saying the script ran too long

The budget is generous: an ordinary loop over a few hundred items, or a recursive function a few dozen calls deep, finishes far inside it. And a catch can't swallow it. A script that wraps an endless loop in try is still stopped, because the budget is the platform's guard, not one of the script's own errors.

Sharing code between scripts

When two rules need the same logic, write it once in a solution script and import it. The script exports the functions it shares:

// Solution script: par_course_rules
func describe(hours: int): string {
  if (hours >= 8) { return 'full day'; }
  return 'short';
}
export describe;

and every script that needs it imports it by name:

import 'par_course_rules';

notify(describe(getValue('par_hours')));

The solution script travels with the solution like any other component, and the editor checks the import: a misspelt name, or a function the script doesn't export, is underlined before you save. Shared logic: solution scripts builds one used by a form rule and a server rule at the same time.

Where scripts run, and what each place offers

The language is the same everywhere; the functions differ, because a button in the browser and a step on the server can do different things.

Where Runs Can, for example Article
A client business rule In the browser, on a form Show, hide, require and fill fields as people type A client rule
A command button In the browser Read the form or the ticked rows, open records and views, save Buttons that do something
A plugin step On the server, before or after a save Read the record as it was and will be, refuse the save, read other records Plugin steps
A Custom API On the server, when another system calls Read the request, query, write records, answer Publish a Custom API
A process step On the server, as a process runs Read and set process variables, decide a branch Code on a task
A Studio screen In the browser Load records, react to clicks, navigate between screens Make a screen react

The editor knows where a script is going to run, and offers only that place's functions in its completion list. A function that place doesn't have is underlined, so you find out while typing, not when somebody presses the button. The helpers panel and the script reference lists them all.

The types, briefly

A variable can say its type after a colon, and a function always says the types of its parameters and its answer. The ones you will meet:

Type Holds
int A whole number
number A decimal
string Text
bool true or false
object Something with named members, such as a record
any Anything; used where a value's type can't be known in advance

Most of the time you can leave types off variables and let the first value decide, as long as that first value is the right kind. The 0.0 rule above is the one place it bites.

Questions that come up

Is BS Lang JavaScript? No, though it reads like a careful subset of it. It runs inside the platform, not in a general browser context, so a script can only do what the platform's functions let it, as the person running it.

Can a script reach the internet? A server script can call an outside service through callService, by the name of an environment variable that holds the address, so the address is configured per environment. Call an outside service shows how.

Where do I learn the functions? In the editor: completion lists them as you type, and signature help shows each one's parameters. The BS editor: completion starts there.

The same script, on either side

A form rule and a button run in the browser. A plugin step, a Custom API and a process run on the server. BS Lang has one interpreter for each, and both must pass the same set of test scripts, so a script means the same on either side. Everything above is in that set.

Try it

Add a button to any list in a development environment, paste the script, and press it. Then change it: add a fourth course, or have describe return 'half day' for four hours. The editor checks every line as you type.

Next: the BS editor, and the errors it shows as you type.