You are here:
Use Pre-Chat API to Pass Customer Information to the Service Rep in an External Website
To provide service reps with customer information that helps move an interaction forward, send end-user data to the service console via the pre-chat API.
Required Editions
| View supported editions. | |
This article applies to:
|
Enhanced Web Chat channels |
This article doesn’t apply to:
|
Enhanced In-App Chat, Enhanced WhatsApp, Standard and Enhanced Facebook Messenger, Standard and Enhanced SMS, Enhanced Apple Messages for Business, Enhanced LINE, and Bring Your Own Channel |
This recipe combines common use cases as examples of data that you can send to service reps using the pre-chat API. First, pass the end user’s login ID to the service rep so that verification is done. Then, to prevent loss on large orders, we pass the customer’s shopping cart value to the rep. To accomplish these goals, create custom hidden pre-chat fields that route the data to Salesforce custom fields via a flow. Then edit the code snippet, telling it to auto-populate the hidden pre-chat fields.
Before building this recipe, consider these caveats.
- You can reference fields from the Messaging Session object or the Messaging User object (because it's linked to Messaging Session) in an auto-response formula template for Start Conversation, End Conversation, and Inactive Conversation. You can’t reference fields from the Messaging Session or Messaging User objects in Conversation
- Salesforce custom object fields are different from pre-chat custom fields. This recipe references both types of fields. Learn more about the relationship and differences between these two types of fields.
- Create custom fields on Salesforce objects. In this example, create a Customer ID field and a Shopping Cart Value field on the messaging session or messaging user objects.
- If your Service Console doesn’t show the object where you stored the fields, edit the layout to give service reps easy access. In this example, we edit the Service Console to give easy access to the Messaging Session and Messaging User objects.
- To capture the data that gets sent to the Salesforce custom fields from step 1, create hidden pre-chat fields. In this example, create custom parameters called Customer ID and Shopping Cart Value in Messaging Settings for the channel, and then select them as hidden pre-chat fields in Embedded Services Pre-Chat Form settings.
- Map your custom pre-chat fields to your Salesforce custom fields, and then populate the Salesforce fields using a flow. In this example, we map the pre-chat field called Customer ID to the Salesforce field called Customer ID. Then we map the pre-chat field called Shopping Cart Value to the Salesforce field called Shopping Cart Value. With the flexibility of flows, you can combine or slice data or populate any other objects, such as Case or Contact, as needed.
-
Update your embedded messaging code snippet, instructing
it to auto-populate the pre-chat fields that you created in step 2. The code in
this example uses the Pre-Chat API to send Customer ID and Shopping Cart Value to your
pre-chat fields automatically. Before adding to your code snippet, consider these
caveats.
- The first section of the code contains an example of the standard code snippet for your Embedded Service deployment. Yours looks slightly different due to your unique deployment ID and name.
-
To complete the recipe, add the code that starts with the comment line
Passing Web Context to Prechat - BEGIN. - The first section assigns static values to these variables, but you can change them to reflect how you get these values. The next section populates the visible pre-chat fields. The final section populates hidden pre-chat fields.
- We use different code settings in assigning the values. For example, the first and last names are locked and not editable by users.
<script type='text/javascript'> function initEmbeddedMessaging() { try { embeddedservice_bootstrap.settings.language = 'en_US'; // For example, enter 'en' or 'en-US' embeddedservice_bootstrap.init( '00DDn00000DuiyZ', 'Cookbook_MIAW_Deployment', 'https://csg-gr-cdo-summer23.my.site.com/ESWCookbookMIAWDeployme1710815189835', { scrt2URL: 'https://csg-gr-cdo-summer23.my.salesforce-scrt.com' } ); } catch (err) { console.error('Error loading Embedded Messaging: ', err); } }; //Passing Web Context to Prechat - BEGIN var sFirstName = "John"; var sLastName = "Doe"; var sSubject = "Test Subject"; var sCustomerID = "12345678"; var sShoppingCartValue = "1000"; window.addEventListener("onEmbeddedMessagingReady", e => { //log event console.log("onEmbeddedMessagingReady event triggered"); //set visible prechat field embeddedservice_bootstrap.prechatAPI.setVisiblePrechatFields({ // List the pre-chat field names with the value and whether // it's editable in the pre-chat form. "_firstName": { "value": sFirstName, "isEditableByEndUser": false }, "_lastName": { "value": sLastName, "isEditableByEndUser": false }, "_subject": { "value": "test subject", "isEditableByEndUser": true } }); //Set invisible prechat field embeddedservice_bootstrap.prechatAPI.setHiddenPrechatFields({ "Shopping_Cart_Value" : sShoppingCartValue, "Customer_ID" : sCustomerID }); }); //Passing Web Context to Prechat - END </script>



