Skip to content

Write a form script, step by step

When clicks are not enough, one short file shapes every form of a table: fields that appear, lock and become required as the record changes, a check before saving, your own ribbon buttons, and a part for each form. In this guide you read a table's script, write one for matters, and see the effect on records.

8 steps·7 min read·Customization

One short file shapes every form of a table, and a form never breaks.

What a form script is

  • One JavaScript file per table, edited on the Code tab of Builder → Forms. Every form of the table runs it.
  • It exports what it needs, each part optional: layout (sections, columns, hidden and read-only fields), onLoad (before the form is drawn), onChange (after any field changes), onSave (can stop the save), ribbon (the record's buttons) and forms (a part for one form, by its key).
  • Work up a ladder and stop at the lowest rung that does the job: the layout, then field state, then a catalogue control on one field, up to a whole custom body or your own control.
  • A file the Designer manages is split into marked regions: onLoad and onChange go in the triggers region, onSave, ribbon and forms in the exotic one. Code outside the regions does not run there.

Good to know

  • A form never breaks: if the script fails, the form shows its plain layout and says which part failed.
  • The script runs in the browser as the signed-in person; every read or write it makes goes through that person's access.
  • A rule that must hold however a record is saved (an import, the API, a workflow) belongs on the server: see Business rules.
  • State the contract version with apiVersion: the ribbon needs "1.2", the forms part "1.3", ctx.getUser() "1.4".

Before you start

  • You need to be a Global Administrator: Builder → Forms shows only to them.
  • Lay the form out first if you can: Forms, step by step. Reach for a script when a click is not enough.
  • We write a script for the Matter table: a Court section, fields that lock and appear by case, a check before saving, a ribbon button, and a part for one form. Try it on your own table; the matters shown here are invented.
  • A script runs in the browser as the person who opened the record, with their access. It costs nothing.

Part 1. Where a table's script lives

1Open Builder → Forms, pick the table, then Code

Go to Builder → Forms, pick Matter in the bar and click Code. A table has one script, matter.js, and every form of the table runs it: the line says which forms share it. The badges say how the file is kept (Hand-written, or managed by the Designer) and which version of the contract it targets (SDK v1.3). Save stays grey until you change something.

Write a form script, step 1: Open Builder → Forms, pick the table, then Code (The Code tab; One file, every form; The contract it targets; Saves the whole file)
1The Code tab2One file, every form3The contract it targets4Saves the whole file

2Read what the script does today

Start by reading the current file. Matter's script does two things you will reuse:

  • The sections list of the file's layout places the sections: general, then an Address section with its fields in order. Any field not listed falls into general, so a new field shows up by itself.
  • export function onLoad runs once before the form is drawn, so the first view is already right. Here it puts the address search control on the Find an Address field.
Write a form script, step 2: Read what the script does today (Rung 0: the Address section; onLoad: runs before the first paint; Rung 2: a catalogue control)
1Rung 0: the Address section2onLoad: runs before the first paint3Rung 2: a catalogue control

Part 2. Write it: the ladder, one rung at a time

Always use the lowest rung that does the job. 0 the layout (sections, columns, hidden and read-only fields). 1 field state (show, hide, require, lock, move a field). 2 a catalogue control on one field. 3 a click action inside a control. 4 a whole body built from platform parts. 5 a registered console. 6 your own custom control. Most forms never need more than rungs 0 to 2.

3Write the layout and the field rules

Replace the text with your own. The file is a small JavaScript module; each export is optional. First the layout (rung 0): three sections, one field hidden, one read-only.

export const apiVersion = "1.4";

export const layout = {
  columns: 2,
  labelPosition: "top",
  sections: [
    { name: "general", columns: 2,
      fields: ["name", "account_id", "legal_hold", "description"] },
    { name: "court", label: "Court", columns: 2,
      fields: ["court_case_identifier", "court_docket_identifier", "jurisdiction_id"] },
    { name: "address", label: "Address", columns: 2, fields: [
      "address_autocomplete", "address_line1", "address_city", "address_state_province",
      "address_postal_code", "address_country",
    ] },
  ],
  hidden: ["source_id"],
  readonly: ["intake_id"],
};

Then the rules (rungs 1 and 2). Put them in one function and call it from onLoad and onChange, so the first view and every later change follow the same logic. onChange runs after any field changes and receives the field's name.

function applyRules(ctx) {
  // While a matter is on legal hold, its name, client and description are locked.
  const held = ctx.getValue("legal_hold") === true;
  for (const f of ["name", "account_id", "description"]) {
    ctx.getAttribute(f).setReadOnly(held);
  }

  // The docket number shows, and is required, once there is a court case number.
  const inCourt = !!ctx.getValue("court_case_identifier");
  ctx.getAttribute("court_docket_identifier").setVisible(inCourt).setRequired(inCourt);
}

export function onLoad(ctx) {
  ctx.getAttribute("address_autocomplete")
    .setControl("address-autocomplete")
    .setFullWidth(true);
  applyRules(ctx);
}

export function onChange(ctx, changedField) {
  if (changedField === "legal_hold" || changedField === "court_case_identifier") {
    applyRules(ctx);
  }
}

Higher rungs, when one field is not enough. They are not part of the matter script above: each is an alternative you reach for only when the lower rungs cannot do the job. Rung 4 replaces the whole body of the form with parts the platform draws (any control above, a list of related records, the timeline, the audit trail); the layout no longer applies, while the header, the ribbon and your onLoad, onChange and onSave keep working. Rung 6 puts your own published custom control on a field, a section or the body (see Custom controls).

export function customBody(host, ctx) {
  const top = document.createElement("div");  host.appendChild(top);
  const grid = document.createElement("div"); host.appendChild(grid);
  const un1 = ctx.components.mount("richtext", top, {
    value: ctx.getValue("description"),
    controlConfig: { toolbar: "minimal" },
    onChange: (v) => ctx.getAttribute("description").setValue(v),
  });
  const un2 = ctx.components.mountComposite("activities-timeline", grid, {});
  return () => { un1(); un2(); };   // cleaned up when the form closes
}

Rung 6 on one field is a single line inside onLoad, naming your control by its key:

ctx.getAttribute("description").setControl("custom:matter_summary");

The Guided button is a switch. Guided (leave it on) underlines mistakes as you type, such as a field method that does not exist; click it for Loose, which checks only that the code parses. Reset drops your unsaved changes and goes back to the saved file.

Write a form script, step 3: Write the layout and the field rules (Type checks as you write; Save when it is right)
1Type checks as you write2Save when it is right

4Stop a save, add a ribbon button, give one form its part

onSave runs when the person presses Save. Call ctx.preventSave with a reason and the save does not happen; the reason shows on the form.

export function onSave(ctx) {
  if (ctx.getValue("court_case_identifier") && !ctx.getValue("jurisdiction_id")) {
    ctx.preventSave("A matter in court needs its Jurisdiction.");
  }
}

ribbon receives the record's buttons and returns them. Push your own; give it an order to place it. The built-in buttons are numbered 3 to 19, so 20 lands just after them. You can relabel or disable a built-in button, but never remove or move it.

export function ribbon(commands, ctx) {
  commands.push({
    key: "open-client", label: "Open the client", icon: "ExternalLink",
    order: 20, groupStart: true,
    disabled: !ctx.getValue("account_id"),
    onClick: () => window.open(`/account/${ctx.getValue("account_id")}`, "_blank"),
  });
  return commands;
}

forms adds a part for one form, by its key (shown in the Form box, see Forms). It runs after the table-wide code, and only when the record is open in that form.

export const forms = {
  M7QX2K4P: {                       // the key of your "Quick intake" form
    onLoad(ctx) {
      ctx.getAttribute("description").setRequired(true);
      if (!ctx.getUser().hasRole("global_administrator")) {
        ctx.getAttribute("legal_hold").setReadOnly(true);
      }
    },
  },
};

Then press Save. The next record anyone opens uses the new file.

Read-only by case, shown here with setReadOnly, holds on the form only. A rule that must hold however the record is changed belongs on the server: see Business rules.

Write a form script, step 4: Stop a save, add a ribbon button, give one form its part (Order 20 lands after these)
1Order 20 lands after these

5Or ask Kuztom to write it

The Kuztom button opens the Builder's assistant on this form. Say in plain words how the form should look and behave. It reads the table and the script, checks its proposal the way a save does, and puts the whole file on the Code tab, unsaved, for you to review and Save. Each message is a paid model call; the pill shows what it has spent.

Write a form script, step 5: Or ask Kuztom to write it (Say what you want; What it has cost)
1Say what you want2What it has cost

Part 3. See it on records

6Rung 2 on a matter: the address search

Open a matter in Matter Form. The Find an Address field shows as an address search across both columns, because of the one setControl line. A person types an address, picks one, and the fields below fill in. The stored value never changes type; only the way it is edited does.

The other catalogue controls: rich text, a code editor, a JSON viewer, a fixed dropdown, a lookup over a pair of fields, a file, a field with an action button.

Write a form script, step 6: Rung 2 on a matter: the address search (One line of script; Filled from one pick)
1One line of script2Filled from one pick

7Rung 1 on an account: fields pinned to columns

The account script pins each field to a column with setColumn and orders it with order: who the client is on the left, the address on the right. Space the order values (5, 10, 15) so a new field can slot in between later.

ctx.getAttribute("name").setColumn(1).order(5);
ctx.getAttribute("industry").setColumn(1).order(10);
ctx.getAttribute("address_autocomplete").setColumn(2).order(5);
ctx.getAttribute("address_line1").setColumn(2).order(10);
Write a form script, step 7: Rung 1 on an account: fields pinned to columns (Column 1; Column 2)
1Column 12Column 2

8One form, its own part: three columns on Account 2

The same account opened in Account 2, the table's second form. Its part of the script, under its key in forms, gives it three columns and hides the description; the table-wide code still runs first. Nothing else is copied: one file serves both forms.

Write a form script, step 8: One form, its own part: three columns on Account 2 (Open in Account 2; A third column, this form only)
1Open in Account 22A third column, this form only

What next