Skip to main content
Version: 0.46

Scripting API

Every script you write in Shesha, whether it is an event handler on a component, an expression that decides if a field is visible, or an action behind a button, runs with the same set of objects already in scope. You do not import anything. You reference the object you need and the code editor offers accurate autocomplete and inline documentation for all of it.


The Objects Available to a Script

ObjectWhat it gives you
formThe current form: its data, its mode, its components, and the functions to read, update, and submit it
formDataThe current form's field values. The same data as form.data
actionsCalling APIs, showing messages, opening dialogs, and navigating
utilsHelpers such as date handling, saving files, modals, and template evaluation
pagePage-scoped state shared between the components on the current page, and the browser location
userThe current user, their details, and their permission and role checks
storageThe browser's local and session storage
applicationApplication-wide services: entities, settings, navigation, and application-scoped state
queryThe query string values of the current page URL

A First Example

Every object is available in every script, so a single handler can read the form, call an API, and tell the user what happened.

Form type to use: Edit Form - use when the user is updating an existing record.

Example - Approve a record and confirm the result to the user:

const onClickAsync = async () => {
// Ask the user to confirm before doing anything irreversible.
const confirmed = await actions.showConfirmation({
title: 'Approve application',
content: 'Approve this application? The applicant will be notified.',
okText: 'Approve',
cancelText: 'Cancel'
});

if (!confirmed) return;

try {
await actions.callApi.post('/api/services/app/Applications/Approve', {
id: form.data.id
});
actions.showMessage.success('Application approved.');
form.submit();
} catch (error) {
form.setValidationErrors(error);
}
};

Moving From Older Script Names

Scripts written against earlier versions of Shesha keep running. The old names still resolve at runtime, so nothing breaks the moment you upgrade. They are no longer suggested or type-checked in the editor though, so you lose autocomplete and inline documentation until you move a script across.

Old nameUse instead
http.get(...), http.post(...)actions.callApi.get(...), actions.callApi.post(...)
message.success(...)actions.showMessage.success(...)
modal.showForm(...)actions.showDialog(...) or utils.modal.showForm(...)
modal.confirm(...)actions.showConfirmation(...) or utils.modal.confirm(...)
moment(...)utils.moment(...)
fileSaver(...)utils.saveAs(...)
pageContext.xpage.state.x
contexts.appContext.xapplication.state.x
contexts.formContext.xform.state.x
contexts.webStoragestorage.local and storage.session
globalState.x, setGlobalState(...)page.state.x, application.state.x, or form.state.x
selectedRowform.components - read the DataTable or DataList component directly
form.formModeform.mode
form.formSettingsform.settings
form.formArgumentsform.arguments
form.contextform.state
form.clearFieldsValue()form.clear()
form.setFieldValue(name, value)form.setFieldsValue({ name: value })
Migrate a script when you next edit it

There is no deadline and no bulk migration to run. The practical approach is to move a script to the new names the next time you open it, so you get autocomplete and type checking on the code you are actually working in.


Where Scripts Run

Scripts fall into two shapes, and the shape determines what your code has to return.

ShapeWhere you find itWhat it must do
Action scriptsThe Action Configuration section of clickable components, and event handlers such as On ChangeRun an action. These are asynchronous, so use async/await with try/catch
Expression scriptsSettings that accept a JavaScript value, such as Visible, Interaction Mode, and Custom ValidatorReturn a value. The name starts with get, for example getHidden
Expression scripts must return a value

An expression script that falls through without returning leaves the setting undefined, which usually reads as "off". Always return explicitly, even when returning false.