var initESW = function(gslbBaseURL, options) {
var queueConfig = {
cs: '5733h000000Qd6t',
ol: '5733h000000Qd73',
test: '5736S0000002gPl',
ns: '5736S000000RsRr',
candid: '573PH0000004IMX'
};
var queueRoutingConfig = {
field: 'Reason',
reasonButtonRoutes: [
{
value: 'Subscription Product Access/Billing',
queue: 'cs'
},
{
value: 'Update Profile/Seals',
queue: 'ns'
},
{
value: 'Claim Profile/Get Listed',
queue: 'ns'
},
{
value: 'Subscription Product Options',
queue: 'cs'
},
{
value: 'Help Me Use Foundation Directory/GTI',
queue: 'cs'
},
{
value: 'Help Me Use Foundation Maps',
queue: 'cs'
},
{
value: 'Help Me Use GuideStar Pro/Charity Check',
queue: 'cs'
},
{
value: 'Report a Data Error',
queue: 'cs'
},
{
value: 'Training/Events',
queue: 'ol'
},
{
value: 'Finding Grants/Fundraising',
queue: 'ol'
},
{
value: 'Starting/Managing a Nonprofit',
queue: 'ol'
},
{
value: 'Other',
queue: 'ol'
}
//For any other reasons, fallback to default queue (buttonId)
]
};
var candidQueueRoutingConfig = {
field: 'Candid_Case_Reason__c',
reasonButtonRoutes: [
{
value: 'Update my Candid nonprofit profile or Seal',
queue: 'ns'
},
{
value: 'Create my nonprofit profile on Candid',
queue: 'ns'
},
{
value: 'Subscription billing',
queue: 'cs'
},
{
value: 'Subscription plans and prices',
queue: 'cs'
},
{
value: 'Help with search filters and results',
queue: 'cs'
},
{
value: 'Report a data issue',
queue: 'cs'
},
{
value: 'Find or register for Candid training',
queue: 'ol'
},
{
value: 'Finding grants/Fundraising',
queue: 'ol'
},
{
value: 'Starting or running a nonprofit',
queue: 'ol'
},
{
value: 'Other',
queue: 'ol'
}
//For any other reasons, fallback to default queue (buttonId)
]
};
var config = {
defaultAccountId: '0013h00000FvRclAAF',
cs: {
init1: 'https://candid.my.salesforce.com',
init2: 'https://help.candid.org/',
initId: '00D3h000001QSAr',
snapIn: 'Customer_Success_Chat',
baseLiveAgentContentURL: 'https://c.la4-c1-ia4.salesforceliveagent.com/content',
deploymentId: '5723h000000QODx',
buttonId: queueConfig.cs,
baseLiveAgentUrl: 'https://d.la4-c1-ia4.salesforceliveagent.com/chat',
eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent04I0S00000000ApUAI_173102c4c42',
isOfflineSupportEnabled: false,
preform: true,
webToCaseUrl: 'https://support.candid.org/case.html?mode=popup&type=all',
webToCaseSettings: {
height: 680,
width: 1000
},
routing: queueRoutingConfig
},
cs_noform: {
init1: 'https://candid.my.salesforce.com',
init2: 'https://help.candid.org/',
initId: '00D3h000001QSAr',
snapIn: 'Customer_Success_No_Preform_Support_Chat',
baseLiveAgentContentURL: 'https://c.la4-c1-ia4.salesforceliveagent.com/content',
deploymentId: '5723h000000QODx',
buttonId: queueConfig.cs,
baseLiveAgentUrl: 'https://d.la4-c1-ia4.salesforceliveagent.com/chat',
eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent04I0S00000000AuUAI_1732a286160',
isOfflineSupportEnabled: false,
preform: false,
webToCaseUrl: 'https://support.candid.org/case.html?mode=popup&type=all',
webToCaseSettings: {
height: 680,
width: 1000
},
routing: queueRoutingConfig
},
ol: {
init1: 'https://candid.my.salesforce.com',
init2: 'https://help.candid.org/',
initId: '00D3h000001QSAr',
snapIn: 'Online_Librarian_Service_Chat',
baseLiveAgentContentURL: 'https://c.la4-c1-ia4.salesforceliveagent.com/content',
deploymentId: '5723h000000QODx',
buttonId: queueConfig.ol,
baseLiveAgentUrl: 'https://d.la4-c1-ia4.salesforceliveagent.com/chat',
eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent04I0S00000000AvUAI_1732a2d4495',
isOfflineSupportEnabled: false,
preform: true,
webToCaseUrl: 'https://support.candid.org/case.html?mode=popup',
webToCaseSettings: {
height: 680,
width: 1000
},
routing: queueRoutingConfig
},
ns: {
init1: 'https://candid.my.salesforce.com',
init2: 'https://help.candid.org/',
initId: '00D3h000001QSAr',
snapIn: 'Nonprofit_Support_Chat',
baseLiveAgentContentURL: 'https://c.la4-c1-ia4.salesforceliveagent.com/content',
deploymentId: '5723h000000QODx',
buttonId: queueConfig.ns,
baseLiveAgentUrl: 'https://d.la4-c1-ia4.salesforceliveagent.com/chat',
eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent04I6S0000004CFWUA2_183468316a4',
isOfflineSupportEnabled: false,
preform: true,
webToCaseUrl: 'https://support.candid.org/case.html?mode=popup&type=all',
webToCaseSettings: {
height: 680,
width: 1000
},
routing: queueRoutingConfig
},
test: {
init1: 'https://candid.my.salesforce.com',
init2: 'https://help.candid.org/',
initId: '00D3h000001QSAr',
snapIn: 'Test_Chat',
baseLiveAgentContentURL: 'https://c.la4-c1-ia4.salesforceliveagent.com/content',
deploymentId: '5723h000000QODx',
buttonId: queueConfig.test,
baseLiveAgentUrl: 'https://d.la4-c1-ia4.salesforceliveagent.com/chat',
eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent04I6S000000fxuaUAA_17d83aa7fa2',
isOfflineSupportEnabled: false,
preform: true,
webToCaseUrl: 'https://support.candid.org/case.html?mode=popup',
webToCaseSettings: {
height: 680,
width: 1000
},
routing: queueRoutingConfig
},
candid: {
init1: 'https://candid.my.salesforce.com',
init2: 'https://help.candid.org/',
initId: '00D3h000001QSAr',
snapIn: 'Candid_Chat',
baseLiveAgentContentURL: 'https://c.la11-core1.sfdc-lywfpd.salesforceliveagent.com/content',
deploymentId: '5723h000000QODx',
buttonId: queueConfig.candid,
baseLiveAgentUrl: 'https://d.la11-core1.sfdc-lywfpd.salesforceliveagent.com/chat',
eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent04IPH0000002odR2AQ_193970d8bef',
isOfflineSupportEnabled: false,
preform: true,
webToCaseUrl: 'https://support.candid.org/case.html?type=candid&mode=popup',
webToCaseSettings: {
height: 680,
width: 1000
},
routing: candidQueueRoutingConfig
}
}
if(!options)
options = {}
if(!options.type)
options.type = 'ol';
var chatConfig = config[options.type];
embedded_svc.settings.displayHelpButton = true; //Or false
embedded_svc.settings.language = ''; //For example, enter 'en' or 'en-US'
embedded_svc.settings.defaultMinimizedText = 'Chat with Candid'; //(Defaults to Chat with an Expert)
embedded_svc.settings.disabledMinimizedText = 'Candid Agent offline'; //(Defaults to Agent Offline)
//Handle dynamic routing to buttons based on reason, if the current provided options
//or config specifes routing options
var routeConfig = options.routing || chatConfig.routing;
if(routeConfig && routeConfig.reasonButtonRoutes.length > 0) {
embedded_svc.settings.directToButtonRouting = function(prechatFormData) {
var routeFieldData = prechatFormData.filter(formDataItem => formDataItem.name === routeConfig.field);
if(routeFieldData.length > 0) {
var dynamicRoute = routeConfig.reasonButtonRoutes.filter(reasonButtonRoute =>
routeFieldData[0].value &&
routeFieldData[0].value.toLowerCase() == reasonButtonRoute.value.toLowerCase()
);
if(dynamicRoute.length > 0) {
if(queueConfig[dynamicRoute[0].queue])
return queueConfig[dynamicRoute[0].queue];
};
}
return chatConfig.buttonId;
}
}
if(options.settings) {
if(options.settings.offlineEnableWebToCase)
embedded_svc.settings.disabledMinimizedText = 'Contact Candid';
if(options.settings.onlineButtonText)
embedded_svc.settings.defaultMinimizedText = options.settings.onlineButtonText;
if(options.settings.offlineButtonText)
embedded_svc.settings.disabledMinimizedText = options.settings.offlineButtonText;
if(options.settings.loadingText)
embedded_svc.settings.loadingText = options.settings.loadingText; //(Defaults to Loading)
//If option for hiding the button is true, dynamically add css to hide it
if(options.settings.hideButton) {
var cssText = '.embeddedServiceHelpButton { display: none !important; }';
var style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
// IE
style.styleSheet.cssText = cssText;
} else {
// Other browsers
style.innerHTML = cssText;
}
var documentHeadTags = document.getElementsByTagName('head');
if(documentHeadTags.length > 0)
documentHeadTags[0].appendChild(style);
}
//Show a close button to allow users to close out the chat button
if(options.settings.enableHideButton) {
var isFirstLoad = true;
embedded_svc._onButtonStatusChange1 = embedded_svc.onButtonStatusChange;
embedded_svc.onButtonStatusChange = function() {
this._onButtonStatusChange1();
//Check if chat snap-in is loaded
if(this.isIframeReady) {
if(options.settings.enableHideButton && isFirstLoad) {
if(document.cookie && document.cookie.indexOf('chatClosed=1') >= 0) {
options.hideButton(true);
}
//Add UI element for close
var closeButton = $('');
$('.embeddedServiceHelpButton .helpButton').append(closeButton);
closeButton = document.querySelector('.embeddedServiceHelpButton a.closeButton');
//Hook logic to newly added close UI element for hiding/closing the chat button
closeButton.onclick = function(event) {
event.stopPropagation();
console.log('close clicked');
options.hideButton(true);
return false;
}
}
isFirstLoad = false;
}
}
}
var webToCaseUrl = chatConfig.webToCaseUrl;
if(options.settings.webToCaseUrl)
webToCaseUrl = options.settings.webToCaseUrl;
//Ensure we always have a ? in the url, as later we tack on querystring parameters with & assuming there is already a ? defined
if(webToCaseUrl && webToCaseUrl.indexOf('?') < 0)
webToCaseUrl += '?';
var openWebToCase = function() {
//Open web to case form in new popup window. Note context parameter is appended url with window.location.href to deal with cross-origin location limitations
return window.open(webToCaseUrl + '&context=' + window.location.href, 'caseWindow', 'toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=' + chatConfig.webToCaseSettings.width + ',height=' + chatConfig.webToCaseSettings.height);
};
if(options.settings.onOpenWebToCase)
openWebToCase = options.settings.onOpenWebToCase;
options.openWebToCase = openWebToCase;
//Special logic to handle showing web to case button when chat agent not online
if(options.settings.offlineEnableWebToCase) {
embedded_svc._onButtonStatusChange = embedded_svc.onButtonStatusChange;
embedded_svc.onButtonStatusChange = function() {
this._onButtonStatusChange();
//Check if chat snap-in is loaded
if(this.isIframeReady) {
//isButtonDisabled indicates whether we are in offline mode
if(this.isButtonDisabled) {
//Add click handler to offline button
var offlineButton = document.querySelector('.helpButtonDisabled');
offlineButton.classList.add("webToCaseButton");
offlineButton.onclick = function() {
openWebToCase();
}
}
}
};
}
}
//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 Live Agent
//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 = {}; //Sets the auto-population of pre-chat form fields
//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.settings.avatarImgURL = "..."
// embedded_svc.settings.chatbotAvatarImgURL = window.location.origin + "/img/avatar-bot.png";
embedded_svc.settings.extraPrechatFormDetails = [];
var url = window.location.href;
//If a specific url was provided in the options values, use it rather than just the current url
if(options.url)
url = options.url;
if(url && url.length > 255)
url = url.substring(0, 255);
//Pass url as contextual value
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"Case originated URL",
"transcriptFields":["Case_Originated_URL__c"],
"value": url,
"displayToAgent":true
},
{
"label":"Case origin",
"transcriptFields":[],
"value": "Chat (Converted to Case)",
"displayToAgent":true
}
// {
// "label":"Account",
// "value": config.defaultAccountId,
// "transcriptFields":[],
// "displayToAgent":true
// }
);
var caseFieldMaps = [{
"isExactMatch":false,
"fieldName":"Case_Originated_URL__c",
"doCreate":true,
"doFind":false,
"label":"Case originated URL"
},
{
"isExactMatch":false,
"fieldName":"Origin",
"doCreate":true,
"doFind":false,
"label": "Case origin"
}
];
var contactFieldMaps = [
{
"isExactMatch": false,
"fieldName": "FirstName",
"doCreate": true,
"doFind": false,
"label": "First Name"
},
{
"isExactMatch": false,
"fieldName": "LastName",
"doCreate": true,
"doFind": false,
"label": "Last Name"
},
{
"isExactMatch": true,
"fieldName": "Email",
"doCreate": true,
"doFind": true,
"label": "Email"
}
// {
// "isExactMatch": false,
// "fieldName": "AccountId",
// "doCreate": true,
// "doFind": false,
// "label": "Account"
// }
];
if(chatConfig.preform) {
embedded_svc.settings.prepopulatedPrechatFields = {};
if(options.product && options.product.toLowerCase() == 'grantspace')
embedded_svc.settings.prepopulatedPrechatFields['Reason'] = 'Training/Events';
if(options.subject) {
embedded_svc.settings.prepopulatedPrechatFields['Subject'] = options.subject;
}
if(options.reason) {
embedded_svc.settings.prepopulatedPrechatFields['Reason'] = options.reason;
}
}
if(!options.userInfo || chatConfig.preform) {
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"First Name",
"transcriptFields":["Contact_First_Name__c"]
});
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"Last Name",
"transcriptFields":["Contact_Last_Name__c"]
});
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"Email",
"transcriptFields":["Contact_Email__c"]
});
}
//If a product was supplied in the chat options, we map it to a transcript field
if(options.product) {
var product = options.product;
if(product && product.length > 255)
product = product.substring(0, 255);
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"Product",
"value": product,
"transcriptFields":["Product_Name__c"],
"displayToAgent":true
});
}
if(options.userInfo) {
var userInfo = options.userInfo;
if(chatConfig.preform) {
//Populate pre-form field defaults
if(userInfo.firstName) {
embedded_svc.settings.prepopulatedPrechatFields['FirstName'] = userInfo.firstName;
}
if(userInfo.lastName) {
embedded_svc.settings.prepopulatedPrechatFields['LastName'] = userInfo.lastName;
}
if(userInfo.email) {
embedded_svc.settings.prepopulatedPrechatFields['Email'] = userInfo.email;
}
if(userInfo.postalCode) {
embedded_svc.settings.prepopulatedPrechatFields['Postal_Code__c'] = userInfo.postalCode;
}
if(userInfo.country) {
embedded_svc.settings.prepopulatedPrechatFields['Country__c'] = userInfo.country;
}
/*embedded_svc.settings.prepopulatedPrechatFields = {
FirstName: 'Kevin',
LastName: 'Mulder',
Email: 'kevin.mulder.fc@gmail.com',
Subject: 'Help!',
//Case_Originated_URL__c: 'hereweare',
};*/
}
else {
if(userInfo.firstName) {
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"First Name",
"value": userInfo.firstName,
"transcriptFields":["Contact_First_Name__c"],
"displayToAgent":true
});
// contactFieldMaps.push({
// "isExactMatch": false,
// "fieldName": "FirstName",
// "doCreate": true,
// "doFind": false,
// "label": "First Name"
// });
}
if(userInfo.lastName) {
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"Last Name",
"value": userInfo.lastName,
"transcriptFields":["Contact_Last_Name__c"],
"displayToAgent":true
});
// contactFieldMaps.push({
// "isExactMatch": false,
// "fieldName": "LastName",
// "doCreate": true,
// "doFind": false,
// "label": "Last Name"
// });
}
if(userInfo.email) {
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"Email",
"value": userInfo.email,
"transcriptFields":["Contact_Email__c"],
"displayToAgent":true
});
// contactFieldMaps.push({
// "isExactMatch": true,
// "fieldName": "Email",
// "doCreate": true,
// "doFind": true,
// "label": "Email"
// });
}
}
//Add memberId value to contextual info if it was supplied
if(userInfo.memberId) {
embedded_svc.settings.extraPrechatFormDetails.push({
"label":"Member Id",
"transcriptFields":["Member_Id__c"],
"value": options.userInfo.memberId,
"displayToAgent":true
});
caseFieldMaps.push({
"isExactMatch":false,
"fieldName":"Member_Id__c",
"doCreate":true,
"doFind":false,
"label":"Member Id"
});
}
}
//Maps hidden fields to actual SF entity objects/fields
embedded_svc.settings.extraPrechatInfo = [];
//Add contact field mapping config, if there are any field values identified to be mapped
if(contactFieldMaps.length > 0) {
embedded_svc.settings.extraPrechatInfo.push({
"entityName": "Contact",
"showOnCreate": true,
"linkToEntityName": "Case",
"linkToEntityField": "ContactId",
"saveToTranscript": "Contact",
"entityFieldMaps": contactFieldMaps
});
}
embedded_svc.settings.extraPrechatInfo.push({
"entityName":"Case",
"showOnCreate":true,
"saveToTranscript":"CaseId",
"entityFieldMaps": caseFieldMaps
});
// console.log(embedded_svc);
// embedded_svc.addEventHandler('onhelpbuttonclick', function() { alert('ready')});
//Changes per environment - make this configurable
embedded_svc.init(
chatConfig.init1,
chatConfig.init2,
gslbBaseURL,
chatConfig.initId,
chatConfig.snapIn,
{
baseLiveAgentContentURL: chatConfig.baseLiveAgentContentURL,
deploymentId: chatConfig.deploymentId,
buttonId: chatConfig.buttonId,
baseLiveAgentURL: chatConfig.baseLiveAgentUrl,
eswLiveAgentDevName: chatConfig.eswLiveAgentDevName,
isOfflineSupportEnabled: chatConfig.isOfflineSupportEnabled
}
);
//updateShadowRootStyle();
};
function initializeChat(options) {
if(!options)
options = {};
options.hideButton = function(persistHide) {
var chatUiButton = document.querySelector('.embeddedServiceHelpButton .helpButton');
chatUiButton.classList.add("closed");
if(persistHide)
document.cookie = "chatClosed=1;path=/";
};
options.showButton = function() {
document.cookie = "chatClosed=0";
var chatButton = document.querySelector(".embeddedServiceHelpButton .helpButton");
if(chatButton) {
chatButton.classList.remove('closed');
}
else {
console.log('chat button not available');
}
};
options.open = function() {
var chatButton = document.querySelector(".embeddedServiceHelpButton .uiButton");
if(chatButton) {
chatButton.click();
}
else {
console.log('chat button not available');
}
//updateShadowRootStyle();
};
options.openChat = function() {
var chatButton = document.querySelector(".embeddedServiceHelpButton .helpButtonEnabled");
if(chatButton) {
chatButton.click();
}
else {
console.log('chat button not available');
}
};
if (!window.embedded_svc) {
var s = document.createElement('script');
s.setAttribute('src', 'https://candid.my.salesforce.com/embeddedservice/5.0/esw.min.js');
s.onload = function() {
initESW(null, options);
};
document.body.appendChild(s);
} else {
initESW('https://service.force.com', options);
}
//updateShadowRootStyle();
return {
hideButton: options.hideButton,
showButton: options.showButton,
open: options.open,
//Has to be a callback, as openWebToCase may not be available until later in the initalization
openWebToCase: function() { return options.openWebToCase() },
openChat: options.openChat
};
}
function updateShadowRootStyle() {
const shadowHost = document.getElementsByTagName('embeddedservice-chat-header');
const shadow = shadowHost.shadowRoot;
if (shadow) {
const childNodes = Array.from(shadow.childNodes);
childNodes.forEach(childNode => {
if (childNode.nodeName === 'STYLE') {
childNode.textContent = `
`;
}
});
}
}