When using the lightningsnapin:prechatAPI component in Salesforce Live Agent (also known as Snap-ins Chat), it is not possible to directly pass dynamic values from a custom prechat form into a custom field on the LiveChatTranscript object. This limitation exists for two reasons:
window object, where embedded_svc.settings.extraPrechatFormDetails is configured.The recommended workaround uses a custom browser event to communicate the prechat form value from the prechat Lightning component to the container page, where extraPrechatFormDetails is accessible and can be updated before the chat request is sent.
The following steps describe how to capture a dynamic value from a custom prechat component and save it to a custom LiveChatTranscript field.
Create a custom field on the LiveChatTranscript object to store the prechat value. For example, create a text field named PrechatText__c.
In the container page where the Snap-in is hosted (not inside the custom prechat component), configure extraPrechatFormDetails by mapping the label to the custom transcript field, but without setting the value property. This reserves the field mapping for the value that will be passed via the custom event:
embedded_svc.settings.extraPrechatFormDetails = [{"label":"Text", "transcriptFields": ["PrechatText__c"]}];
In your custom prechat component's startChat function, instead of calling startChat directly, fire a CustomEvent named setCustomField. The event detail should include the startChat callback and the custom field value:
In the custom prechat component's startChat function: validate the prechat fields, then create a CustomEvent named "setCustomField" with a detail containing two properties — "callback" (set to the startChat function bound to the current fields) and "customField" (set to the value you want to store in the transcript). Dispatch the event on the document object. If validation fails, log a warning.
if(cmp.find("prechatAPI").validateFields(fields).valid) {
var event = new CustomEvent(
"setCustomField",
{
detail: {
callback: cmp.find("prechatAPI").startChat.bind(this, fields),
customField: "value"
}
}
);
// Dispatch the event.
document.dispatchEvent(event);
} else {
console.warn("Prechat fields did not pass validation!");
}
In the container page, add an event listener for the setCustomField event. When the event is received, update extraPrechatFormDetails with the received value and call the startChat callback:
In the container page: add a document event listener for "setCustomField". When triggered, set embedded_svc.settings.extraPrechatFormDetails[0].value to the value from event.detail.customField, then call event.detail.callback() to initiate the chat.
document.addEventListener(
"setCustomField",
function(event) {
embedded_svc.settings.extraPrechatFormDetails[0].value = event.detail.customField;
// Fire startChat callback.
event.detail.callback();
},
false
);
000380580

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.