# Actions

{% hint style="success" %}
This is the official documentation of the `forestadmin-agent-django` and `forestadmin-agent-flask` Python agents.
{% 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://2921382565-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HgnlEINLUAEQC1KgN48%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.

```python
from typing import Union
from forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSingle
from forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilder
from forestadmin.datasource_toolkit.interfaces.actions import ActionResult

async def execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> Union[None, ActionResult]:
    pass
    # implement your controller here

agent.customize_collection("companies").add_action("Mark as Live", {
    "scope": "Global",
    "execute": execute,  # this method can be a callable, awaitable or a lambda
    "description": "Mark the company as live",
    "submit_button_label": "Validate 🫵",
    "form": [
        {
            "type": "Layout",
            "component": 'Page',
            "next_button_label": "Go to address",
            "elements":[
                {"type": "Date", "label": "Live date","id": "LiveDate"},
                {"type": "Date", "label": "Exist since", "id": "CreationDate"},
                {"type": "Layout", "component": "Separator"},
                {
                    "type": "Layout",
                    "component": "Row",
                    "fields": [
                        {"type": "Boolean", "label": "CreditCard ?" "id": "WithCreditCard"},
                        {
                            "type": "String",
                            "if_": lambda ctx :ctx.form_values.get("WithCreditCard") is True,
                            "label": "Number",
                            "id": "CreditCardNumber",
                        },
                    ],
                },
            ],
        },
        {
            "type": "Layout",
            "component": "Page",
            "previous_button_label": "Go back to identity",
            "elements": [
                {
                    "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-python/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-python/agent-customization/scope-context#the-context-object) and [result builder](https://docs.forestadmin.com/developer-guide-agents-python/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-python/agent-customization/actions/forms-dynamic) page for more details.                                                                                                              |
| description           | *optional*  | An optional description of the action. *Default: null*                                                                                                                                                                                                                                                                                                                                 |
| submit\_button\_label | *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://2921382565-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2HgnlEINLUAEQC1KgN48%2Fuploads%2Fgit-blob-a2c3d4ee72768a9fa63d091617829bbf9b026488%2Factions-visibility.png?alt=media)
