When customers start a messaging conversation, you sometimes already know basic information such as their name, email address, and what product they’re looking at. You can set up a pre-chat form in the UI with visible fields such as name, email address, and product name. Then use the SDK to populate the pre-chat fields with this information so it appears when the user first opens the pre-chat form.
This feature requires version 1.5.0 or later of the Enhanced In-App Chat SDK.
This article applies to the following implementations:
Before you use the SDK, add the visible fields to the pre-chat form. See Customize Pre-Chat for Enhanced Chat in Salesforce Help.
After you add the visible fields to the pre-chat form, you can populate these values by using the SDK.
- To populate known values in the pre-chat fields, implement
preChatFieldValueProvider.
- Add your provider to the SDK by using
init in the Interface.swift file.
- To modify the pre-chat fields, create a
setPreChatValues function.
- To make the pre-chat fields editable, set
isEditable to true. To make the pre-chat fields non-editable, set isEditable to false.
If you configure the pre-populated fields to be non-editable, make sure that the pre-chat form fields follow the validation rules. If a required field exceeds the character limit or is set to an invalid data type, the createConversation flow stops and the form isn’t submitted. In these cases, the SDK logs error messages in the developer console.
1Interface.swift
2/**
3 * A provider to populate known values in the pre-chat fields.
4 * It requires a function to modify the list of [PreChatField]s, before they're rendered in the pre-chat form.
5 */
6var preChatFieldValueProvider: (([PreChatField]) async throws -> [PreChatField])
7
8/**
9 * Add the provider using init in Interface.swift
10 */
11public init(_ config: UIConfiguration,
12 preChatFieldValueProvider: ((_ preChatFields: [PreChatField]) async throws -> [PreChatField])? = nil)
1// Create an key value pair object that will be pre-populated
2 let prechatKeyValuePairs = ["_firstName": "Joe", "_subject": "My app is not working"]
3
4 // Create a function to modify the pre-chat fields
5 private func setPreChatValues() -> (([PreChatField]) async throws -> [PreChatField]) { { prechatFields in
6 prechatFields.enumerated().forEach { (index, value) in
7 let currenPrechatName = value.name
8
9 for prechatKeyValuePair in prechatKeyValuePairs {
10 if currenPrechatName == prechatKeyValuePair.key {
11 prechatFields[index].value = // Set the value for the pre-chat fields
12 prechatFields[index].isEditable = // Set it to true to make the pre-chat fields editable, or to false to make them non-editable
13 }
14 }
15 }
16
17 return prechatFields
18 }
19 }