Skip to content

Write a plugin script, step by step

Put a rule inside the save itself: refuse it, fill in a value, or update other records in the same breath. In this guide you write a real one for the Matter table, check it, save it switched off and learn how to test it and turn it on.

10 steps·8 min read·Customization

Logic that runs inside the save, written in a sentence and tested before it goes live.

What it is

  • A short JavaScript that runs when a record of one table is created, updated or deleted (or read), at one stage of the save.
  • The stages: Before checks (100) to refuse early, Before save (200) to check or change the record being saved, After save (300) to change other records inside the same save, and After, in the background (400) for work that may wait until the save is done.
  • It can read records, create, update and delete them, run a query across tables, call the platform's own operations and start a published workflow, all with the access of the person saving, or of a chosen Run as user.
  • An error in stages 100 to 300 refuses the save, and the person sees your message. At 200 and 300 everything the save did is undone with it; a record a stage 100 script wrote stays, so do not write records there.
  • It lives in Builder → Plugin Scripts, one studio per script: when it runs on the left, the code in the middle, Kuztom beside it.

Good to know

  • Reach for a script last. A validation rule, a computed field, a rollup, a field check or a workflow often does the job with no code: see Business rules.
  • A new script is saved switched on and runs from the next save. Press Switch off before Save while you are still working on it.
  • Scripts run in a sandbox on the server: no network, no files, no AI calls, with limits on time and memory. Paid or outside work is handed to an agent or a workflow, after the save.
  • To stop a script, switch it off; its body and settings stay. A switched-off script is read-only until it is switched on again.

Before you start

  • You need Builder → Plugin Scripts: Global Administrators have it. A script runs inside other people's saves, so keep it to administrators.
  • We build a rule for the Matter table: a matter that still has open tasks cannot be closed. When someone sets Status to Closed, the script looks for open tasks on the matter and, if it finds one, refuses the save with a clear message.
  • Check first that a lighter feature does not already do it. A rule about the same record is a validation rule; a value worked out from the same record is a computed field; totals of child records are a rollup: see Business rules. A script is for logic that reads or writes other records, as this one does.

Part 1. Find the scripts and start a new one

1Open Builder → Plugin Scripts

The list shows every script in the office: its name, the Table, the Message it runs on, When (the stage, in words), its Order and who it Runs as. A switched-off script carries an Off badge. Type part of a name, a table or a message in the search box; here matter.Update shows the scripts that run when a matter is changed.

Scripts that say After, in the background are the office's own agent triggers (Coin and Tenor): leave them as they are. Press New script to start one. It opens an empty studio and writes nothing until you press Save.

Write a plugin script, step 1: Open Builder → Plugin Scripts (Start a new script; Find by name, table or message; Include the ones that are off; This one is switched off)
1Start a new script2Find by name, table or message3Include the ones that are off4This one is switched off

Part 2. Choose when it runs

The left panel asks, in order: the table, the message (the operation), the stage, its place among the steps, the fields that start it and who it runs as. Each answer opens the next question. The platform names the script from these answers: table.Message.stage.order.

2Choose the table: Matter

Open Table and pick the table whose records start the script: Matter. The bar at the top fills in as you go: the script's name (matter.Message.stage until you finish), and the stage in words.

Look at the green On badge: a new script is saved switched on, and it runs from the very next save. If you want to save it before it is ready, press Switch off before Save (Part 4).

Write a plugin script, step 2: Choose the table: Matter (The table it runs on; New scripts start On)
1The table it runs on2New scripts start On

3Choose the message: Update

Open Message. The first group, On a record, holds the five record operations:

  • Create: a new record is saved.
  • Update: an existing record is changed. Pick this one for our rule: closing a matter is an update of its Status.
  • Delete: a record is deleted.
  • Retrieve and RetrieveMultiple: a record or a list is read (rare: to add or hide values on the way out).

Below them come the platform's own operations, and the office's Custom actions on this table: a script is also how a custom action of type Script does its work (see Custom actions). One script runs on one message, so a rule for Create and Update is two scripts.

Write a plugin script, step 3: Choose the message: Update (The record operations; A matter is changed)
1The record operations2A matter is changed

4Choose the stage: when, inside the save, it runs

Every save passes through four stages. The studio offers the first three:

  • Before checks (stage 100): before the save opens. Use it to refuse early. Never write records here: a write at this stage stays even if the save fails later.
  • Before save (stage 200): inside the save, before the record is written. The script can still change the values being saved, and an error refuses the whole save. Pick this for our rule.
  • After save (stage 300): inside the save, just after the record is written. Use it to create or update other records; an error still undoes everything.
  • After, in the background (stage 400): after the save has finished, on its own. You see it in the list on the office's agent triggers; it is for slow work that may lag the save.

The line under the choice repeats what the stage allows.

Write a plugin script, step 4: Choose the stage: when, inside the save, it runs (100: refuse early; 200: check or change this record; 300: change other records)
1100: refuse early2200: check or change this record3300: change other records

5Give it its place among the steps

At each stage the platform runs its own steps in order, and leaves the places 40 to 60 for scripts. The list under Its place shows what already runs at this stage, with its number. Type a free number: 50. Two switched-on scripts cannot share a place on the same step, and a number outside 40 to 60 is refused.

Write a plugin script, step 5: Give it its place among the steps (A free place, 40 to 60; What runs before and after it)
1A free place, 40 to 602What runs before and after it

6Starts when: only when Status is in the change. Runs as: the person

Starts when (on Update only) narrows the script to saves that carry one of the chosen fields. Leave it empty and the script runs on every change of a matter; add Status and it runs only when Status is part of the save. It looks at what is sent, not what changed, so the script still compares old and new itself.

Runs as: empty means the person whose save it is, with their access. Pick a service user when the script must read or write records that person may not see, or count rows they cannot see. Its writes then carry that user's name; inside the script, crift.initiatingUserId still names the person who saved.

What it can use here lists, for the stage you chose, what the script may read and change: at Before save, crift.input (the values being saved) is writable and crift.preImage holds the record as it was.

Write a plugin script, step 6: Starts when: only when Status is in the change. Runs as: the person (Only when Status is sent; Empty: the person saving; What this stage allows)
1Only when Status is sent2Empty: the person saving3What this stage allows

Part 3. Write the body and check it

7Write the body

The editor on the right is where the script goes. The strip above it says where it will run: Matter · Update · Before save, as the person whose operation it is. Type the body:

var newStatus = crift.input.status_id;
var oldStatus = crift.preImage ? crift.preImage.status_id : null;
var closing = Number(newStatus) === 2 && Number(oldStatus) !== 2;
var open = closing ? crift.retrieveMultiple('task', { regarding_entity_name: 'matter', regarding_record_id: crift.recordId, state_id: true }, ['id']) : [];
if (open[0]) throw new Error('This matter still has open tasks. Close them first, then close the matter.');

Line by line: read the new Status from what is being saved (crift.input) and the old one from the record as it was (crift.preImage); go on only when the matter is becoming Closed (Status 2); read the matter's open tasks, asking only for their ids; if there is one, throw an error. The error's text is what the person sees, and their save is refused. A save that changes anything else costs one comparison and nothing more.

The editor knows the crift object: hover a word for help, press Ctrl+Space for what is there, and it underlines mistakes as you type. Scripts run in a sandbox on the server, with no network or files and with time and memory limits.

Write a plugin script, step 7: Write the body (Where and as whom it runs; The message the person sees)
1Where and as whom it runs2The message the person sees

8Validate: is anything in the way?

Press Validate. It checks, before you save, that the table, message and stage are chosen and that the editor finds no error in the body. A green line, Nothing in the way, repeats where it will run. A red one lists what to fix, each with its line number; the badge beside it counts the same problems.

Validate does not run the script. The real test is a save on a test record (next step).

Write a plugin script, step 8: Validate: is anything in the way? (Check before saving; Ready to save)
1Check before saving2Ready to save

Part 4. Save it, test it, switch it on and off

9Switch it off, then Save

Press Switch off: the badge beside the name turns Off and the button now reads Switch on. Then press Save. The script is stored with its name, matter.Update.200.50, and does nothing until you switch it on. If you save it On, it runs from the next save of a matter.

Kuztom beside the editor can draft or change the body from a sentence ("refuse to close a matter with open tasks"); its proposal lands in the editor for you to read and save. Each question to Kuztom is an AI call and costs a little.

Write a plugin script, step 9: Switch it off, then Save (Switch off, pressed: now it offers Switch on; Store it, switched off; Draft it from a sentence (AI, costs))
1Switch off, pressed: now it offers Switch on2Store it, switched off3Draft it from a sentence (AI, costs)

10Test it on a test matter, then switch it on or off

Back in the list, the script shows Off. Open it and press Switch on, then Save: it runs from the next save. Test it at once on a test matter:

  1. Give the matter an open task (a task whose Regarding is the matter).
  2. Close the matter (its Status to Closed): the save is refused with your message.
  3. Close the task, then close the matter: this time it saves.
  4. Change only the matter's name: the script does not run at all (Status is not in the save).

To stop a script, open it, press Switch off and Save. Switching off is the way to stop one: its body and settings stay, and switching it on brings it back. While it is off, the script is read-only. The arrow icon opens it as an ordinary record, with its number and history.

Write a plugin script, step 10: Test it on a test matter, then switch it on or off (Off: it does not run; Switch on, then Save; Its record and history)
1Off: it does not run2Switch on, then Save3Its record and history

What next

  • The other half of server logic, the lighter half: Business rules (validation rules, computed fields, rollups).
  • Logic people call by name, from a button or a workflow, rather than logic that fires on a save: Custom actions.
  • Several steps, a wait or an approval after the save: Workflows.