Loading

Pass value from the prechat form to a custom field on the LiveChatTranscript object when using lightningsnapin:prechatAPI

Julkaisupäivä: Jun 14, 2026
Kuvaus

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:

  1. Salesforce Snap-ins does not load Chat APIs on the page directly.
  2. The Locker Service blocks access to the global 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.

 

Ratkaisu

The following steps describe how to capture a dynamic value from a custom prechat component and save it to a custom LiveChatTranscript field.

Step 1: Create a Custom Field on the Transcript Object

Create a custom field on the LiveChatTranscript object to store the prechat value. For example, create a text field named PrechatText__c.

Step 2: Configure extraPrechatFormDetails Without a Value

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"]}];

Step 3: Fire a Custom Event from the Prechat Component

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!");
}

 

Step 4: Listen for the Event in the Container Page

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
);


 

Knowledge-artikkelin numero

000380580

 
Ladataan
Salesforce Help | Article