The default behavior, when no exception is thrown in the handler is to display a generic notification.
agent.customizeCollection('companies', collection =>collection.addAction('Mark as live', { scope:'Single',execute:async context => {// Not using the resultBuilder here will display the generic success notification.// (as long as no exception is thrown) }, }),);
Custom notifications
When customizing the notification message, you can use the resultBuilder to generate different types of responses.
agent.customizeCollection('companies', collection =>collection.addAction('Mark as live', { scope:'Single',execute:async (context, resultBuilder) => {// The resultBuilder can be used to generate different types of responses.if (/* ... Company is not live ... */) {returnresultBuilder.success('Company is now live!'); } else {returnresultBuilder.error('The company was already live!'); } }, }),);
HTML result
You can also return an HTML page to give more feedback to the user who triggered your Action.
agent.customizeCollection('companies', collection =>collection.addAction('Charge credit card', { scope:'Single',execute:async (context, resultBuilder) => {/* ... charge the credit card ... */if (/* ... the credit card was successfully charged ... */) {returnresultBuilder.success('Success', { html:` <p class="c-clr-1-4 l-mt l-mb">\$${record.amount /100} USD has been successfuly charged.</p> <strong class="c-form__label--read c-clr-1-2">Credit card</strong> <p class="c-clr-1-4 l-mb">**** **** **** ${record.source.last4}</p> `, }); } else {returnresultBuilder.error('An error occured', { html:` <p class="c-clr-1-4 l-mt l-mb">\$${record.amount /100} USD has not been charged.</p> <strong class="c-form__label--read c-clr-1-2">Credit card</strong> <p class="c-clr-1-4 l-mb">**** **** **** ${record.source.last4}</p> <strong class="c-form__label--read c-clr-1-2">Reason</strong> <p class="c-clr-1-4 l-mb">You can not charge this credit card. The card is marked as blocked</p> `, }); } }, }),);
File generation
Because of technical limitations, Smart Actions that generate files should be flagged as such with the generateFile option.
This will cause the GUI to download the output of the action, but will also prevent from being able to use the resultBuilder to display notifications, errors, or HTML content.
Smart actions can be used to generate or download files.
The example code below will trigger a file download (with the file named filename.txt, containing StringThatWillBeInTheFile using text/plain mime-type).
collection.addAction('Download a file', { scope:'Global',// This option is required to trigger the file download. generateFile:true,execute:async (context, resultBuilder) => {constrandom=Math.random();if (random <0.33) {// Files can be generated from JavaScript strings.returnresultBuilder.file('StringThatWillBeInTheFile','filename.txt','text/plain'); } elseif (random <0.63) {// Or from a Buffer.constbuffer=Buffer.from('StringThatWillBeInTheFile');returnresultBuilder.file(buffer,'filename.txt','text/plain'); } else {// Or from a stream.conststream=fs.createReadStream('path/to/file');returnresultBuilder.file(stream,'filename.txt','text/plain'); } },});
Redirections
To streamline your operation workflow, it could make sense to redirect to another page after an action was successfully executed.
It is possible using the redirectTo function.
The redirection works both for internal (\*.forestadmin.com pages) and external links.
After an action you can set up an HTTP (or HTTPS) callback - a webhook - to forward information to other applications.
Note that the webhook will be triggered from the user's browser, so it will be subject to CORS restrictions.
Its intended use is often to perform a login on a third-party application or to trigger a background job on the current user's behalf.
agent.customizeCollection('companies', collection =>collection.addAction('Mark as live', { scope:'Single',execute:async (context, resultBuilder) => {returnresultBuilder.webhook('http://my-company-name',// The url of the company providing the service.'POST',// The method you would like to use (typically a POST). {},// You can add some headers if needed. { adminToken:'your-admin-token' },// A body to send to the url (only JSON supported). ); }, }),);