> 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/pass-a-selected-row-to-an-lwc.md). Agents can also search and read this documentation through its [MCP server](https://docs.avonnicomponents.com/~gitbook/mcp).

# Pass a Selected Row to an LWC

Send the account a user selects in a Data Table to your custom LWC through LWC Attributes, so the LWC always shows the row in focus.

An account manager reviews accounts in a Data Table. Beside it, your team's `accountSnapshot` LWC shows the industry, rating and annual revenue of the one account they select. The LWC Container passes the selected row's Id to the LWC each time the selection changes.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FK4fYUdxaX3J5Aml87hOE%2Flwc-container-pass-a-selected-row-to-an-lwc-result.png?alt=media" alt="Accounts table with one account selected and the account snapshot LWC showing that account"><figcaption><p>Select an account and the snapshot shows it.</p></figcaption></figure>

## Before you start

* The `accountSnapshot` LWC deployed to your sandbox. Its code is in [The LWC](#the-lwc) below: hand it to your developer if you do not write LWCs yourself.
* A Dynamic Component open in the builder. [LWC Container](https://docs.avonnicomponents.com/dynamic-components/components/lwc-container.md) describes every setting used here.

## The LWC

The LWC receives the account Id through an `@api` property named `accountId` and loads the record itself. That name is the one you will type in **LWC Attributes**.

{% code title="accountSnapshot.js" %}

```javascript
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME from '@salesforce/schema/Account.Name';
import INDUSTRY from '@salesforce/schema/Account.Industry';
import RATING from '@salesforce/schema/Account.Rating';
import REVENUE from '@salesforce/schema/Account.AnnualRevenue';

export default class AccountSnapshot extends LightningElement {
    @api accountId;

    @wire(getRecord, { recordId: '$accountId', fields: [NAME, INDUSTRY, RATING, REVENUE] })
    account;

    get name() { return getFieldValue(this.account.data, NAME); }
    get industry() { return getFieldValue(this.account.data, INDUSTRY); }
    get rating() { return getFieldValue(this.account.data, RATING); }
    get revenue() { return getFieldValue(this.account.data, REVENUE); }
}
```

{% endcode %}

{% code title="accountSnapshot.html" %}

```html
<template>
    <lightning-card title="Account snapshot" icon-name="standard:account">
        <div class="slds-p-horizontal_medium">
            <template lwc:if={account.data}>
                <p class="slds-text-heading_small">{name}</p>
                <p>Industry: {industry}</p>
                <p>Rating: {rating}</p>
                <p>Annual revenue: <lightning-formatted-number value={revenue} format-style="currency"></lightning-formatted-number></p>
            </template>
            <template lwc:else>
                <p>Select an account in the table.</p>
            </template>
        </div>
    </lightning-card>
</template>
```

{% endcode %}

{% code title="accountSnapshot.js-meta.xml" %}

```xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>63.0</apiVersion>
    <isExposed>true</isExposed>
</LightningComponentBundle>
```

{% endcode %}

## 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="pass-a-selected-row-to-an-lwc-build-with-ai" %}
Your assistant drafts the Dynamic Component with the `avonni-dynamic-components` skill; the `accountSnapshot` LWC must already be deployed to 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 `accountSnapshot` first, so the assistant can find it.
{% 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 names of the Data Table and LWC Container components against the Avonni MCP server before you use them.

Inspect my sandbox: confirm that the Account object has Name, Industry, Rating and AnnualRevenue, and that the Lightning Web Component c/accountSnapshot is deployed and has an @api property named accountId.

Build a Dynamic Component with two components side by side. A Data Table with the API Name AccountsTable, querying Account, showing Name, Industry, Rating and AnnualRevenue, with Max Row Selection set to 1. An LWC Container with LWC Name c/accountSnapshot and one LWC Attributes item: Name accountId, Value {!AccountsTable.firstSelectedRowSObject.Id}.

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 selecting an account in Preview shows its industry, rating and revenue in the snapshot.
{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="Build with clicks" icon="screwdriver-wrench" slug="pass-a-selected-row-to-an-lwc-build-with-clicks" %}
{% stepper %}
{% step %}

### Add the accounts table

1. Drag a **Data Table** onto the canvas and set its **API Name** to `AccountsTable`.
2. Set its query on **Account** and add the columns **Account Name**, **Industry**, **Account Rating** and **Annual Revenue**.
3. Set **Max Row Selection** to `1`, so the user picks one account at a time.
   {% endstep %}

{% step %}

### Add the LWC Container

1. Drag an **LWC Container** next to the table.
2. Set **LWC Name** to `c/accountSnapshot`.

*Note:* the namespace is part of the name. `accountSnapshot` alone does not load.
{% endstep %}

{% step %}

### Pass the selected account

1. In **LWC Attributes**, add an item.
2. **Name**: `accountId`, spelled exactly like the `@api` property.
3. **Value**: click the field to open the resource selector, then pick **Accounts Table** > **First Selected Row sObject** > **Id**. The field shows an **Id** resource pill. In a long field list, type `Id` in the search box.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FRf5Bu9sCNgnlx1sENAP8%2Flwc-container-pass-a-selected-row-to-an-lwc-attributes.png?alt=media" alt="The LWC Attributes item with Name accountId and Value bound to the Id of the row selected in AccountsTable" width="320"><figcaption><p>Name matches the LWC's @api property, Value reads the selected row.</p></figcaption></figure>
{% endstep %}

{% step %}

### Save and test

Save, then open Preview. The snapshot reads "Select an account in the table." Select an account: its name, industry, rating and annual revenue appear in the snapshot. Select another one and the snapshot follows.
{% endstep %}
{% endstepper %}
{% endtab %}
{% endtabs %}

## Troubleshooting

<details>

<summary>The snapshot stays on "Select an account in the table."</summary>

The **First Selected Row sObject** fills only when the user selects a row, so check that **Hide Checkbox Column** is off (with **Max Row Selection** at 1, that column shows radio buttons). Also check that **Name** is `accountId` exactly: `accountid` or `AccountId` does not reach the property.

</details>

<details>

<summary>The container shows "Errors while loading LWC component".</summary>

**LWC Name** does not match a deployed component. Check the `c/` prefix and the spelling, and that the bundle was deployed with `isExposed` set to `true`.

</details>

## Related tutorials

* [React to an LWC Event](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/lwc-container/react-to-an-lwc-event.md): add a button to the snapshot that runs an interaction.
* [Show a Placeholder in the Builder](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/lwc-container/show-a-placeholder-in-the-builder.md): give the snapshot a clean look on the canvas.
* [Data LWC Container or LWC Container?](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/data-lwc-container/data-lwc-container-or-lwc-container.md): when the LWC should get its records from an Avonni query instead.


---

# 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/pass-a-selected-row-to-an-lwc.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.
