---
schema_version: "1"
id: 9ea9ddaf9f00f1a2a73d66e3769a04f2c584ff7d6b8c1944a657076d06346ef6
title: Use Context Variables in Enhanced Chat Conversations
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/ai/264-0-0/agent_enhanced_chat_v2_context_variables.md
canonical_url: https://help.salesforce.com/s/articleView?id=ai.agent_enhanced_chat_v2_context_variables.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: a8dc8c80de1707b3b3bb0b1364644608b9b19a21cd283c080a02187338369996
description: Personalize agent conversations in Enhanced Chat with context variables. Context variables let your agent use Messaging Session record fields as action inputs.
products:
  - Agentforce
  - Cross Cloud
release: "264"
---
# Use Context Variables in Enhanced Chat Conversations

Personalize agent conversations in Enhanced Chat with context variables. Context variables let your agent use Messaging Session record fields as action inputs.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition">
        <colgroup>
          <col>
        </colgroup>
        <tbody>
          <tr class="">
            <td class="slds-cell-wrap" style="vertical-align:top;">Available in: Lightning Experience</td>

          </tr>
          <tr class="">
            <td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Enterprise</strong>, <strong>Performance</strong>, <strong>Unlimited</strong>, and
              <strong>Developer</strong> Editions. <a href="https://help.salesforce.com/s/articleView?id=ai.agent_setup_explore_types.htm&amp;release=264.0.0&amp;type=5" title="Learn about agent types and default templates for specific clouds and common use cases.">Required
                add-on licenses vary by agent type.</a>
            </td>

          </tr>
          <tr class="">
            <td class="slds-cell-wrap" style="vertical-align:top;">
              <p><span style="vertical-align:text-bottom;display:inline-block;"><img style="margin-bottom:0;" src="https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ai-generative_ai-264-0-0-production-enus/f8ef6498-d7b2-44e3-9b34-ffd5a850aae9/resources-global/_corporate/images/checkmark.png" alt="Check mark"></span>This article applies to: Agentforce Service agents</p>
              <p><span style="vertical-align:text-bottom;display:inline-block;"><img style="margin-bottom:0;" src="https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ai-generative_ai-264-0-0-production-enus/f8ef6498-d7b2-44e3-9b34-ffd5a850aae9/resources-global/_corporate/images/crossmark.png" alt="Cross mark"></span>This article doesn't apply to: Agentforce Employee agents</p>
              <p style="margin-bottom:0;">To learn about channel support for other agent types, see the documentation for
                that type.</p>
            </td>

          </tr>
        </tbody>
      </table> <table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="col" class="slds-cell-wrap" style="text-align:center;vertical-align:top;" id="d218241e105">User Permissions Needed</th>
            <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d218241e108">&nbsp;</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d218241e105 ">To build and manage Service agents:</td>

            <td data-label="" class="slds-cell-wrap" style="vertical-align:top;" headers="d218241e108 ">
              <p>Manage Agentforce Service Agents AND Manage AI Agents</p>
              <p>OR</p>
              <p style="margin-bottom:0;">Customize Application</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d218241e105 ">To build and manage Employee agents:</td>

            <td data-label="" class="slds-cell-wrap" style="vertical-align:top;" headers="d218241e108 ">
              <p>Manage AI Agents and Manage Agentforce Employee Agents</p>
              <p dir="ltr">OR</p>
              <p style="margin-bottom:0;" dir="ltr">Customize Application</p>
            </td>

          </tr>
        </tbody>
      </table>

Before you begin:

- [Connect your agent to Enhanced Chat v2](https://help.salesforce.com/s/articleView?id=ai.service_agent_deploy_enhanced_chat_v2.htm&release=264.0.0&type=5 "To connect an agent to Enhanced Chat v2 in the new Agentforce Builder, set up the Enhanced Chat v2 connection. Agentforce Builder creates the channel and Enhanced Chat v2 deployment for you. Set up Agentforce Voice for Enhanced Chat v2 so that users can switch between speech and text when they chat with an Agentforce Service agent.") or [Enhanced Chat v1](https://help.salesforce.com/s/articleView?id=ai.agent_deploy_enhanced_chat_v1.htm&release=264.0.0&type=5 "Connect an Agentforce Service agent to Enhanced Chat v1 or an Agentforce Employee agent to your Experience Cloud site with Enhanced Web Chat v1.").
- [Deactivate your agent](https://help.salesforce.com/s/articleView?id=ai.copilot_setup_activate_deactivate.htm&release=264.0.0&type=5 "Activate your agent to make it available to your customers or employees. When an agent is deployed to one or more channels, including the Agentforce panel, activating the agent makes it immediately available to your users.").

By default, Service agents include a Messaging Session variable that’s mapped to a MessagingSession record. Agents can use the information stored in the Messaging Session ID, Messaging User ID, and other included fields. They can also update the Messaging Session record as they chat with customers.

Context variables let you map Messaging Session object fields to messaging channels. For example, to save your agent from having to ask for common information conversationally, you can configure your agent to reference any Messaging Session object field, including the ones populated via a pre-chat form. This data can even be used as an input for actions. We don't recommend using context variables for authentication.

You can’t test context variable use cases in Agentforce Builder because context variables require a Messaging Session record. To test context variables, chat with your agent on a connected channel.

1. [Add context variables to your agent in Agentforce Builder](https://help.salesforce.com/s/articleView?id=ai.agent_builder_variables_create.htm&release=264.0.0&type=5 "Use variables to store and reuse values in your agent’s reasoning and control how an agent makes decisions. For example, when you assign a variable to an action input, your agent can use only the assigned variable to fill the value of the input."). To make more Messaging Session fields available as context variables, [add custom fields to the Messaging Session object](https://help.salesforce.com/s/articleView?id=platform.adding_fields.htm&type=5&language=en_US).
2. You can also add field values to the Messaging Session record by using a pre-chat form, your inbound Omni-Channel flow, or Pre-Chat API.
    1. To use the pre-chat form, [create and customize an Enhanced Chat pre-chat form](https://help.salesforce.com/s/articleView?id=service.miaw_custom_prechat_3.htm&language=en_US). You can add standard and custom fields to the pre-chat form. Then [map your pre-chat values to your messaging channel and update your inbound Omni-Channel flow](https://help.salesforce.com/s/articleView?id=service.miaw_map_messaging_2.htm&language=en_US).
    2. To use your inbound Omni-Channel flow, customize your inbound Omni-Channel flow so that it passes information from previous messaging sessions to your agent. See [Persist Pre-Chat Inputs Across Messaging Sessions](https://help.salesforce.com/s/articleView?id=service.miaw_handle_sessions.htm&language=en_US).
    3. To use Pre-Chat API, send user data that isn’t visible in the pre-chat form, known as hidden pre-chat fields, to your agent. See [Enhanced Web Chat Hidden Pre-Chat API](https://developer.salesforce.com/docs/service/messaging-web/guide/pre-chat.html), [iOS Hidden Pre-Chat API](https://developer.salesforce.com/docs/service/messaging-in-app/guide/ios-pre-chat.html), and [Android Hidden Pre-Chat API](https://developer.salesforce.com/docs/service/messaging-in-app/guide/android-pre-chat.html).
3. To use a context variable during agent conversations, assign it to an action input or use it in subagent instructions. See [Create Variables in Agentforce Builder](https://help.salesforce.com/s/articleView?id=ai.agent_builder_variables_create.htm&release=264.0.0&type=5 "Use variables to store and reuse values in your agent’s reasoning and control how an agent makes decisions. For example, when you assign a variable to an action input, your agent can use only the assigned variable to fill the value of the input.").

To share context from your website with your agent during an Enhanced Chat v2 conversation, such as the current page or search, pass context events with the Enhanced Web Chat API. See [Context Events in Enhanced Web Chat v2](https://developer.salesforce.com/docs/service/messaging-web/guide/context-events.html).