Create and manage Smart Actions
Please be sure of your agent type and version and pick the right documentation accordingly.
This is the documentation of the forest-express-sequelize and forest-express-mongoose Node.js agents that will soon reach end-of-support.
forest-express-sequelize v9 and forest-express-mongoose v9 are replaced by @forestadmin/agent v1.
Please check your agent type and version and read on or switch to the right documentation.
This is still the latest Ruby on Rails documentation of the forest_liana agent, you’re at the right place, please read on.
This is the documentation of the django-forestadmin Django agent that will soon reach end-of-support.
If you’re using a Django agent, notice that django-forestadmin v1 is replaced by forestadmin-agent-django v1.
If you’re using a Flask agent, go to the forestadmin-agent-flask v1 documentation.
Please check your agent type and version and read on or switch to the right documentation.
This is the documentation of the forestadmin/laravel-forestadmin Laravel agent that will soon reach end-of-support.
If you’re using a Laravel agent, notice that forestadmin/laravel-forestadmin v1 is replaced by forestadmin/laravel-forestadmin v3.
If you’re using a Symfony agent, go to the forestadmin/symfony-forestadmin v1 documentation.
Please check your agent type and version and read on or switch to the right documentation.
Create and manage Smart Actions
What is a Smart Action?
Sooner or later, you will need to perform actions on your data that are specific to your business. Moderating comments, generating an invoice, logging into a customer’s account or banning a user are exactly the kind of important tasks to unlock in order to manage your day-to-day operations.
On our Live Demo example, our companies collection has many examples of Smart Action. The simplest one is Mark as live.
Creating a Smart action
In order to create a Smart action, you will first need to declare it in your code for a specific collection. Here we declare a Mark as Live Smart action for the companies collection.
const { collection } = require('forest-express-sequelize');
collection('companies', {
actions: [
{
name: 'Mark as Live',
},
],
});const { collection } = require('forest-express-mongoose');
collection('companies', {
actions: [
{
name: 'Mark as Live',
},
],
});class Forest::Company
include ForestLiana::Collection
collection :Company
action 'Mark as Live'
endEnsure the file app/forest/__init__.py exists and contains the import of the previous defined class :
After declaring it, your Smart action will appear in the Smart actions tab within your collection settings.
A Smart action is displayed in the UI only if:
it is set as "visible" (see screenshot below) AND
in non-development environments, the user's role must grant the "trigger" permission
You must make the action visible there if you wish users to be able to see it.

It will then show in the actions dropdown button:

The Smart Action behavior is implemented separately from the declaration.
In the following example, we've implemented the Mark as live Smart Action, which simply changes a company's status to live.
You must make sure that all your Smart Actions routes are configured with the Smart Action middleware: permissionMiddlewareCreator.smartAction(). This is mandatory to ensure that all features built on top of Smart Actions work as expected (permissions, approval workflows,...).
You must make sure that all your Smart Actions routes are configured with the Smart Action middleware: permissionMiddlewareCreator.smartAction(). This is mandatory to ensure that all features built on top of Smart Actions work as expected (permissions, approval workflows,...).
The route declaration takes place in config/routes.rb.
The business logic in this Smart Action is extremely simple. We only update here the attribute status of the companies to the value live:
You must make sure that all your Smart Actions controllers extend from the ForestLiana::SmartActionsController. This is mandatory to ensure that all features built on top of Smart Actions work as expected (authentication, permissions, approval workflows,...)
Make sure your project urls.py file include you app urls with the forest prefix.
The route declaration takes place in app/urls.py.
The business logic in this Smart Action is extremely simple. We only update here the attribute status of the companies to the value live:
Note that Forest Admin takes care of the authentication thanks to the ActionView parent class view.
The route declaration takes place in routes/web.php.
The business logic in this Smart Action is extremely simple. We only update here the attribute status of the companies to the value live:
What's happening under the hood?
When you trigger the Smart Action from the UI, your browser will make an API call: POST /forest/actions/mark-as-live.
The payload of the HTTP request is based on a JSON API document.
The data.attributes.ids key allows you to retrieve easily the selected records from the UI.
The data.attributes.values key contains all the values of your input fields (handling input values).
Other properties of data.attributes are used to manage the select all behavior.
Should you want not to use the RecordsGetter and use request attributes directly instead, be very careful about edge cases (related data view, etc).
Available Smart Action options
Here is the list of available options to customize your Smart Action:
name
string
Label of the action displayed in Forest Admin.
download
boolean
(optional) If true, the action triggers a file download in the Browser. Default is false
endpoint
string
(optional) Set the API route to call when clicking on the Smart Action. Default is '/forest/actions/name-of-the-action-dasherized'
httpMethod
string
(optional) Set the HTTP method to use when clicking on the Smart Action. Default is POST.
description
string
(optional) Add a description shown in the smart action form. This supports html tags. ⚠️ only available in forest-express-sequelize and forest-express-mongoose 9.4.0
submitButtonLabel
string
(optional) Sets the text written on the submit button at the end of the form. Default value is the Smart Action name. ⚠️ only available in forest-express-sequelize and forest-express-mongoose 9.4.0
name
string
Label of the action displayed in Forest Admin.
download
boolean
(optional) If true, the action triggers a file download in the Browser. Default is false
endpoint
string
(optional) Set the API route to call when clicking on the Smart Action. Default is '/forest/actions/name-of-the-action-dasherized'
http_method
string
(optional) Set the HTTP method to use when clicking on the Smart Action. Default is POST.
description
string
(optional) Add a description shown in the smart action form. This supports html tags. ⚠️ only available in forest_liana 9.4.0
submit_button_label
string
(optional) Sets the text written on the submit button at the end of the form. Default value is the Smart Action name. ⚠️ only available in forest_liana 9.4.0
Want to go further with Smart Actions? Read the next page to discover how to make your Smart Actions even more powerful with Forms!
Available Smart Action properties
req.user
The JWT Data Token contains all the details of the requesting user. On any authenticated request to your Admin Backend, you can access them with the variable req.user.
req.body
You can find important information in the body of the request.
Customizing response
Default success notification
Returning a 204 status code to the HTTP request of the Smart Action shows the default notification message in the browser.
On our Live Demo example, if our Smart Action Mark as Live route is implemented like this:
We will see a success message in the browser:

Custom success notification
If we return a 200 status code with an object { success: '...' } as the payload like this…
… the success notification will look like this:

Custom error notification
Finally, returning a 400 status code allows you to return errors properly.

Custom HTML response
You can also return a HTML page as a response to give more feedback to the admin user who has triggered your Smart Action. To do this, you just need to return a 200 status code with an object { html: '...' }.
On our Live Demo example, we’ve created a Charge credit card Smart Action on the Collection customersthat returns a custom HTML response.

You can either respond with an HTML page in case of error. The user will be able to go back to his smart action's form by using the cross icon at the top right of the panel.

Setting up a webhook
After a smart action you can set up a HTTP (or HTTPS) callback - a webhook - to forward information to other applications.
To set up a webhook all you have to do is to add a webhookobject in the response of your action.
Another interesting use of this is automating SSO authentication into your external apps.
Downloading a file
On our Live Demo, the collection customers has a Smart Action Generate invoice. In this use case, we want to download the generated PDF invoice after clicking on the action. To indicate a Smart Action returns something to download, you have to enable the option download.
On our Live Demo, the collection customers has a Smart Action Generate invoice. In this use case, we want to download the generated PDF invoice after clicking on the action. To indicate a Smart Action returns something to download, you have to enable the option download.
On our Live Demo, the collection Customer has a Smart Action Generate invoice. In this use case, we want to download the generated PDF invoice after clicking on the action. To indicate a Smart Action returns something to download, you have to enable the option download.
Don’t forget to expose the Content-Disposition header in the CORS configuration (as shown in the code below) to be able to customize the filename to download.
On our Live Demo, the collection Customer has a Smart Action Generate invoice. In this use case, we want to download the generated PDF invoice after clicking on the action. To indicate a Smart Action returns something to download, you have to enable the option download.
Don’t forget to expose the Content-Disposition header in the CORS configuration (as shown in the code below) to be able to customize the filename to download.
On our Live Demo, the collection Customer has a Smart Action Generate invoice. In this use case, we want to download the generated PDF invoice after clicking on the action. To indicate a Smart Action returns something to download, you have to enable the option download.

Refreshing your related data
If you want to create an action accessible from the details or the summary view of a record involving related data, this section may interest you.
In the example below, the “Add new transaction” action (1) is accessible from the summary view. This action creates a new transaction and automatically refresh the “Emitted transactions” related data section (2) to see the new transaction.

Below is the sample code. We use faker to generate random data in our example. Remember to install it if you wish to use it (npm install faker).
Below is the sample code. We use faker to generate random data in our example. Remember to install it if you wish to use it (npm install faker).
Below is the sample code. We use the gem 'faker' to easily generate fake data. Remember to add this gem to your Gemfile and install it (bundle install) if you wish to use it.
Below is the sample code. We use the python Faker package to easily generate fake data. Remember to add this package to your requirements.txt and install it if you wish to use it.
Below is the sample code. We use the Faker package to easily generate fake data. Remember to add this package to your composer.json and install it if you wish to use it.
Redirecting to a different page on success
To streamline your operation workflow, it could make sense to redirect to another page after a Smart action was successfully executed.
It is possible using the redirectTo property.
The redirection works both for internal (*.forestadmin.com pages) and external links.
Here's a working example for both cases:
Your external links must use the http or https protocol.
Enable/Disable a Smart Action according to the state of a record
Sometimes, your Smart Action only makes sense depending on the state of your records. On our Live Demo, it does not make any sense to enable the Mark as Live Smart Action on the companies collection if the company is already live, right?
In the collection settings, you can configure the UI options of your Smart Actions.


Restrict a smart action to specific roles
When using Forest Admin collaboratively with clear roles defined it becomes relevant to restrict a smart action only to a select few. This functionality is accessible through Smart Actions Permissions in the Role section of your Project Settings. Learn more about roles.

Require approval for a Smart action
Critical actions for your business may need approval before being processed.
Set up your approval workflow
To add an additional layer of security over a smart action, head over to the Roles tab of your projects settings. From there, you'll be able to select Trigger with approval for that smart action. Note that this must be set for each role.
Review approval requests
Actions requiring approval will be available in the Collaboration menu (3) in the “Approvals” section:
“Requested” for all incoming requests (yours to approve or not)
“To Review” (4) for requests you need to review
“History” for all past requests.
In “To Review”, you will be able to approve or reject the request (5) with an optional message (6) for more details.


Review past approval requests
All past approval requests - made by you or other approvers - in the History tab (1).

You can get more details on a specific action by clicking on it:

Want to go further with Smart Actions? Read the next page to discover how to make your Smart Actions even more powerful with Forms!
Last updated
Was this helpful?