Parsware
All articles

BS Lang

The BS editor: completion

The BS editor offers names as you type. At the start of a line it lists the functions the script's host provides, the functions it imported from a solution script, and its own variables. After a dot it lists the value's methods. Here it is in a form rule from the Expense Limit sample, with what it offers and what it leaves to you.

The BS editor in a form rule with a completion list open: applyLimits void(), then cancelSave, changedField, eventType, getValue, isEmpty, isNewRecord, needsApproval, needsSecondApprover, setDisabled, setRequired, setValue, next to the title "The BS editor: completion"

You don't have to remember every function a script can call. The BS editor, where every Parsware script is written, completes names as you type: the functions the script's host gives it, the ones it imported, its own variables, and the methods of a value after a dot. Each suggestion shows its signature, so you see what it takes before you choose it.

We used the Expense Limit — Solution Script sample (business-rules-expense-limit-solution-script). It has a Claim table, a form, and two form rules that both import one solution script, Claim limits. Import it into a development environment to follow along.

The rule

Open the sample, then Tables → Claim → Forms → Claim → Events, and open Apply the claim limits when the form opens. The whole rule is two lines: it imports the solution script and calls one of its functions.

The Business rule designer for Apply the claim limits when the form opens, with Runs when set to OnLoad and a two-line script: import 'par_claim_limits'; and applyLimits();

We typed into it to try completion, and pressed Cancel afterwards, so the sample was left as it came.

The host's functions

A form rule runs in the browser with a form in front of it, so it can read and change fields. Start a new line and type set:

Completion after typing set: setDisabled with void(field: string, disabled: bool), then setRequired, setValue and setVisible

The list narrows as you type. The highlighted item shows its signature: setDisabled takes a field name and a true or false, and returns nothing (void). Press Enter or Tab to insert it, Escape to close the list.

These are the functions of a form rule. A plugin step, which runs on the server, gets a different set, and a Custom API another. The editor offers only what the script can call where it runs, so a suggestion is always one that works there.

Everything in scope: Ctrl+Space

On an empty line, press Ctrl+Space:

Completion opened with Ctrl+Space on an empty line: applyLimits void() at the top, then the rule's functions and needsApproval and needsSecondApprover from the imported script

This is everything the rule can name at that point:

  • the form rule's own functions: getValue, setVisible, eventType, isNewRecord and the rest;
  • what the import brought in: applyLimits, needsApproval and needsSecondApprover, from Claim limits;
  • the rule's own variables and functions, once it declares some.

The solution script also has a function called approvalLimit, and it isn't in the list. That's deliberate. Claim limits doesn't export it, so the import doesn't bring it in, and a rule can't call it. Completion offers exactly what the rule is able to call.

Functions from another script

Type needs:

Completion after typing needs: needsApproval with bool(amount: number), and needsSecondApprover

needsApproval takes an amount that is a number and answers true or false. You can see that without opening the other script. It's the reason to import a solution script rather than copy its code: the logic lives in one place, and completion still shows you what it offers.

After a dot: the value's methods

Completion also works after a dot. We declared some text, var note = 'Over the limit ';, and typed note.:

Completion after typing note.: includes with bool(searchString: string), indent, length, mark, purify, trim, trimEnd and trimStart

These are the methods of a string: trim removes the spaces at either end, includes checks whether it contains some text, and so on. The list depends on the kind of value: a list offers push, join, filter and the others instead. The form rule's own functions aren't in it, because note.getValue isn't something you can call. Keep typing and the list narrows: note.tr leaves trim, trimEnd and trimStart.

What it doesn't offer

  • Field names in a form rule. Inside getValue('...') you type the field's logical name, such as par_amount, yourself. The table's Fields page lists every field's logical name. A mistake there isn't underlined either (see errors as you type), so copy the name rather than typing it from memory.
  • Names inside a function body. Completion offers the names at the top of the script: its variables and functions, and the imports. Inside a function, its parameters and local variables aren't offered yet.

The scripts behind the suggestions

Completion offered applyLimits, needsApproval and needsSecondApprover because of two solution scripts in the sample. The form half, Claim form, is short:

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;

and the limits themselves, Claim limits, which the form script imports in turn:

func approvalLimit(): number {
  return 1000;
}

func needsApproval(amount: number): bool {
  return amount > approvalLimit();
}

func needsSecondApprover(amount: number): bool {
  return amount > approvalLimit() * 5;
}

func claimAmount(): number {
  var raw = getValue('par_amount');
  if (isEmpty(raw)) {
    return 0;
  }
  return raw;
}

export claimAmount, missingApproval, needsApproval, needsSecondApprover;

(missingApproval, which the server rules use to word their refusal, is left out here.) Two details are the ones completion helps you get right:

  • amount: number, not int. The amount is a decimal field, so a real claim arrives as 1200.50. A parameter declared int wouldn't accept it, and the rule would fail on every value except a whole number, the one value people test with. Completion shows needsApproval(amount: number), so the type is in front of you every time you call it.
  • approvalLimit isn't exported. It is the script's own business, so completion never offers it to the rules. They ask does this need approval? rather than reading the number and comparing it themselves.

Which functions each place offers

The host's part of the list is what changes most from one script to another. A few examples of what you will see after typing a few letters:

You type In a form rule In a plugin step In a command button
set setValue, setVisible, setRequired, setDisabled setValue setValue
get getValue getValue, getPreValue, getRecord, getEnvironmentVariable getValue
open openRecord, openView, openScreen, openUrl, …
throw throwValidationError

The pattern follows where the script runs. Only a form has fields to show and hide, only the server can refuse a save for everybody, and only a command bar navigates. The editor offers what makes sense where you are, and the helpers panel lists the whole set for each host with a sentence on each function.

The keys

Key Does
Typing a name Opens the list, narrowing as you go
Ctrl+Space Opens the list on demand, with everything in scope
Up, Down Move through the suggestions
Enter or Tab Insert the highlighted one
Escape Close the list without inserting anything
After a dot The methods of the value before it

The editor is the same one in every place a script is written, so these keys work in a form rule, a plugin step, a Custom API, a process step and a screen's code page alike.

Writing a rule with completion

A short walk-through, to make it a habit. Suppose a claim's Currency must be shown only when the amount is over the limit:

  1. Type needs and press Enter on needsApproval. The editor inserts the name.
  2. Type ( and signature help shows amount: number. Type claimAmount(), which completion also offers.
  3. Wrap it: var over = needsApproval(claimAmount());
  4. Type setV, pick setVisible, and fill in 'par_currency', over.
  5. Pause. No red underline, and no problems list: the script reads and every name exists.

Nothing in those five steps was typed from memory except the field's logical name, which is the one thing the editor can't know. Signature help and formatting covers step 2 in more detail.

Questions that come up

Why doesn't completion offer a function I know exists? Either it isn't available where this script runs, or it lives in a solution script that this one doesn't import, or that script doesn't export it. Check the host in the helpers panel, then the import and export lines.

Does completion know about my tables? It offers names from the language, the host and the scripts; field and table names are text in quotes, which you take from the table's Fields page.

Can I use completion in a Studio binding? Yes. A binding on a screen is written in the same editor, on one line, and offers the screen's own functions and state.

Three gaps we found and fixed

Three things were wrong with completion when we started this article, and all three are fixed:

  • It left out imported functions. Typing needs offered nothing, although the rule could call both functions. It offered only the host's functions, never the script's own names or anything it imported. Now it offers all three, as in the pictures above.
  • After a dot it mixed in the host's functions, then gave up. Typing note. listed the string's methods and getValue, setVisible and the rest, which can't go after a dot. One letter later the list closed. The editor was cutting one character too many off the end before asking for suggestions. Now it lists only the value's methods and keeps narrowing as you type.
  • A list was offered its elements' methods. After lines. on a list of text, it offered trim and includes, the methods of one piece of text, and never push or join. It now offers a list's methods for a list.

Each has a test, in the editor or in the language.

Try it

Import the sample, open either rule, and press Ctrl+Space on an empty line. Then open Scripts → Claim limits and look at the export line: every name on it is one completion offers in the rules.

Next: signature help, and formatting a script with one key.