> For the complete documentation index, see [llms.txt](https://docs.avonnicomponents.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/lwc-container/react-to-an-lwc-event.md). Agents can also search and read this documentation through its [MCP server](https://docs.avonnicomponents.com/~gitbook/mcp).

# React to an LWC Event

Let a button inside your custom LWC run an Avonni interaction, such as a toast or a flow, through the LWC Container's Custom Interactions.

The account manager from [Pass a Selected Row to an LWC](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/lwc-container/pass-a-selected-row-to-an-lwc.md) wants to flag an account for review without leaving the page. A **Request review** button in the `accountSnapshot` LWC fires an event; the LWC Container catches it and shows a toast naming the account. Swap the toast for **Execute Flow** or **Open Flow Dialog** later and the same button starts a real review process.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2F1Ph1ucyzjGxD8PVjsBiJ%2Flwc-container-react-to-an-lwc-event-result.png?alt=media" alt="A success toast saying the selected account is flagged for review, after the button in the snapshot was clicked"><figcaption><p>The button in the LWC fires an event, the container shows the toast.</p></figcaption></figure>

## Before you start

* The table and the snapshot from [Pass a Selected Row to an LWC](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/lwc-container/pass-a-selected-row-to-an-lwc.md), working in Preview.
* A developer to add the button to the LWC (two small changes, below).

## Add the button to the LWC

The LWC dispatches a `CustomEvent` named `requestreview`. The name is all the container receives: anything the LWC puts in the event's `detail` does not reach the interaction. The interaction reads the account from the Data Table instead.

{% code title="accountSnapshot.js (add this method)" %}

```javascript
handleRequestReview() {
    this.dispatchEvent(new CustomEvent('requestreview'));
}
```

{% endcode %}

{% code title="accountSnapshot.html (add inside the lwc:if block)" %}

```html
<lightning-button label="Request review" onclick={handleRequestReview}></lightning-button>
```

{% endcode %}

Use a lowercase event name with no spaces or hyphens. The container listens on the LWC itself, so the event needs no `bubbles` or `composed` option.

## Build it

Choose how you want to build:

✨ **Build with AI**: give your AI assistant a ready-to-use prompt.

🛠️ **Build with clicks**: follow the steps in the builder yourself.

{% tabs %}
{% tab title="Build with AI" icon="wand-magic-sparkles" slug="react-to-an-lwc-event-build-with-ai" %}
Your assistant adds the interaction with the `avonni-dynamic-components` skill; the updated `accountSnapshot` LWC and the Dynamic Component from the previous tutorial must already be in your sandbox.

{% stepper %}
{% step %}

### Set up the Avonni Skills and MCP server

Follow [Setup](https://docs.avonnicomponents.com/dynamic-components/build-with-ai/setup.md) to install the Avonni Skills, connect your AI assistant to the Avonni MCP server, and sign in to your sandbox through Salesforce CLI.

Deploy the version of `accountSnapshot` that dispatches `requestreview` first.
{% endstep %}

{% step %}

### Copy the prompt and review the plan

Paste the prompt into your configured assistant. Check the target org and the field mappings in its plan before you approve it.

{% prompt description="Prompt for this tutorial" icon="wand-magic-sparkles" openInAIProviders="false" defaultExpanded="partial" %}
{% code overflow="wrap" collapsedlinecount="10" %}

```
Use the avonni-dynamic-components skill. Verify the LWC Container component, its Custom Interactions property and the Show Toast interaction against the Avonni MCP server before you use them.

Inspect my sandbox: find the Dynamic Component that holds the AccountsTable Data Table and the LWC Container showing c/accountSnapshot, and confirm that the accountSnapshot LWC dispatches a CustomEvent named requestreview.

On that LWC Container, add one Custom Interactions item: Event Name requestreview, Type Show Toast, Title "Review requested", Message "{!AccountsTable.firstSelectedRowSObject.Name} is flagged for review.", Variant success. Change nothing else.

Show me the plan and the field bindings before writing files. Then draft the local metadata, list what is left for me to set up by hand, and stop before deployment or activation.
```

{% endcode %}
{% endprompt %}
{% endstep %}

{% step %}

### Deploy to the sandbox and check the result

Review what the assistant generated and what it left for you to configure. Deploy the approved files to your sandbox only, finish any remaining setting with the **Build with clicks** tab, then check that **Request review** shows a green toast with the selected account's name in Preview.
{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="Build with clicks" icon="screwdriver-wrench" slug="react-to-an-lwc-event-build-with-clicks" %}
{% stepper %}
{% step %}

### Open the container's interactions

Select the LWC Container on the canvas and open the **Interactions** tab of the **Edit Lwc Container** panel.
{% endstep %}

{% step %}

### Add a Custom Interactions item

1. Click **Add Custom Interactions**.
2. **Event Name**: `requestreview`, exactly as the LWC dispatches it.
3. **Type**: **Show Toast**.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FyQ0IZdktnKGZlgfTYrHF%2Flwc-container-react-to-an-lwc-event-custom-interactions.png?alt=media" alt="Custom Interactions item with Event Name requestreview and a Show Toast interaction with its title, message and variant" width="320"><figcaption><p>Event Name matches the name the LWC dispatches.</p></figcaption></figure>
{% endstep %}

{% step %}

### Write the toast

1. **Title**: `Review requested`.
2. **Message**: use **Insert** to add **Accounts Table** > **First Selected Row sObject** > **Name**, then type `is flagged for review.` after it. The message reads `{!AccountsTable.firstSelectedRowSObject.Name} is flagged for review.`
3. **Variant**: **Success**.
   {% endstep %}

{% step %}

### Save and test

Save, then open Preview. Select an account and click **Request review** in the snapshot. A green toast reads "Review requested" with the account's name. On the canvas the button does nothing: Custom Interactions run only in Preview and on the page.
{% endstep %}
{% endstepper %}
{% endtab %}
{% endtabs %}

## Troubleshooting

<details>

<summary>Clicking the button does nothing.</summary>

Check, in this order: you are in Preview, not on the canvas; **Event Name** matches the dispatched name letter for letter (`requestReview` and `requestreview` are two different events); the updated LWC is deployed.

</details>

<details>

<summary>The toast shows but the account name is blank.</summary>

No row is selected in the table, so its **First Selected Row sObject** is empty. Select an account before you click, or hide the button in the LWC until `accountId` has a value.

</details>

## Related tutorials

* [Pass a Selected Row to an LWC](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/lwc-container/pass-a-selected-row-to-an-lwc.md): the table and snapshot this page starts from.
* [Show a Placeholder in the Builder](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/lwc-container/show-a-placeholder-in-the-builder.md): keep the snapshot readable on the canvas, where its button cannot work.
* [Show Toast](https://docs.avonnicomponents.com/dynamic-components/component-builder/interactions/show-toast.md): every option of the toast used here.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.avonnicomponents.com/dynamic-components/tutorials/components/lwc-container/react-to-an-lwc-event.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
