> 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/map/draw-a-territory.md).

# Draw a Territory and Save It to the Record

Let a sales manager draw a territory on an Avonni Map in a screen flow, save it as a file on the account, and reopen it later to edit.

A sales manager gives a distributor the exclusive zone south of Market Street. From the distributor's account, she opens a flow, traces the zone around the accounts it should cover, and clicks **Save territory**. The shape is saved as a file on the account, and the flow shows it again the next time she opens it.

<figure><img src="https://27923732-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1FUd4apB9YHgCEMUFbVb%2Fuploads%2F6STxgkUfeevrh5LgFOqT%2Fmaptuto-draw-a-territory-result.png?alt=media" alt="A San Francisco account map with a blue polygon drawn over SoMa around six account pins, a drawing toolbar with polygon, edit and delete tools at the top right, and a Save territory button under the map."><figcaption><p>A territory traced over SoMa, saved to the account with Save territory.</p></figcaption></figure>

## Before you start

* The map uses **Type** **Leaflet**: drawing does not exist on Google Maps. See [Map](/flow/flow-components/map.md).
* On Account, a text field to remember the saved file, for example **Territory File Id** (`Territory_File_Id__c`, Text, 18 characters).
* The users who draw can create Files on the account.

## Steps

{% stepper %}
{% step %}

### Get the account in the flow

1. Create a screen flow with a Text variable `recordId`, **Available for input**.
2. Add a **Get Records** element named `Get_Account`: **Account** where **Account ID** equals `{!recordId}`, first record only.
3. Create a Text formula `TerritoryTitle`: `"Territory - " & {!Get_Account.Name}`.
   {% endstep %}

{% step %}

### Add the map

Add a **Screen** element after **Get\_Account** and drag an **Avonni Map** onto it. Name it `map`.

1. In the **Properties** tab, set **Type** to **Leaflet**.
2. Set **Data Source** to **Query** on **Account**, for example where **Billing City** equals `San Francisco`, so the manager sees the accounts she is enclosing.
3. In **Data Mappings**, map **Title** to Account Name, and under **Location** map **Latitude** to Billing Latitude and **Longitude** to Billing Longitude.
   {% endstep %}

{% step %}

### Turn on drawing

Open the **Draw** group:

1. Turn on **Enable Draw**. A drawing toolbar appears on the map.
2. In **Toolbar**, keep **Polygon**, **Edit** and **Delete**, and remove the others. A territory is an outline, so the manager needs no lines or markers.
3. Set **Toolbar Position** to **Top Right**.
   {% endstep %}

{% step %}

### Save the drawing as a file on the account

Still in **Draw**, turn on **Save as Content Document**. Its settings appear below it.

| Setting                               | Value                                 | Why                                                                                                                          |
| ------------------------------------- | ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| **Content Document Id**               | `{!Get_Account.Territory_File_Id__c}` | Empty the first time, so a new file is created. Filled later, so the saved shape opens again and Save updates that same file |
| **Content Document Title**            | `{!TerritoryTitle}`                   | The file reads "Territory - " and the account name                                                                           |
| **Content Document Linked Entity Id** | `{!recordId}`                         | The file is attached to the account                                                                                          |
| **Save Button Label**                 | `Save territory`                      | The button under the map                                                                                                     |

<figure><img src="https://27923732-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1FUd4apB9YHgCEMUFbVb%2Fuploads%2F8CVgmtooFzYwE2IOURRY%2Fmaptuto-draw-a-territory-builder-draw.png?alt=media" alt="The Draw group of the Map&#x27;s Properties tab: drawing enabled with the polygon, edit and delete tools, Save as Content Document on, the file linked to the record Id and the save button labeled Save territory." width="320"><figcaption><p>Draw settings that save the territory as a file on the account.</p></figcaption></figure>
{% endstep %}

{% step %}

### Store the file Id on the account

After the screen, the Map's **Content Document Id** output holds the saved file.

1. Add a **Decision** after the screen: continue only when `{!map.contentDocumentId}` **Is Null** is `False`. A manager who closes the screen without saving changes nothing.
2. On that path, add an **Update Records** element: **Account** where **Account ID** equals `{!recordId}`, set **Territory File Id** to `{!map.contentDocumentId}`.
   {% endstep %}

{% step %}

### Add the flow to the account page

Activate the flow. In Lightning App Builder, add the **Flow** component to the Account record page, pick the flow, and check **Pass record ID into this variable** for `recordId`.
{% endstep %}

{% step %}

### Save and test

1. Open an account, pick the polygon tool and click around a few accounts on the map. Click the first point again to close the shape.
2. Click **Save territory**. A message confirms "Your content document has been created." The file appears in the account's Files.
3. Click **Next**. The account's **Territory File Id** is filled.
4. Open the flow again from the same account. The territory is back on the map. Move a corner with **Edit** and click **Save territory**: the message now reads "Your content document has been updated."
   {% endstep %}
   {% endstepper %}

{% hint style="info" %}
The file is saved only when the manager clicks the save button. With **Auto Save Content Document** on, the button disappears and each change is written to the file.
{% endhint %}

<details>

<summary>Keep the shape in a field instead of a file</summary>

Every shape drawn is also available, as GeoJSON text, in the Map's **Draw GeoJSON Value** output, file or no file.

1. Leave **Save as Content Document** off and add a Long Text Area field to Account, for example **Territory GeoJSON**.
2. After the screen, an **Update Records** element sets **Territory GeoJSON** to `{!map.drawGeoJsonValue}`.
3. To open the shape again, set the **Draw GeoJSON Value** setting in the **Draw** group to `{!Get_Account.Territory_GeoJSON__c}`. It shows only while **Save as Content Document** is off.

The same output can feed the next screen, for example a second map whose **GeoJSON Value** shows the territory read-only for a final check.

</details>

## Troubleshooting

<details>

<summary>"Error creating your content document" when saving</summary>

**Content Document Linked Entity Id** is empty or is not a record Id at run time, or the user cannot create Files on that record. Check that the flow receives `recordId` from the page.

</details>

<details>

<summary>"Error updating your content document"</summary>

The file in **Territory File Id** was deleted, or the user cannot edit it. Clear the field on the account so the next save creates a new file.

</details>

<details>

<summary>The drawing toolbar is missing</summary>

Check that **Type** is **Leaflet** and **Enable Draw** is on.

</details>

## Related tutorials

* [Drag a Pin to Update a Location](/flow/tutorials/components/map/drag-a-pin-to-update-a-location.md): the other way to save a change made on the map.
* [Add Search and Filters to a Map](/flow/tutorials/components/map/add-search-and-filters.md): show only the accounts that matter while drawing.
* [Plan a Route from the Map](/flow/tutorials/components/map/plan-a-route.md): visit the accounts inside the territory.


---

# 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/map/draw-a-territory.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.
