A setup script configures the system from code: it adds and changes fields, sets what picking a record fills (autofill), sets defaults, settings and the menu, and defines any other configuration: models, views, form sections, automations, save rules, change rules, numbering. Write one when a configuration is easier to say as a loop or a condition than to click through Setup, or when the same configuration must be made the same way more than once.
A setup script never changes anything itself. When you run it:
- It runs as you (your companies and access rules), in a transaction that is always rolled back.
- Each
api.configwrite adds a change to a list. Nothing is saved. - The list is checked as a whole, as if every earlier change were made, and kept as a proposal: each change with what it creates or changes (before and after), and a problem when it can't be made.
- You review the proposal and press Apply. Apply checks every change again with your rights at that moment and makes them all, or none. Undo takes an applied proposal back, unless what it made was changed since.
These are the same proposals Droot AI makes, with the same checks, audit and Undo. You don't need Droot AI to run or apply a setup script.
Write and Run One
In Setup › Developer › Scripts, press New Script and choose Setup (Run on Demand) under Runs. A setup script names no model and acts as whoever runs it. Publish it, then press Run under Run and Apply. While you edit, Run can run the editor's version before you publish it.
Who can: running a setup script and applying its proposal needs Write Metadata and Author Scripts
(metadata.write, scripts.author), and access to every company, since configuration applies to all of them. Each
change also needs its own right, as it does in Setup (a setting the right its page asks for; a script Author Scripts).
// Adds a Delivery Zone to sales orders, filled from the customer, and makes it required in the settings.
if (!(await api.config.get("field", "sales.order:deliveryZone"))) {
await api.config.field("sales.order", "deliveryZone", {
label: "Delivery Zone", type: "lookup", lookupModelKey: "logistics.zone",
});
}
await api.config.fill("sales.order", "customer", [{ to: "deliveryZone", from: ["defaultZone"] }]);
await api.config.setting("sales.orders", { requireZone: true });
return "Adds the Delivery Zone to sales orders";
What the script returns, when it is text, becomes the proposal's summary. api.log lines show with the run, and every
run is kept in Recent Runs.
Run it again once its proposal is applied: the reads find everything in place, the script proposes nothing, and the page says Nothing To Change. Changes that would change nothing (the value is already so) are no proposal either. Write setup scripts so they can run again safely: read first, change what is missing.
The Configuration API
| Call | Proposes |
|---|---|
api.config.get(kind, key) |
Reads one row as a define takes it, or null. A field's key is <model>:<field>. |
api.config.list(kind, filter?) |
Reads the rows of a kind whose properties equal the filter's plain values ({ modelKey: "sales.order" }), at most 500. |
api.config.define(kind, input) |
A row of any kind: a new one (every required property), or the properties to change on an existing one. |
api.config.field(model, field, props) |
A field, or changes to one (label, type, required, options, lookupModelKey…). |
api.config.fill(model, lookupField, rules) |
What picking a record in a lookup fills: [{ to, from: [field] }]; [] removes the rules. |
api.config.default(model, field, value) |
A field's default; { expression } for a computed one. |
api.config.setting(key, values, company?) |
A setting's values, for every company, or for the one company named. |
api.config.menu(modules) |
The menu: [{ module, entries: [{ key, section? }] }]. |
api.config.remove(kind, key) |
Removing a row made here (an app's rows are switched off or customised instead). |
api.config.deleteField(model, field, reason?), api.config.restoreField(model, field) |
Deleting a field made here, or restoring one. |
api.config.changeFieldType(model, field, to, options?) |
A field's new type; its values are converted when the proposal is applied. |
The kinds are the metadata kinds (model, field, view, nav, setting, automation, save-rule,
change-rule, flow, template, …): the same names and properties as in an app's bundle, described by the JSON
Schemas in sdk/schemas. On an app's row, only what a tenant may customise can change (a label, options, a default);
the proposal says so for any other property.
Reads see this run's own changes: after api.config.field(...), api.config.get("field", ...) returns the field as it
will be, and a removed row reads as null.
Besides api.config, a setup script reads records as you (api.records.get, query, aggregate), settings,
determination tables and dates. It can't create or delete records, send email or notifications, submit approvals or
call connectors: those calls stop the run.
Filling Existing Records
A setup script can also fill fields of records that already exist: a new field's value on every customer, a code worked out from each record's name, the value a fill rule would have copied on records made before it existed.
| Call | Proposes |
|---|---|
api.records.fill(model, values, filter?) |
The same values on every record the filter finds (every record you can read without one), at most 500 per call. |
api.records.update(model, id, values) |
One record's own values; call it in a loop for values worked out per record. |
// A Tier field on customers, filled from their yearly revenue.
if (!(await api.config.get("field", "sales.customer:tier"))) {
await api.config.field("sales.customer", "tier", { label: "Tier", type: "select", options: ["gold", "silver"] });
}
// The field may not exist yet (this run proposes it), so read every customer and skip those already set.
const { items } = await api.records.query("sales.customer", { limit: 500 });
for (const c of items) {
if (c.tier) continue;
await api.records.update("sales.customer", c.id, { tier: Number(c.yearlyRevenue) >= 1000000 ? "gold" : "silver" });
}
These are proposals too, and they come after the configuration: when a run proposes both, the proposal has two steps. Step 1 is the configuration and Step 2 the records, because the records can be filled only once the fields they fill exist. Apply All applies both in order, or apply Step 1 and then Step 2. Until Step 1 is applied, a record fill of a new field is checked only for the records it names (each one must be one you can read). On Apply each record is written as the record API would write it as you: your role in the record's company and its field rights, read-only and computed fields refused, the record's own scripts and rules run. A record that already holds the values is left out, and one that can't take them is listed with the reason and left out. Undo puts each record's previous values back (the records step first, then the configuration), unless a record changed since.
A filter is read once, when the run is checked: Apply fills those records, not others that match later. Record reads
in the script see what is stored, not what the run proposes. Filling records needs Edit Records (records.write).
Limits
- At most 60 changes per run (a proposal's limit); split a larger setup into several scripts. A fill counts as one
change, and so do all of a run's
api.records.updatecalls on one model. - At most 500 records per fill, and 1,000 records filled per run in all (Apply writes them in one transaction). For more, run the script again: the records already filled are left out. Data Import suits a large data load better.
- The time limit of a script that runs in a save (2 seconds by default, at most 10), and the usual read and memory limits.
- A run that fails, or stops at a limit, proposes nothing.
Over the API
| Route | Does |
|---|---|
POST /api/admin/scripts/:key/run |
Runs the script (or { draft }, the editor's unpublished definition) and answers { run, changesets, changeset }: the run's status, logs and changes, and the stored proposal (one changeset, or two steps when it fills records too; empty when the run failed or there is nothing to change; changeset is the first). |
GET /api/admin/scripts/:key/changesets |
Your latest proposals from this script, newest first. |
GET /api/admin/script-changesets/:id |
One of them. |
POST /api/admin/script-changesets/:id/apply, /reject, /revert |
Apply, Reject, Undo. |
POST /api/admin/script-chains/:chainId/apply |
Apply All: both steps in order (it stops at a step that doesn't apply). |
A proposal is its author's: nobody else sees or applies it.