# Actions

{% hint style="success" %}
This is the official documentation of the `@forestadmin/agent` Node.js agent.
{% endhint %}

Sooner or later, you will need to perform actions on your data that are specific to your business.

Moderating comments, generating invoices, logging into a customer’s account, or banning users are exactly the kind of important tasks to unlock to manage your day-to-day operations.

![Custom Action displayed in a Table View](https://3861847666-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9UN5oBJhgzLadOqi7jx6%2Fuploads%2Fgit-blob-d43582d7cd00503688418f24ec3689808dc40730%2Factions-dropdown.png?alt=media)

### In your code

To create an Action, you will first need to declare it in your code for a specific collection. Here we declare a "Mark as Live" Action for the `companies` collection.

The action behavior is implemented in the `execute` function.

```javascript
agent.customizeCollection('companies', collection =>
  collection.addAction('Mark as live', {
    scope: 'Single',
    description: 'Mark the company as live',
    submitButtonLabel: 'Turn on',
    execute: async context => {
      // Implement your controller here.
    },
    form: [
      {
        type: 'Layout',
        component: 'Page',
        nextButtonLabel: 'Go to address',
        elements: [
          { type: 'Date', label: 'Live date', id: 'LiveDate' },
          { type: 'DateOnly', label: 'Exist since', id: 'CreationDate' },
          { type: 'Layout', component: 'Separator' },
          {
            type: 'Layout',
            component: 'Row',
            fields: [
              { type: 'Boolean', label: 'Credit Card ?', id: 'WithCreditCard' },
              {
                type: 'String',
                if: ctx => ctx.formValues?.WithCreditCard == true,
                label: 'Number',
                id: 'CreditCardNumber',
              },
            ],
          },
        ],
      },
      {
        type: 'Layout',
        component: 'Page',
        previousButtonLabel: 'Go back to general information',
        elements: [
          {
            type: 'Layout',
            component: 'HtmlBlock',
            content:
              async context => `If the company headquarter didn't change continue.<br/>
                    Otherwise set the new address. <br/>
                    <strong>The current address of the company is ${await context.getRecord(
                      ['fullAddress'],
                    ).fullAddress}.
                    `,
          },
          {
            type: 'Layout',
            component: 'Row',
            fields: [
              { type: 'Number', label: 'StreetNumber' },
              { type: 'String', label: 'StreetName' },
            ],
          },
          { type: 'Layout', component: 'Separator' },
          { type: 'String', label: 'PostalCode' },
          { type: 'Number', label: 'City' },
          { type: 'Layout', component: 'Separator' },
          { type: 'String', label: 'Country' },
        ],
      },
    ],
  }),
);
```

| Property          | Usage       | Description                                                                                                                                                                                                                                                                                                                                                                            |
| ----------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| scope             | **require** | `Single`, `Global` or `Bulk`. See [here](https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions/scope-context) for more detail                                                                                                                                                                                                                         |
| execute           | **require** | The callable called when the action is executed, with `context` and `result builder` as parameters. See [context](https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/scope-context#the-context-object) and [result builder](https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions/result-builder) pages for more details. |
| form              | *optional*  | A list of static fields to be input by the user or a function called with `context` as parameters which returns a list of fields. See [form](https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions/forms-dynamic) page for more details.                                                                                                              |
| description       | *optional*  | An optional description of the action. *Default: null*                                                                                                                                                                                                                                                                                                                                 |
| submitButtonLabel | *optional*  | A custom label for the submit button. *Default: the action name*                                                                                                                                                                                                                                                                                                                       |

### In the admin panel

After declaring it, the Action will appear in the Smart Actions tab within your Collection Settings.

{% hint style="info" %}
An 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
  {% endhint %}

You must make the Action visible there if you wish users to be able to see it in this Team.

![Making the Action visible](https://3861847666-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9UN5oBJhgzLadOqi7jx6%2Fuploads%2Fgit-blob-a2c3d4ee72768a9fa63d091617829bbf9b026488%2Factions-visibility.png?alt=media)
