Newer Version Available

This content describes an older version of this product. View Latest

Pre-Chat Code Examples

The following examples illustrate some common use cases for pre-chat code snippets.

Find contacts but don't create new ones

In this example, we don't want to create contact records — we only want to find them. To disable creation of a record, set doCreate to false for all the required fields for the record. This code disables a common default behavior of creating a contact record with each chat session.

Don’t attach records to the chat transcript

If you don’t want to attach contact records to the transcript, set saveToTranscript to an empty string.

Attach a record to a custom field on the chat transcript

If you want to attach the created case or contact record to a custom field on the transcript, use saveToTranscript.

Don’t show the created record to the agent

If you don’t want to show the created record when the chat session starts, set showOnCreate to false.

Override fields specified in your org’s setup

This example overrides the first name, last name, and subject passed in from the pre-chat form. To test this code, select the service scenario in your org’s setup and add this code to your snippet.

Create a new record from a different Salesforce object

If your business needs a record from a Salesforce object that isn’t available in the standard scenarios, you can define the object in extraPrechatInfo. For example, you can create an account when a chat session starts.

Link to another Salesforce object

If you want to link the case record created by Embedded Chat to the account record you created from extraPrechatInfo, use linkToEntityName and linkToEntityFieldName.

Don’t attach a contact to a case

If you don’t want the default link between a contact and a case, set linkToEntityName to an empty string.

Disable pre-chat and pass along the user's name

If you want to avoid the pre-chat form but still have the user’s name show up in the Waiting state, you can:

  1. Find the object that contains "label" : "First Name".
  2. Add a property "name" : "FirstName".
  3. Set the value of the "value" property to the desired first name value. For example, "value" : "Jane".

Set custom fields on the transcript object

If you want to set custom fields on the transcript object, pass those fields in as transcript fields in prechatFormDetails. This code assumes you created a custom field called CartValue on the chat transcript object.

Save pre-chat form values into a custom field on the transcript object

If you want to save dynamic values from the pre-chat form directly into a custom field in the transcript object, you can:
  1. Create a custom field on the transcript object (for example, LastName__c).

    For the transcript field, the following data types work best: text, number, email, checkbox, and phone.

    Tip

  2. Pass the transcript fields in the extra pre-chat form details without passing the value property.

The value is calculated from the pre-chat form and saved into the custom field.

We don’t support passing pre-chat form values to standard transcript fields.

Note