Skip to main content
Version: 0.46

Actions API

The actions object is how a script reaches out beyond the form. Use it to call an API, show a toast message, open a form in a dialog, ask the user a Yes/No question, or send the user to another page. It is available in every Shesha script.


Calling an API

actions.callApi object

An HTTP client that already knows your application's base URL and sends the current user's authentication token, so you do not have to build either yourself.

It exposes one method per HTTP verb.

MethodSignature
getget(url, config?)
postpost(url, data?, config?)
putput(url, data?, config?)
patchpatch(url, data?, config?)
deletedelete(url, config?)
headhead(url, config?)
optionsoptions(url, config?)

Every method returns a promise resolving to a response object with data, status, statusText, and headers. The body your API returned is on response.data.

Form type to use: Create Form - use when the user is creating a new record.

Example - Create a record through a custom endpoint:

const executeScriptAsync = async () => {
const payload = {
name: form.data.name,
author: form.data.author
};

try {
const response = await actions.callApi.post('/api/services/app/Books/Create', payload);
// The API's own response body sits on response.data.
return response.data;
} catch (error) {
console.error(error);
throw error;
}
};

The optional config argument tunes a single request.

OptionTypeWhat it does
headersobjectExtra request headers
omitStandardHeadersbooleanSends the request without Shesha's standard headers
timeoutnumberRequest timeout in milliseconds
responseTypestringOne of arraybuffer, blob, document, json, text, stream, formdata
signalAbortSignalLets you cancel the request

Form type to use: Details View - use when displaying a record as read-only.

Example - Download a generated document as a file:

const onClickAsync = async () => {
try {
// responseType 'blob' keeps the binary intact instead of parsing it as JSON.
const response = await actions.callApi.get(
`/api/services/app/Statements/Download?id=${form.data.id}`,
{ responseType: 'blob' }
);
utils.saveAs(response.data, 'statement.pdf');
} catch (error) {
actions.showMessage.error('Could not download the statement.');
}
};
Keep one API call per script

A script that makes a single call is far easier to debug than one that chains several. Where you need a follow-up step, make the call in the script and configure Handle Success or Handle Fail on the action to do the rest. See Action Configurations.


Showing a Message

actions.showMessage object

Shows a short toast message at the top of the screen. It disappears on its own, so use it for feedback the user does not need to dismiss.

MethodWhen to use
successThe action completed
errorThe action failed
warningThe action completed but something needs attention
infoNeutral information
loadingA long-running action is in progress

Each method takes the message text, an optional duration in seconds, and an optional callback that runs when the message closes.

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

Example - Confirm a save to the user:

const onSubmitSuccess = () => {
actions.showMessage.success('Your changes have been saved.');
};

Opening a Dialog

actions.showDialog(args) function

Opens a Shesha form inside a modal dialog and returns a promise. The promise resolves with the form's values when the user submits, and rejects when the user cancels.

ArgumentTypeWhat it does
formIdstring or objectThe form to open, either its id or { name, module }
titlestringThe dialog title
widthstringsmall, medium, large, full, or a CSS size such as 60% or 800px
modestringedit, readonly, or designer. Defaults to edit
formArgumentsobjectArguments passed through to the opened form
initialValuesobjectValues the opened form starts with
showCloseIconbooleanShows a close icon in the dialog header
footerButtonsstringdefault, custom, or none

Form type to use: Table / List View - use when showing multiple records.

Example - Capture a reason in a dialog before continuing:

const onClickAsync = async () => {
try {
const result = await actions.showDialog({
formId: { name: 'rejection-reason', module: 'Membership' },
title: 'Reason for rejection',
width: '60%'
});

// result holds the values the user submitted in the dialog.
await actions.callApi.post('/api/services/app/Applications/Reject', {
id: form.data.id,
reason: result.reason
});
actions.showMessage.success('Application rejected.');
} catch {
// The promise rejects when the user cancels the dialog, so there is nothing to do.
}
};

actions.showConfirmation(args) function

Asks the user a Yes/No question and returns a promise resolving to true if they confirm and false if they cancel.

ArgumentTypeWhat it does
titlestringThe dialog title
contentstringThe question. This one is required
okTextstringText on the confirm button. Defaults to Yes
cancelTextstringText on the cancel button. Defaults to No
okTypestringprimary, default, dashed, link, or text. Defaults to primary

Form type to use: Details View - use when displaying a record as read-only.

Example - Confirm before deleting:

const onClickAsync = async () => {
const confirmed = await actions.showConfirmation({
title: 'Delete record',
content: 'Are you sure you want to delete this record? This cannot be undone.',
okText: 'Delete',
cancelText: 'Cancel'
});

if (!confirmed) return;

await actions.callApi.delete(`/api/services/app/Books/Delete?id=${form.data.id}`);
actions.showMessage.success('Record deleted.');
};
Confirm before destructive actions

A confirmation dialog is the last thing standing between a user and a delete they cannot undo. Use one for any action that removes or overwrites data.


actions.navigateToUrl(url, queryParameters?) function

Sends the user to a URL, with optional query string parameters supplied as an object.

actions.navigateToUrl('/dynamic/Membership/members-list', { status: 'active' });

actions.navigateToForm(formId, args?) function

Sends the user to a Shesha form by its identifier, with optional arguments passed as query parameters.

actions.navigateToForm({ name: 'member-details', module: 'Membership' }, { id: form.data.id });
navigateTo does not exist

There is no navigateTo(...) function in Shesha. Use actions.navigateToUrl or actions.navigateToForm, or the Navigate action type in the designer.