> 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/chat/refresh-chat-across-sessions.md).

# Refresh Chat across sessions

Refresh a case conversation in another agent's open Lightning session when Salesforce publishes a Platform Event for the same case.

One agent posts a case update while a colleague already has the case open. Use a Salesforce Platform Event to signal that a message has changed. Avonni Chat listens for the case ID and reloads the matching conversation.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FOE8HcKxLG717px7Q5Ff3%2Fchat-refresh-chat-across-sessions-compact.jpg?alt=media" alt="Open Case chat with the replacement cable update in the receiving Lightning tab." width="386"><figcaption><p>The replacement cable update appears in the receiving tab.</p></figcaption></figure>

[Open the wider Salesforce capture](https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FcfDhqIAiGkJP1C2s7AMx%2Fchat-refresh-chat-across-sessions-result.jpg?alt=media)

## Before you start

* Complete [Keep replies and pins after reload](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/chat/persist-replies-and-pins.md). Reuse its activated `Escalation Chat`, `Case_Message__c` object and two test cases.
* Use a sandbox where you can create a Platform Event and activate a record-triggered Flow. Open two desktop Lightning views. You can start with two tabs. To verify your team setup, use separate browser profiles signed in as users who can access the test cases and subscribe to the event.

## Set up case-specific refresh

{% stepper %}
{% step %}

### Create an event for a case change

1. In Salesforce Setup, create a Platform Event named `Case Message Changed` (`Case_Message_Changed__e`).
2. Set its publish behavior to **Publish After Commit** so Chat can read the change when it receives the event.
3. Add a text field named `Case Record ID` (`Case_Record_Id__c`) with length `18`.

The event carries a case identifier. The message text stays on the Case Message record. See [Real-Time Data](https://docs.avonnicomponents.com/dynamic-components/component-builder/real-time-data.md) for the Salesforce setup screens.
{% endstep %}

{% step %}

### Publish the event when a message changes

1. Create a Salesforce record-triggered Flow on `Case_Message__c`. Run it when a record is created or updated and optimize it for **Actions and Related Records**.
2. Add **Create Records** to create one `Case_Message_Changed__e` event. Set `Case_Record_Id__c` to `{!$Record.Case__c}`.
3. Save and activate the Flow. With create and update both enabled, a new message and a change to a saved pin can each trigger a refresh.
   {% endstep %}

{% step %}

### Subscribe the Chat query

1. Open `Escalation Chat` in the Component Builder and select Chat. Under the query's **Advanced Options**, open **Query Refresh Emp**.
2. Set **Channel Name** to `Case_Message_Changed__e` and **Key Field Name** to `Case_Record_Id__c`. Enter `{!$Component.RecordId}` directly in **Key Field Value**. Chat compares the event value with that page record ID.
3. Keep the existing case filter and the publish interaction's **On Success** refresh. The filter controls the records shown. The publish interaction refreshes the sender's view.
4. Click **Save** and choose **Save and Activate** for the active version. Open the test Case page from the test app before starting the check in two views.

<figure><img src="https://2532358799-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FODPvvv7Cx9Z9RECLn3oV%2Fuploads%2FnJrWlkseOYRzAJuyd4iy%2Fchat-refresh-chat-across-sessions-subscription.jpg?alt=media" alt="Query Refresh Emp configured with the Case Message Changed event and its Case Record ID field." width="320"><figcaption><p>The Case ID in the event determines which open chat refreshes.</p></figcaption></figure>
{% endstep %}

{% step %}

### Check the matching case in two views

1. Open `Label printer offline` in both views. Wait for both threads to finish loading.
2. From session A, send `The replacement cable is installed.` Leave session B's page open. The update should appear in session B without a reload. Check the saved Case Message record belongs to `Label printer offline`.
3. Pin the update in session A. Confirm session B adds it to its pinned banner and that the source record's **Pinned** field is checked.
4. In session A, switch to `Packing station slow` and post `Checking packing station settings.` Check that the new record points to the second case and that the message stays out of the first case's thread in session B.
5. Switch session B to the second case and confirm the message appears there. Send one more update from session A and confirm it appears in session B without a reload.
   {% endstep %}
   {% endstepper %}

## Troubleshooting

<details>

<summary>The sender sees the message, but the other viewer has to reload</summary>

Check that the Flow is active and publishes the Case lookup, and that **Channel Name** and **Key Field Name** match the event's API names. Check that the receiving viewer can subscribe and can read the saved message. The sender's **Refresh Query** can succeed independently of the event.

</details>

<details>

<summary>New messages arrive, but pin changes do not</summary>

Check that the Flow runs on updates as well as creates. Verify that the pin interaction actually changed the message's Salesforce record.

</details>

## Related tutorials

* [Keep replies and pins after reload](https://docs.avonnicomponents.com/dynamic-components/tutorials/components/chat/persist-replies-and-pins.md) configures the message records, reply mapping, and pin interactions.
* [Real-Time Data](https://docs.avonnicomponents.com/dynamic-components/component-builder/real-time-data.md) shows the general Platform Event setup.
* [Chat reference](https://docs.avonnicomponents.com/dynamic-components/components/chat.md) lists the component's query and publisher settings.


---

# 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/chat/refresh-chat-across-sessions.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.
