Catalog scripting
Every script hook on catalog items, questions, bundles and reference filters, with the variables each one receives and what it must return.
This page lists every script field in the service catalog, the variables the script receives and the result it must return. It is for administrators who write catalog scripts and already know the record each script sits on.
How to read this page
Each table lists the hooks on one record. Variables are the names the script can read. Result is what the script returns: the value of its last expression, or the value it assigns to answer. The H3 under each table gives an example for each hook.
The variables are the same in every hook that lists them:
| Variable | Type | Description |
|---|---|---|
questions |
Record | The answers given so far, one field per question, named by the question’s Name. Read an answer with questions.<Name>.value(). Questions from question sets are included. |
value |
String | The answer being validated or processed. |
current |
Record | The record the hook works on: the generated record in a generation or transform script, the question in a process answer script, the catalog form in a reference filter script. |
sourceRecord |
Record | The record the form was opened from, when the item was opened from a form or an action. Empty otherwise. |
bundleCatalogId |
String | The ID of the bundle when the item or question is loaded as part of a bundle. Empty when the item is ordered on its own. |
answer |
Any | Set it to return the result. |
For the Table and DateTime objects used in the examples, see Table and DateTime in the API reference.
Question scripts
| Script | Field | Variables | Result |
|---|---|---|---|
| Precondition script | Precondition script on the question | questions, bundleCatalogId |
true shows the question. Any other result hides it. Runs after the Precondition condition, which must also match. |
| Validation script | Validation script on the question | questions, value, bundleCatalogId |
A message string blocks the answer and shows the message. A blank result accepts it. |
| Default from script | Default from script on the question | questions, bundleCatalogId, sourceRecord |
The default value. For a reference question, the ID of the record. Used only when no default function or default question is set. |
| Process answer | Process answer on the question | current, value, sourceRecord |
A string that replaces the answer copied to Record field. With Replace answer on, it also replaces the stored answer. |
| Transform script | Transform script on a Multiple records question | current, question, rows, sourceRecord |
Nothing. The script reads each row in rows and creates what it needs. Runs only when Transform mode is Script. |
Precondition script
Show the question only when the item is ordered on its own, not as part of a bundle:
answer = !bundleCatalogId;
Validation script
Reject an end date more than six months from today:
let message = "";
if (value && DateTime(value).isAfter(DateTime().plusMonths(6))) {
message = "End date must be within 6 months from now";
}
answer = message;
Default from script
Default the storage size from the answer to a question named MyColour:
let storage = "256GB";
if (questions.MyColour.hasChanged() && questions.MyColour.value() == "Black") {
storage = "512GB";
}
answer = storage;
Return true when the question is loaded as part of a bundle:
answer = !!bundleCatalogId;
Process answer
The script runs when the request is generated, before the answer is copied to Record field. Use it to copy a different value from the one the requester chose, such as a key of the referenced record instead of its ID.
Transform script
The script runs once per submission with every pending row of a Multiple records question. Each row’s cell values are named by the questions of the question set the question takes its columns from.
Catalog item scripts
| Script | Field | Variables | Result |
|---|---|---|---|
| Entitlement script | Visibility → Advanced tab | None | false hides the item from the user. Runs only if the user passes the Groups and Roles checks. |
| Post-processing script | Item configuration → Advanced tab | questions, answers, firstQuestions, questionId, bundleCatalogId |
The answers object, changed. Each entry is keyed by question name and carries readonly, mandatory, validationMessage, defaultValue and answer. An entry you drop keeps the engine’s response. |
| Generation script | Record generation configuration → Advanced tab | current, questions, sourceRecord |
Nothing. The script sets fields on current, the record being generated, before it is saved. |
| Cost script | Item configuration → Cost tab | questions, lineItems, bundleCatalogId |
An object keyed by frequency, each entry holding cost. See Cost script. |
In the post-processing script, firstQuestions is true when the form first loads. questionId is the ID of the question whose answer triggered the round.
Entitlement script
The script decides whether the user sees the item in the portal and in catalog search. A script that fails hides the item.
Post-processing script
The script runs each time the form asks the server for the next questions. Use it to set questions read-only or mandatory, or to set a validation message, from answers elsewhere on the form.
Generation script
The script runs after the record is filled from Name field, Fields and each question’s Record field, and before it is saved. Use it for values a field mapping cannot express.
Cost script
lineItems holds the costs from the One-off cost and Recurring cost fields, keyed one-off and by the Recurring frequency key (monthly, quarterly or annually). Return the same shape. Keys other than these are ignored, and costs of 0 are dropped.
Charge 10 per licence requested, once:
let quantity = questions.LicenceQuantity.value() || 0;
answer = { "one-off": { cost: quantity * 10 } };
Bundle scripts
| Script | Field | Variables | Result |
|---|---|---|---|
| Precondition script | Precondition script on a bundled item entry | questions |
true includes the bundled item in the bundle form. Runs after the Precondition condition, which must also match. |
The questions of a bundled item also receive bundleCatalogId in their precondition, validation, default and cost scripts, so a question can behave differently inside a bundle.
Bundled item precondition script
Use it when the condition builder cannot express the rule, for example a rule over several of the bundle’s answers.
Other catalog scripts
| Script | Field | Variables | Result |
|---|---|---|---|
| Category entitlement script | Entitlement script on a catalog category | None | false hides the category and its items from the user. Runs only if the user passes the Groups and Roles checks. |
| Reference filter script | Script on a reference filter | current, the catalog form with its answers |
A table record query. It is combined with the filter’s Condition. |
Reference filter script
Offer only selectable locations in the country chosen in a question named Country:
AND(
EQUAL("Selectable", true),
EQUAL("Country", current.Country())
)
Category entitlement script
The script works as the catalog item entitlement script does, for the category and everything in it.