This blog assumes you have basic understanding of chat agent configurations. So once you have created ‘Embedded Service Deployments’, you have to ‘Chat Code Snippet share it to the customer portal.
Now there is below requirement-
For Available Chat Agents-
- New Contact should not be created.
- Contact should be searched based on combination of first name, last name and email and should be displayed to agent if matches.
- First Name, Last Name and Email should be passed from portal, but it should not be visible to customers.
- Subject should be auto-populated and visible to customer also.
- A picklist field should also be displayed and ‘None’ should be displayed by default.
Solutions:
If you use the ‘Chat code snippet’, contact will be always created if doesn’t match customer’s entry. So in order to stop creation of case, we can create a flag on contact in sfdc and allow case to be created only when this flag is true using validation rule. And since no flag will be passed from customer chat, hence no contact will be created. (Note: There may be other required fields also in sfdc which can be alternative to flag).
Select Name, Email, Subject and Type field from prechat-form as shown below-

Now hide FirstName, LastName and Email when ‘AfterMaximize’ event is fired as shown in below code-
embedded_svc.addEventHandler("afterMaximize", function(data) {
document.querySelector('.FirstName').parentNode.style.display = 'none';
document.querySelector('.LastName').parentNode.style.display = 'none';
document.querySelector('.Email').parentNode.style.display = 'none';
});
Now populate First Name, Last Name, Email and Subject field as shown below-
embedded_svc.settings.prepopulatedPrechatFields = {
FirstName : "SandeepTest",
LastName : "KumarTest",
Email : "sandeep.saini482@gmail.com",
Subject: "Need Assistance Immediately"
};
Note: Type:”” will not work here as this is picklist field and this is limitation of chat agent.
We have a jugaad. We will use java-script to populate it. We want to populate it when afterMaximize event is fired. So we will use following javascript code to populate it-
let typeEle = document.querySelector('.Type');
typeEle.value = '';
//simply populating value as empty is not enough, following code is necessary in order to reflect value in sfdc
typeEle.dispatchEvent(new Event('change',{'bubbles':!0}));
Now the final script would look like-
<pre class="wp-block-syntaxhighlighter-code"><style type='text/css'>
.embeddedServiceHelpButton .helpButton .uiButton {
background-color: #005290;
font-family: "Salesforce Sans", sans-serif;
}
.embeddedServiceHelpButton .helpButton .uiButton:focus {
outline: 1px solid #005290;
}
@font-face {
font-family: 'Salesforce Sans';
src: url('https://c1.sfdcstatic.com/etc/clientlibs/sfdc-aem-master/clientlibs_base/fonts/SalesforceSans-Regular.woff') format('woff'),
url('https://c1.sfdcstatic.com/etc/clientlibs/sfdc-aem-master/clientlibs_base/fonts/SalesforceSans-Regular.ttf') format('truetype');
}
</style>
<a href="https://service.force.com/embeddedservice/5.0/esw.min.js">https://service.force.com/embeddedservice/5.0/esw.min.js</a>
<script type='text/javascript'>
var initESW = function(gslbBaseURL) {
embedded_svc.settings.displayHelpButton = true; //Or false
embedded_svc.settings.language = ''; //For example, enter 'en' or 'en-US'
//embedded_svc.settings.defaultMinimizedText = '...'; //(Defaults to Chat with an Expert)
//embedded_svc.settings.disabledMinimizedText = '...'; //(Defaults to Agent Offline)
//embedded_svc.settings.loadingText = ''; //(Defaults to Loading)
//embedded_svc.settings.storageDomain = 'yourdomain.com'; //(Sets the domain for your deployment so that visitors can navigate subdomains during a chat session)
// Settings for Chat
//embedded_svc.settings.directToButtonRouting = function(prechatFormData) {
// Dynamically changes the button ID based on what the visitor enters in the pre-chat form.
// Returns a valid button ID.
//};
embedded_svc.settings.prepopulatedPrechatFields = {
FirstName : "SandeepTest",
LastName : "KumarTest",
Email : "sandeep.saini482@gmail.com",
Subject: "Need Assistance Immediately"
};
embedded_svc.addEventHandler("afterMaximize", function(data) {
document.querySelector('.FirstName').parentNode.style.display = 'none';
document.querySelector('.LastName').parentNode.style.display = 'none';
document.querySelector('.Email').parentNode.style.display = 'none';
let typeEle = document.querySelector('.Type');
typeEle.value = '';
typeEle.dispatchEvent(new Event('change',{'bubbles':!0}));
});
//embedded_svc.settings.fallbackRouting = []; //An array of button IDs, user IDs, or userId_buttonId
//embedded_svc.settings.offlineSupportMinimizedText = '...'; //(Defaults to Contact Us)
embedded_svc.settings.enabledFeatures = ['LiveAgent'];
embedded_svc.settings.entryFeature = 'LiveAgent';
embedded_svc.init(
'https://poc1com-dev-ed.my.salesforce.com',
'https://pocsites-developer-edition.ap24.force.com/CustomerService',
gslbBaseURL,
'00D5g000002FEOc',
'Chat_Agents',
{
baseLiveAgentContentURL: 'https://c.la2-c1-ukb.salesforceliveagent.com/content',
deploymentId: '5725g00000006Gk',
buttonId: '5735g00000007Ma',
baseLiveAgentURL: 'https://d.la2-c1-ukb.salesforceliveagent.com/chat',
eswLiveAgentDevName: 'Chat_Agents',
isOfflineSupportEnabled: true
}
);
};
if (!window.embedded_svc) {
var s = document.createElement('script');
s.setAttribute('src', 'https://poc1com-dev-ed.my.salesforce.com/embeddedservice/5.0/esw.min.js');
s.onload = function() {
initESW(null);
};
document.body.appendChild(s);
} else {
initESW('https://service.force.com');
}
</script></pre>
Demo: