# Result builder

{% hint style="success" %}
This is the official documentation of the `forestadmin/laravel-forestadmin` v2+ and `forestadmin/symfony-forestadmin` PHP agents.
{% endhint %}

Actions can be configured to achieve different results in the GUI.

Most actions will simply perform work and display the default notification, however, other behaviors are possible:

* [Displaying a notification with a custom message](#custom-notifications)
* [Displaying HTML content in a side panel](#html-result)
* [Generating a file download](#file-generation)
* [Redirecting the user to another page](#redirections)
* [Calling a webhook from the user's browser](#webhooks) (for instance to trigger a login in a third-party application)
* [Setting up response headers](#response-headers)
* [Invalidating related data](/developer-guide-agents-php/agent-customization/actions/related-data-invalidation.md)

### Default behavior

The default behavior, when no exception is thrown in the handler is to display a generic notification.

![](/files/9jXcOd9iHNOTj6Ih7zzv)

```php
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseAction;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Context\ActionContextSingle;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\ActionScope;

$forestAgent->customizeCollection(
    'Company',
    function (CollectionCustomizer $builder) {
        $builder->addAction(
            'Mark as live',
            new BaseAction(
                scope: ActionScope::SINGLE,
                execute: function(ActionContextSingle $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.

![](/files/r48ahd8clXU3hU3CL5pQ) ![](/files/qPD0yPPJpvz0S9Fl61cY)

```php
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseAction;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Context\ActionContextSingle;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\ResultBuilder;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\ActionScope;

$forestAgent->customizeCollection(
    'Company',
    function (CollectionCustomizer $builder) {
        $builder->addAction(
            'Mark as live',
            new BaseAction(
                scope: ActionScope::SINGLE,
                execute: function(ActionContextSingle $context, ResultBuilder $resultBuilder) {
                    if (/* ... Company is not live ... */) {
                        return $resultBuilder->success('Company is now live!');
                    } else {
                        return $resultBuilder->error('The company was already live!');
                    }
                }
            )
        );
    }
);
```

### HTML result

You can also return an HTML page to give more feedback to the user who triggered the Action.

![](/files/Fkaq4rHtZxO9Ajy5Uue4)

```php
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseAction;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Context\ActionContextSingle;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\ResultBuilder;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\ActionScope;

$forestAgent->customizeCollection(
    'Company',
    function (CollectionCustomizer $builder) {
    	$builder->addAction(
    	    'Mark as live',
    	    new BaseAction(
    	    	scope: ActionScope::SINGLE,
    	    	execute: function(ActionContextSingle $context, ResultBuilder $resultBuilder) {
    	    	    /* ... charge the credit card ... */
    	    	    $record = $context->getRecord(['amount', 'source:last4']);
    	    	    if (/* ... the credit card was successfully charged ... */) {
    	    	        return $responseBuilder->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 {
    	    	        return $responseBuilder->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

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

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).

```php
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseAction;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Context\ActionContextSingle;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\ResultBuilder;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\ActionScope;

$forestAgent->customizeCollection(
    'Company',
    function (CollectionCustomizer $builder) {
        $builder->addAction(
            'Download a file',
            new BaseAction(
                scope: ActionScope::GLOBAL,
                isGenerateFile: true,
                execute: function(ActionContextSingle $context, ResultBuilder $resultBuilder) {
                    $myfile = fopen('filename.txt', 'w');
                    fwrite($myfile, 'StringThatWillBeInTheFile');
                    fclose($myfile);

                    return $resultBuilder->file('filename.txt', 'filename.txt', 'text/plain');
                }
            )
        );
    }
);
```

### Redirections

To streamline your operation workflow, it could make sense to redirect to another page after an Action has successfully been executed.

It is possible using the `redirectTo` function.

The redirection works both for internal (`\*.forestadmin.com` pages) and external links.

{% tabs %}
{% tab title="Internal link" %}

```php
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseAction;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Context\ActionContextSingle;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\ResultBuilder;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\ActionScope;

$forestAgent->customizeCollection(
    'Company',
    function (CollectionCustomizer $builder) {
        $builder->addAction(
            'Mark as live',
            new BaseAction(
                scope: ActionScope::SINGLE,
                execute: function(ActionContextSingle $context, ResultBuilder $resultBuilder) {
                    return $resultBuilder->redirectTo('/MyProject/MyEnvironment/MyTeam/data/20/index/record/20/108/activity');
                }
            )
        );
    }
);
```

{% endtab %}

{% tab title="External link" %}

```php
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseAction;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Context\ActionContextSingle;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\ResultBuilder;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\ActionScope;

$forestAgent->customizeCollection(
    'Company',
    function (CollectionCustomizer $builder) {
        $builder->addAction(
            'Mark as live',
            new BaseAction(
                scope: ActionScope::SINGLE,
                execute: function(ActionContextSingle $context, ResultBuilder $resultBuilder) {
                    return $resultBuilder->redirectTo('https://www.royalmail.com/portal/rm/track?trackNumber=ZW924750388GB');
                }
            )
        );
    }
);
```

{% endtab %}
{% endtabs %}

### Webhooks

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.

```php
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\BaseAction;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Context\ActionContextSingle;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\ResultBuilder;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Actions\Types\ActionScope;

$forestAgent->customizeCollection(
    'Company',
    function (CollectionCustomizer $builder) {
        $builder->addAction(
            'Mark as live',
            new BaseAction(
                scope: ActionScope::SINGLE,
                execute: function(ActionContextSingle $context, ResultBuilder $resultBuilder) {
                    return $resultBuilder->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.
                    );
                }
            )
        );
    }
);
```


---

# 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-php/agent-customization/actions/result-builder.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.
