# 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](/files/twrnZU4PByecfrHryUML)

### 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](/developer-guide-agents-nodejs/agent-customization/actions/scope-context.md) for more detail                                                                                                                                                                                                        |
| execute           | **require** | The callable called when the action is executed, with `context` and `result builder` as parameters. See [context](/developer-guide-agents-nodejs/agent-customization/actions/scope-context.md#the-context-object) and [result builder](/developer-guide-agents-nodejs/agent-customization/actions/result-builder.md) 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](/developer-guide-agents-nodejs/agent-customization/actions/forms-dynamic.md) 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](/files/tPx0tCpKSlVDPYMqXIkA)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
