Parsware
All articles

Model-driven apps

Buttons that do something

Add your own buttons to a list's or a form's command bar. Most need no code, because they open a view, start a record or open an address. When the button depends on what the user ticked or typed, it runs a short script.

A support ticket form in dark mode with Escalate and Urgent queue buttons on its command bar and the note "Escalated — press Save to keep it", next to the title "Buttons that do something"

The platform puts its own commands on every bar: New, Edit, Delete and Refresh on a list, and Save on a form. Your own commands sit beside them. This article is about what those commands do when someone presses them.

It uses the mda-support-ticket-commands sample. Import it into a development environment and open the Tickets — Commands app. The sample has no records, so we typed in five tickets. Every picture is the real product.

The list's own buttons

A ticket has a Summary, a Severity, a Status and Notes. The list's bar carries two buttons of the sample's own, Urgent only and Log another, after the platform's:

The Tickets list: New, Import data, Edit, Delete and Refresh, then Urgent only and Log another, and a three-dot menu open on Open the first ticked. Five tickets below, two of them urgent

Neither needs any code.

  • Urgent only opens another view of the table, Urgent tickets:

    The same list switched to the Urgent tickets view, showing Checkout times out at peak and Invoice PDF is blank

    The address changes too, so the result can be bookmarked or sent to someone. While writing this we found that this button changed the address and nothing else: the list kept showing every ticket. A list only read its view from the address when it first opened. It now follows the address, so a button can open another view of the table you're already on.

  • Log another opens a blank ticket. It names no table, which means "this table", so the same button can be copied onto another table's list and still work there.

When the button needs to know something

Open the first ticked is in the ⋯ menu. What it does depends on which rows the user ticked, and no list of choices can say that. So it runs a script:

if (selectedCount() == 0) {
  notify('Tick a ticket first.');
} else {
  openRecord('par_ticket', selectedId(0));
}

Press it with nothing ticked and it says so, rather than doing nothing:

The Tickets list with a message at the top of the screen reading Tick a ticket first.

A button on the form

Open Password reset email late. The form's bar carries Escalate and Urgent queue. Press Escalate:

The Password reset email late form after pressing Escalate: Severity now reads urgent, Notes has a new line "Escalated from the command bar.", and a message reads Escalated — press Save to keep it.

The severity changes to urgent and a line is added to the notes, but nothing is saved. Press Save to keep it, or leave and throw it away. A command changes the form in front of you. What may be stored is still decided by the server, with the same checks as any other save.

Split buttons

A split button has a menu of entries, and each entry can do something different. The ticket form's Reference sits in the ⋯ menu, where it's a heading with its entries under it:

The form's three-dot menu open: a Reference heading with Support handbook and Log a linked ticket under it

Support handbook opens a web address in a new tab. Log a linked ticket starts a new ticket.

While writing this we found that a split button in the ⋯ menu showed as one entry that ran only the button itself, so its entries could not be reached. Here the button itself does nothing, so pressing it only reported that. The menu now lists the entries under the button's name.

Setting it up

Open Tables → Ticket → Views → All tickets in the View Designer. The Commands strip shows the whole bar: the platform's commands with a dashed outline, yours with a solid one. Select Urgent only:

The View Designer's Commands strip with Urgent only selected, and its properties: Label Urgent only, Icon Filter, Type Button, Placement Action bar, When pressed Open a view, Table This table, View par_ticket_urgent

  • Label and Icon are what the button looks like.
  • Placement is the bar itself, or the ⋯ menu for commands people use less often.
  • When pressed is what it does: open a view, start a new record, open a dashboard, a screen or a report, start a process, open a web address, or run a script.
  • Table and View say where it goes. Leave the table empty to mean "this table".

A web address can include the record's fields, such as {telephone1}, and each value is encoded for you. The address must start with http:// or https://: a javascript: address is refused by the designer, by the import and by the app.

Choose Run a script and Edit code opens the BS Lang editor. This is Escalate's script, in the Form Designer:

The action script editor for Escalate: if getValue('par_severity') == 'urgent' notify 'Already urgent.', else setValue par_severity to urgent, add a line to par_notes, and notify 'Escalated — press Save to keep it.'

A script can read and set fields on the open form, read the ticked rows, open records, views and addresses, show a message, refresh, and press Save. The editor checks it and suggests functions as you type. The full list is on the help page for commands.

The commands, as the view and the form store them

A view's and a form's own commands are a list in their document, under actions, in the order they appear on the bar. The ticket list's:

"actions": [
  { "id": "act-open-urgent", "label": "Urgent only", "icon": "Filter", "kind": "button",
    "placement": "bar", "run": { "kind": "OpenView", "view": "par_ticket_urgent" } },
  { "id": "act-new-ticket", "label": "Log another", "icon": "Plus", "kind": "button",
    "placement": "bar", "run": { "kind": "NewRecord" } },
  { "id": "act-sep-1", "kind": "separator", "placement": "bar" },
  { "id": "act-open-selected", "label": "Open the first ticked", "icon": "fileText", "kind": "button",
    "placement": "overflow",
    "code": "if (selectedCount() == 0) {\n  notify('Tick a ticket first.');\n} else {\n  openRecord('par_ticket', selectedId(0));\n}\n" }
]

A command either has a run, which is one of the choices under When pressed, or code, which is a script. placement is bar or overflow, the ⋯ menu. A separator draws a line between groups of commands, and a split holds items, each with its own run, as the form's Reference does:

{ "id": "act-links", "label": "Reference", "icon": "link", "kind": "split", "placement": "overflow",
  "items": [
    { "id": "act-links-help", "label": "Support handbook",
      "run": { "kind": "OpenUrl", "url": "https://example.test/support-handbook" } },
    { "id": "act-links-new", "label": "Log a linked ticket", "run": { "kind": "NewRecord" } }
  ] }

Because commands are part of the view or form, they travel with it in the solution, and a second view of the same table can have a completely different bar.

Choose a built-in action first

Most buttons people want are one of the built-in actions, and every one of them needs no code:

When pressed What it does A typical button
Open a view Shows another list, of this table or another Urgent only, My open cases
New record Opens a blank form, of this table or another Log another, Add a contact
Open a report Runs a report on the record Print delivery note, Invoice
Start a process Starts an approval or another process on the record Submit for approval
Open a dashboard or a screen Opens a dashboard or a drawn Studio screen Today's figures
Open a web address Opens an address in a new tab, with the record's fields filled in Show on map, Support handbook
Run a script Anything else Open the first ticked, Escalate

The first six can't go wrong in a way a script can, because there is nothing to mistype. A Start a process button is how most approvals begin: the person presses Submit claim, and the claim goes to whoever the process says. Start a process from a record builds one. An Open a report button is how a form prints its own paperwork; An invoice shows the report it opens.

Scripts that read the form and decide

Choose Run a script when the button has to look before it acts. Escalate's script reads the severity, and changes it only if it isn't already urgent:

// Read the form, decide, and change it. It does NOT save: pressing Save is still yours.
if (getValue('par_severity') == 'urgent') {
  notify('Already urgent.');
} else {
  setValue('par_severity', 'urgent');
  setValue('par_notes', getValue('par_notes') + '\nEscalated from the command bar.');
  notify('Escalated — press Save to keep it.');
}

A command script uses the same BS Lang as every other rule, with the functions that make sense on a command bar: getValue and setValue for the form's fields, recordId for the open record, selectedCount and selectedId for the ticked rows of a list, openRecord, newRecord, openView, openScreen, openDashboard, openGuide and openUrl to go somewhere, runReport and startProcess to do the same as the built-in actions when a script has decided to, notify for a message, refresh to reload, and save to press Save on a form.

A few small scripts people often want:

// Open the account behind this contact.
var company = getValue('company');
if (isEmpty(company)) { notify('This contact has no company.'); } else { openRecord('account', company); }
// Mark as reviewed and save in one press.
setValue('par_reviewed', true);
save();

The second one presses Save for the person, so every check a normal save makes still runs: required fields, uniqueness rules, server validation. A script can't save anything the person couldn't have saved by hand.

Keeping the bar readable

The platform's commands come first, in a fixed order, and yours follow. A few habits keep a busy bar easy to scan:

  • Three or four of your own on the bar, the ones people press every day. Everything else goes in the ⋯ menu.
  • A verb that says what happens: Escalate, Print delivery note, Submit for approval. Not Action 1, and not the name of the script.
  • An icon that matches. Every icon-only button shows its name when the pointer rests on it and reads it to a screen reader, so the name matters even when the label is hidden.
  • A separator between groups, such as going somewhere and changing something.

Tidy command bars goes further: hiding platform commands a table doesn't need, and ordering what remains.

What a button can't do

A button runs in the browser, so it can't be the thing that enforces a rule. Anyone can decide not to press it. A rule that must always hold, whoever saves the record and however they do it, belongs in a server plugin step. A button can take people to the right place, fill in a form for them, and press Save for them, with the same checks as if they had pressed it.

Next: tidy command bars.