> 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/data-table/apply-badge-color-coding.md).

# Apply Badge Color-Coding

Color the Stage column as a badge that follows each deal's stage, driven by one formula field, so a manager spots late and closed deals in one pass.

A sales manager reviewing the pipeline wants to see in one pass which deals are still early, which are in negotiation, and which are won or lost. Here the Stage column becomes a badge whose color follows the stage: gray for early deals, blue in discovery, orange near the close, green for won, red for lost.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FFU9YT48gZfcOpRAKwRVI%2Fdata-table-tutorial-apply-badge-color-coding-result.png?alt=media" alt="Data Table of opportunities where each Stage value is a badge colored by stage: gray, blue, orange, green and red."><figcaption><p>Each Stage badge takes its color from the Stage Color formula field.</p></figcaption></figure>

## Before you start

* A [Data Table](https://docs.avonnicomponents.com/dynamic-components/components/data-table.md) on Opportunity with a [Query](https://docs.avonnicomponents.com/dynamic-components/component-builder/data-sources/query.md) data source, showing at least the **Opportunity Name**, **Stage** and **Amount** columns.
* Permission to create a formula field on Opportunity (Customize Application).

## Steps

{% stepper %}
{% step %}

### Create the Stage Color formula field

The formula returns one hex color per stage. The table reads that value row by row.

1. In Setup, open **Object Manager**, then **Opportunity**, then **Fields & Relationships**, and click **New**.
2. Choose **Formula**, name the field `Stage Color`, and set the return type to **Text**.
3. Paste this formula, then save:

```
CASE(StageName,
  "Prospecting", "#706E6B",
  "Qualification", "#706E6B",
  "Needs Analysis", "#0176D3",
  "Value Proposition", "#0176D3",
  "Id. Decision Makers", "#0176D3",
  "Perception Analysis", "#0176D3",
  "Proposal/Price Quote", "#DD7A01",
  "Negotiation/Review", "#DD7A01",
  "Closed Won", "#2E844A",
  "Closed Lost", "#BA0517",
  "#706E6B"
)
```

| Stages                                                                      | Hex code  | Color  |
| --------------------------------------------------------------------------- | --------- | ------ |
| Prospecting, Qualification, any other value                                 | `#706E6B` | Gray   |
| Needs Analysis, Value Proposition, Id. Decision Makers, Perception Analysis | `#0176D3` | Blue   |
| Proposal/Price Quote, Negotiation/Review                                    | `#DD7A01` | Orange |
| Closed Won                                                                  | `#2E844A` | Green  |
| Closed Lost                                                                 | `#BA0517` | Red    |

{% hint style="info" %}
`CASE` compares the stage's API value. If your org renamed a stage, use the API value shown on the StageName picklist, not the label.
{% endhint %}
{% endstep %}

{% step %}

### Set the Stage column to Badge

1. Select the Data Table on the canvas.
2. In the Properties panel, open the **Data Mappings** section and click the Stage column in the **Columns** list.
3. Set **Type** to **Badge**.
   {% endstep %}

{% step %}

### Bind the badge color to the formula field

1. Under **Type Attributes**, find **Background Color**.
2. Turn on the **Field Name** toggle at the top right of the property.
3. Select `Stage Color` in the field list.
4. Leave the toggle of **Text Color** off and enter `#FFFFFF`, so the stage name stays readable on every color.

You do not need a column for `Stage Color`. The table fetches a bound field on its own.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FwnssknXh2lzYBsVTBnkg%2Fdata-table-tutorial-apply-badge-color-coding-panel.png?alt=media" alt="Stage column settings with Type set to Badge, Background Color bound to the Stage Color field and Text Color set to white." width="320"><figcaption><p>Background Color is bound to the formula field with the Field Name toggle.</p></figcaption></figure>
{% endstep %}

{% step %}

### Save and test

Save the component and open the page that holds it. Each Stage cell now shows a badge: a Prospecting deal in gray, a Negotiation/Review deal in orange, a Closed Won deal in green. Change a deal's stage and reload: its badge changes color with it.
{% endstep %}
{% endstepper %}

## Use the built-in variants instead of hex codes

If you would rather match the standard Lightning colors than pick your own, bind **Variant** instead of **Background Color**. Variant badges are lighter than hex badges: a pale tint with dark text, the way standard Lightning badges look. The field must return one of these values:

| Variant label | Value the formula returns |
| ------------- | ------------------------- |
| Base          | `base`                    |
| Brand         | `brand`                   |
| Inverse       | `inverse`                 |
| Alt Inverse   | `alt-inverse`             |
| Success       | `success`                 |
| Info          | `info`                    |
| Error         | `error`                   |
| Warning       | `warning`                 |
| Offline       | `offline`                 |

Create a second text formula field, `Stage Variant`, with the same five groups:

```
CASE(StageName,
  "Needs Analysis", "info",
  "Value Proposition", "info",
  "Id. Decision Makers", "info",
  "Perception Analysis", "info",
  "Proposal/Price Quote", "warning",
  "Negotiation/Review", "warning",
  "Closed Won", "success",
  "Closed Lost", "error",
  "base"
)
```

On the Stage column, turn on the **Field Name** toggle of **Variant** and select `Stage Variant`. Leave **Background Color** and **Text Color** empty: a background color overrides the variant's color.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FlT5LoZyN6QZLueIrIkXd%2Fdata-table-tutorial-apply-badge-color-coding-variant.png?alt=media" alt="Stage badges in the standard Lightning variants: neutral, info, warning, success and error." width="560"><figcaption><p>The same stages with the Variant bound to a field instead of a hex color.</p></figcaption></figure>

## Badge, dot or cell background

| Pick                                                                                                                                                                       | When                                                                                          |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| Badge (this page)                                                                                                                                                          | The value must stay readable and stand out, in a column of normal width.                      |
| Status dot ([Add Status Indicators](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/data-table/add-status-indicators.md))                        | Space is tight, and the stage name already appears in another column.                         |
| Cell background ([Apply Conditional Cell Styling](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/data-table/apply-conditional-cell-styling.md)) | You want the whole cell tinted, or to style a column that is not a badge (an amount, a date). |

All three tutorials use the same stage groups, so you can mix them on one table.

## Troubleshooting

<details>

<summary>Every badge has the same gray color</summary>

The formula returned its default value for every row. Check that each stage in the `CASE` is spelled exactly like the picklist API value, including the period in `Id. Decision Makers`.

</details>

<details>

<summary>The badge ignores the variant</summary>

**Background Color** or **Text Color** is still set. Clear both so the variant's own colors apply.

</details>

## Related tutorials

* [Add Status Indicators](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/data-table/add-status-indicators.md): the same colors as a compact dot.
* [Apply Conditional Cell Styling](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/data-table/apply-conditional-cell-styling.md): tint the whole cell with SLDS classes.
* [Configuring Data Grouping](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/data-table/configuring-data-grouping.md): group the colored rows by stage.


---

# 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/data-table/apply-badge-color-coding.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.
