Context Events in Enhanced Web Chat v2
This method of passing context events is only available in Enhanced Web Chat v2. For more information, see Enhanced Chat in Salesforce Help.
The optional utilAPI method enables you to pass information like the current page or a recent search to the agent. See the reference for setSessionContext.
The input parameter to setSessionContext must follow the exact schema shown in the example. Valid variables are currentPage and search. Both variables are optional.
Call embeddedservice_bootstrap.utilAPI.setSessionContext only after onEmbeddedMessagingReady fires; don't call it in initEmbeddedMessaging.
You can set context variables and send a user utterance in a single call with sendTextMessage. Setting variables and sending a message in a single call makes sure that the context is immediately available for the agent.
Before you can access context variables in new agents, manually create them in Agentforce Builder. See Create Variables In Agentforce Builder.
For the currentPage and search variables supported by ECv2, create four variables with these settings.
Enable API write access for each variable.
| Name | API Name | Data Type | List Item Type |
|---|---|---|---|
currentPage | currentPage | String | N/A |
search_result | search_result | String | N/A |
search_filters | search_filters | List | String |
search_facets | search_facets | List | String |
You can access the context variables in Agent Script using ${!@variables.<apiName>}. For example, use {!@variables.currentPage} to reference the current page in an action input. For more information on agent variables, see Agent Script Reference: Variables.
You can use event handlers in both setSessionContext() and sendTextMessage() methods to perform actions after the operation completes.
thenevent handler— If the context is set successfully, you can use this handler to, for example, update the UI or record analytics.catchevent handler— If there's an error setting the context, you can use this handler to, for example, try again or show an error message.finallyevent handler— This handler runs after the attempt to set context completes, whether it succeeds or fails. Use this handler to run any clean-up actions.