> 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/flow/tutorials/components/tree-grid/work-with-selected-rows.md).

# Work with selected rows

Let an account manager tick accounts in a screen flow Tree Grid, then hand those accounts and their open cases to a colleague after Next.

An account manager hands part of a customer group to a colleague. On a flow screen they open the group's hierarchy, tick the subsidiaries that change hands, and click **Next**. The flow then reassigns those accounts, and their open cases with them, in one pass.

<figure><img src="https://27923732-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1FUd4apB9YHgCEMUFbVb%2Fuploads%2FgtE0UUHkmhnOHdXeXN53%2Ftree-grid-work-with-selected-rows-result.png?alt=media" alt="Tree Grid on a flow screen with two subsidiary accounts ticked and the selection count showing 2"><figcaption><p>Two subsidiaries ticked, ready to hand over after Next.</p></figcaption></figure>

## Before you start

* A screen flow whose first screen holds a Tree Grid showing the customer group's accounts, with the **Query** data source. [Show an account hierarchy](https://docs.avonnicomponents.com/flow/tutorials/components/tree-grid/show-an-account-hierarchy.md) sets it up.
* The examples use the API Name `AccountTree`. Replace it with yours in every reference.
* With the **Query** source the grid keys every row on the record `Id`, so the key outputs below return Account Ids.

## Steps

{% stepper %}
{% step %}

### Keep the checkboxes on

Select the Tree Grid on the screen and click **Open Component Builder**. In the **Properties** tab:

* Leave **Hide Checkbox Column** off. When it is on, users cannot tick rows and every selection output stays empty.
* Turn on **Show Number of Items Selected** so the user sees how many accounts are ticked ("2 items selected", with a **Clear selection** link).
* Ticking a parent row does not tick its subsidiaries: each account to hand over is ticked on its own.

The Tree Grid has no setting that makes a selection required or caps it. The third step makes it required in the flow.
{% endstep %}

{% step %}

### Pick the output you need

Every element after the screen can read the selection, through five outputs:

| Output variable                                        | Reference                                               | Use it for                                                                              |
| ------------------------------------------------------ | ------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| **Selected Rows**                                      | `{!AccountTree.selectedRows}`                           | The ticked records, for a **Loop** or an **Update Records**                             |
| **Selected Rows Key Field Value**                      | `{!AccountTree.selectedRowsKeyValue}`                   | The Ids as a text collection, for a **Get Records** with the **In** operator            |
| **Selected Rows Key Field Value comma separated**      | `{!AccountTree.selectedRowsKeyValueCommaSeparated}`     | One string, `'id1','id2'`, the shape a SOQL `IN (...)` clause expects in an Apex action |
| **Selected Rows Key Field Value semi colon separated** | `{!AccountTree.selectedRowsKeyValueSemiColonSeparated}` | One string, `id1;id2`, unquoted                                                         |
| **Selected Rows Serialized**                           | `{!AccountTree.selectedRowsSerialized}`                 | The ticked rows as a JSON string, for an action that parses JSON                        |

This flow uses the first two.
{% endstep %}

{% step %}

### Stop when nothing is ticked

1. After the screen, add an **Assignment**. Create a Number variable `SelectedCount` and set it with **Equals Count** `{!AccountTree.selectedRowsKeyValue}`.
2. Add a **Decision** with an outcome `Accounts ticked`: `{!SelectedCount}` **Greater Than** `0`.
3. On the default outcome, add a screen that says "Tick at least one account", then connect it back to the Tree Grid screen.
   {% endstep %}

{% step %}

### Ask for the new account manager

On the `Accounts ticked` path, add a **Screen** with a standard **Lookup** component:

* **API Name**: `New_Owner`
* **Object API Name**: `Account`
* **Field API Name**: `OwnerId`
* **Label**: `New account manager`

The Lookup searches users, the same way the **Account Owner** field does.
{% endstep %}

{% step %}

### Reassign the ticked accounts

1. Add a **Loop** over `{!AccountTree.selectedRows}`, named `Loop_Accounts`.
2. Inside the loop, an **Assignment**: `{!Loop_Accounts.OwnerId}` **Equals** `{!New_Owner.recordId}`, then **Add** `{!Loop_Accounts}` to a new Account record collection variable, `AccountsToUpdate`.
3. After the loop (**After Last Item**), one **Update Records** on `{!AccountsToUpdate}`.

Updating once after the loop keeps the flow to a single database update, however many accounts were ticked.

<figure><img src="https://27923732-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1FUd4apB9YHgCEMUFbVb%2Fuploads%2F38V4VVPLgBXHBTCobmII%2Ftree-grid-work-with-selected-rows-flow.png?alt=media" alt="Flow Builder canvas with the Tree Grid screen, a count check, a new owner screen, two loops and two Update Records elements" width="320"><figcaption><p>The flow after the screen: one update for the accounts, one for their open cases.</p></figcaption></figure>
{% endstep %}

{% step %}

### Move their open cases too

The cases do not sit in the grid, so fetch them from the Ids:

1. Add a **Get Records** on **Case**, with two conditions: **AccountId** **In** `{!AccountTree.selectedRowsKeyValue}`, and **IsClosed** **Equals** `False`. Store all records.
2. Loop over the cases the same way as in the previous step: set `OwnerId` to `{!New_Owner.recordId}`, add each case to a Case record collection, then one **Update Records** after the loop.

**In** takes the text collection as it is. The comma separated string does not work there: it is a single text value.
{% endstep %}

{% step %}

### Save and test

Run the flow with **Debug**. Click **Next** with nothing ticked: the message screen appears, then the grid again. Under Meridian Group, tick two regional companies, Meridian Europe and Meridian Asia Pacific, and the grid reads **2 items selected**. Click **Next**, pick a user in **New account manager** and finish. Both accounts, and every open case on them, now show that user as owner.
{% endstep %}
{% endstepper %}

{% hint style="info" %}
To act on the selection without leaving the screen, send it to a second flow from a header button instead: [Add row and header actions](https://docs.avonnicomponents.com/flow/tutorials/components/tree-grid/add-row-and-header-actions.md) shows how.
{% endhint %}

## Troubleshooting

<details>

<summary>Selected Rows is empty after the screen</summary>

**Selected Rows** returns records only with the **Query** data source. With **Manual** there is no Salesforce record behind a row, so read **Selected Rows Key Field Value** instead. If the source is right, the user ticked nothing, or **Hide Checkbox Column** is on.

</details>

<details>

<summary>The Get Records returns no cases</summary>

Check that the **In** condition reads **Selected Rows Key Field Value** (the collection), not one of the separated strings, and that it filters on **AccountId**, not on **Id**.

</details>

<details>

<summary>The tree mixes accounts, contacts and cases</summary>

In a tree built across several objects, the user can tick rows of different objects. Work from the Ids: run one **Get Records** per object with **Id** **In** **Selected Rows Key Field Value**. Each one returns only the ticked records of its own object, ready for its own update. [Browse accounts, contacts and cases](https://docs.avonnicomponents.com/flow/tutorials/components/tree-grid/browse-accounts-contacts-and-cases.md) builds that kind of tree.

</details>

## Related tutorials

* [Show an account hierarchy](https://docs.avonnicomponents.com/flow/tutorials/components/tree-grid/show-an-account-hierarchy.md): build the grid this flow reads from.
* [Add row and header actions](https://docs.avonnicomponents.com/flow/tutorials/components/tree-grid/add-row-and-header-actions.md): act on one row, or on the ticked rows, without leaving the screen.
* [Tree Grid](https://docs.avonnicomponents.com/flow/flow-components/tree-grid.md): every selection setting and output variable.


---

# 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/flow/tutorials/components/tree-grid/work-with-selected-rows.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.
